[Luogu4921] lovers? Burn It for me! [Dislocation arrangement]

Source: Internet
Author: User

Test instructions

Test instructions very clear \ Funny

Analysis
  • For each query \ (k\) , remember \ ( g (x) \) indicates that \ (x\) couples are staggered by the total number of scenarios, then the answer can be written as follows:
    \[{ans}_k= \binom{n}{k}\times a_n^k\times 2^k\times g (n-k) \]
  • Consider how to ask for \ (g (x) \) (a misaligned arrangement).

  • Consider the first row, a total of three cases: two men and two women or a man and a woman (not paired).

    • Two male : the number of options for selecting two males in sequence is \ (x (x-1) \) , then consider the pairing of their spouse in the following situation:

      • If the force is not paired, then treat them as a couple to ensure that after the process is not paired (gay gay qi ), ie (g (x-1) \) .

      • If you force pairing, then select a row in the remaining \ (x-1\) row, and the two can be exchanged in order and transferred to \ (2 (x-1) \times g (x-2) \).

    • Two women : the number of programmes is clearly the same as for the two men.

    • a man and a woman: enumeration of a man and a woman, can be exchanged in the order of the number of programs (x (x-1) \) , the transfer is actually the same,

  • So we get:\ (g (x) =4x (x-1) \times[g (x-1) +2 (x-1) \times g (x-2)]\) .

  • Single processing \ (g\) complexity \ ( o (n) \) , each answer enumeration \ (k\) complexity \ (o (n) \) , the total time complexity is \ (o (n) \) .

Code
#include <bits/stdc++.h>using namespace std; #define REP (i,a,b) for (int i=a;i<=b;++i) #define PB Push_    Backtypedef long Long ll;inline int gi () {int X=0,f=1;char ch=getchar ();    while (!isdigit (CH)) {if (ch== '-') F=-1;ch=getchar ();}    while (IsDigit (ch)) {x= (x<<3) + (x<<1) +ch-48;ch=getchar ();} return x*f;} Template<typename t>inline bool Max (T &a,t b) {return a<b?a=b,1:0;} Template<typename t>inline bool Min (T &a,t b) {return b<a?a=b,1:0;} const int N=2004,mod=998244353;int t,n; ll fac[n],inv[n],invfac[n],bin[n],g[n];void Add (ll &a,ll b) {a+=b;if (a>=mod) A-=mod;}    ll Pow (ll A,ll b) {ll res=1ll;    for (; b;b>>+1,a=a*a%mod) if (b&1) Res=res*a%mod; return res;} LL C (int n,int m) {return fac[n]*invfac[n-m]%mod*invfac[m]%mod;}    int main () {fac[0]=invfac[0]=inv[1]=bin[0]=1;        Rep (i,1,n-1) {if (i^1) inv[i]= (mod-mod/i) *inv[mod%i]%mod;        Fac[i]=fac[i-1]*i%mod;        Invfac[i]=invfac[i-1]*inv[i]%mod; bin[i]=bin[i-1]*2%mod;    } g[0]=1,g[1]=0;        Rep (n,2,1000) g[n]=4ll*n* (n-1)%mod* (g[n-1]+2* (n-1) *g[n-2])%mod;    T=gi ();        while (t--) {n=gi ();    Rep (k,0,n) printf ("%lld\n", C (n,k) *c (n,k)%mod*fac[k]%mod*bin[k]%mod*g[n-k]%mod); } return 0;}

[Luogu4921] lovers? Burn It for me! [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.