luogu4931. A couple? Burn It for me! (Enhanced version) (dislocation arrangement)

Source: Internet
Author: User
Tags mul

Topic links

https://www.luogu.org/problemnew/show/P4931

Exercises

The following sections are my first thoughts.

For each \ (k\), meet exactly have \ (k\) couples in harmony with the number of programs

\[\binom{n}{k}x\binom{n}{k}xk!x2^kxf_{n-k}\]

Among them,\ (f_x\) means \ (x\) pairs of couples sitting \ (x\) row seats and no pair of couples sitting in the same row of the scheme number.

The meaning of the above expression is: from \ (n\) to the couple elected \ (k\) pairs as a harmonious, and then from the \ (n\) platoon (k\) platoon for this \ (k\) couples sitting, The number of scenarios is \ (\binom{n}{k}x\binom{n}{k}\). Because \ (k\) pairs of couples can not agree on the order, and each team couple inside the two can also exchange seats, so the program number is \ (k!x2^k\). Multiplied by the remaining \ (n-k\) couples do not appear in harmony with the number of programs is the final answer.

The question now is how to find out \ (f_x\). Consider the tolerance,\ (f_x=\) the number of options to sit ( -\) at least a couple of couples in harmony with the number of programs \ (+\) There are at least two couples in harmony with the number of programs \ (-\) There are at least three couples in harmony with the number of programs \ (... \)

So doing and similar analysis above, we can get:

\[f_x = \sum_{i = 0}^{x} ( -1) ^ix\binom{x}{i}x\binom{x}{i}xi!x2^ix (2x (x-i))!\]

The last \ ((2x (x-i))!\) of the above expression indicates the number of programs that do not force harmonious \ (x-i\) couples to sit at random.

The total preprocessing complexity is \ (O (n^2) \).

Although the idea of this kind of repulsion is natural, the complexity of time is not good. We need to find a better way to beg \ (f_x\).

Let's recall the recursive type of the wrong row:

\[d_n = (n-1) (d_{n-1} + d_{n-2}) (n \geq 2) \]

The proof of it in combinatorial mathematics is probably this:

We want to get \ (d_n\) , considering the first position of the arrangement of the length \ (n\) , with \ (2, 3, 4, \cdots, n\) total \ (n-1\) method of filling. Obviously, the number of scenarios is the same no matter who it starts with, so we just need to figure out the number of scenarios that begin with any number, multiplied by the \ (n-1\) . We assume that the first one fills in the \ (2\) , then the second position can be filled \ (1, 3, 4, \cdots, n\) . If you fill in the \ (1\) , then the rest is a misplaced arrangement of \ (3, 4, \cdots, n\) . The number of scenarios is \ (d_{n-2}\) ; If you do not fill \ (1\) , then this bit \ (1\) and \ (3, 4, \cdots, n\) together constitute the length of \ ( n-1\) , the scheme number is \ (d_{n-1}\) , so there is \ (D_n = (n-1) (D_ {n-1} + d_{n-2}) (n \geq 2) \) .

We extend it to this topic. If \ (n\) pair of couples to sit \ (n\) row seats, consider the first row of seats must sit two non-reciprocal couples, a total of span class= "Math inline" >\ (2n \times (2n-2) = 4n (n-1) \) scheme (the first person can be \ (2n\) personal Any one of the faces, the second person can be the remaining \ (2n-1\) any one of the individuals except the first person's spouse). There are two different cases: if the first row of the two persons corresponding to the spouse also sat in the same row, then the remaining \ (n-2\) for the couple sitting and not satisfied with the same row couples of the program number is \ (f_{n-2}\) , because the first row of two people corresponding to the spouse can choose \ (n-1\) row in a row to sit, and can be swapped position, Therefore, the total number of scenarios in this case is \ (2 (n-1) f_{n-2}\) ; If the spouses of the first row of two people do not sit in the same row, then we can treat them as a couple, they and the rest of the span class= "Math inline" >\ (n-2\) the number of programs that are valid for couples is \ (f_{n-1}\) .

In this way, we get the recursion of \ (f\) :\ (f_n = 4n (n-1) (F_{n-1} + 2 (n-1) f_{n-2}) (n \geq 2) \), the boundary is \ (f_0 = 1, f_1 = 0\). This problem can be preprocessed in the time of \ (O (n) \) .

Code
#include <bits/stdc++.h>using namespace std; #define X first#define Y second#define MP Make_pair#define PB push_back #define DEBUG (...) fprintf (stderr, __va_args__) typedef long Long ll;typedef long double ld;typedef unsigned int uint;typed  EF pair<int, int> pii;typedef unsigned long long ull;template<typename t> inline void Read (t& x) {Char    c = GetChar ();    bool F = false;        for (x = 0;!isdigit (c); c = GetChar ()) {if (c = = '-') {F = true;    }} for (; IsDigit (c); c = GetChar ()) {x = x * + C-' 0 ';    } if (f) {x = x; }}template<typename T, TypeName ... u> inline void Read (t& x, u& ... y) {read (x), read (Y ...);} Template<typename t> inline bool Checkmax (t& A, const t& b) {return a < b? a = B, true:false;} Template<typename t> inline bool Checkmin (t& A, const t& b) {return a > b? a = B, true:false;} const int N = 5e6 + ten, mod = 998244353;inline voID mul (int& x, int y) {x = 1ll * x * y% MoD;}    inline int Qpow (int v, int p) {int res = 1;        for (; p; p >>= 1, Mul (V, v)) {if (P & 1) {Mul (res, v); }} return res;} int fac[n], invfac[n], pow2[n], f[n];inline int binom (int N, int m) {return 1LL * fac[n] * invfac[m]% mod * invfac[n -m]% MoD;}    void init (int n) {fac[0] = invfac[0] = pow2[0] = f[0] = 1;        for (register int i = 1; I <= n; ++i) {Fac[i] = 1LL * fac[i-1] * I% mod;        Pow2[i] = (Pow2[i-1] << 1)% MoD;        if (i > 1) {f[i] = 4ll * I * (i-1)% mod * (f[i-1] + 2ll * (i-1) * f[i-2]% mod)% MoD;    }} Invfac[n] = Qpow (Fac[n], mod-2);    for (register int i = n-1; i; i) {invfac[i] = 1ll * invfac[i + 1] * (i + 1)% MoD;    }}int Main () {init (5000000); int t;    Read (t);        for (register int kase = 1; kase <= T; ++kase) {int n, k; Read (n, k); printf ("%lld\n", 1ll * BINOM (n, k) * Binom (n, k)% mod * fac[k]% mod * pow2[k]% mod * f[n-k]% mod); } return 0;}

luogu4931. A couple? Burn It for me! (Enhanced version) (dislocation arrangement)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.