[Loj # 3106] [TJOI2019] sing, dance, rap and basketball

Description of the meaning of problems

Each student likes to sing, dance, \ (RAP \) , basketball in one, like the number of items were \ (a, b, c,
d \) from which to select \ (n \) students lined up one, requiring 4 consecutive students do not exist once like to sing, dance, \ (RAP \) , basketball.
Total seeking arrangement for modulo 998,244,353.

\(n\leq 1000\)
\(a,b,c,d\leq 500\)


idea

Inclusion and exclusion classic title, but I was too inexperienced a ......

Set \ (f [m] \) is at least \ (m \) stack number does not meet the conditions are arranged.
Binomial or generalized inversion by inclusion and exclusion, \ (ANS = \ SUM \ limits_ = {0} ^ n-I (-1) ^ I \ Binom {I} {0} F [I] = \ SUM \ limits_ {i = 0} ^ n (-1) ^ if [i] \)

How to calculate \ (f [m] \) it?
First students packaged together into a pile, and then discharge them to a position, a total of \ (\ binom {n-3m } {m} \) seed discharge method.
Then fill the remaining students hobby.
The remaining \ (n-4m \) positions, left \ (am, bm, cm, dm \) likes to sing, jump, \ (RAP \) , basketball places.
It is with repeated permutation problem, let \ (n-4m \) positions in exactly \ (ta, tb, tc, td \) likes to sing, dance, \ (RAP \) , basketball students, then the arrangement number \ (\ frac {(n-
4m)!} {ta! tb! tc! td!} \) the total number of permutations of the remaining students \ (\ sum \ limits_ {ta + tb + tc + td = n- 4m} \ frac {(n-
4m)!} {ta! tb! tc! td!} \) then how do it? Notice \ (TA + TB + = the n-tc + td-4M \) , which makes us think of convolution (or generating function).

Set \ (FA (X) = \ SUM \ limits_ {I = 0} ^ {AM} \ FRAC {. 1} {I!} X ^ I \) , \ (FB (X) = \ SUM \ limits_ {I = 0} ^ {BM} \ FRAC {. 1} {I!} X ^ I \)
\ (FC (X) = \ SUM \ limits_ {I = 0} ^ {cm & lt} \ FRAC {. 1} {I!} X ^ I \) , \ (FD (X) = \ SUM \ limits_ {I = 0} ^ {DM} \ FRAC {. 1} {I!} X ^ I \)
disposed \ (g (x) = fa (x ) fb (x) fc (x ) fd (x) \) a \ (x ^ {n-4m } \) coefficients is \ (P \)
to \ (fa (x), fb (x), fc ( x), fd (x) \ ) are forward \ (NTT \) after full to multiply the point value, then after reverse \ (NTT \) obtaining \ (P \)
the total number of permutations of the remaining students \ (P (n-4m)! \)

The \ (f [m] = \ binom {n-3m} {m} p (n-4m)! \)

Seeking finished \ (f [] \) offspring into the broad principle of inclusion-exclusion formula you can find the answer.
The total complexity is \ (O (n ^ 2 logn ) \)


to sum up

model

Just one kind of inclusion and exclusion from the calculation model at least it (the generalized inclusion and exclusion, binomial inversion)

二项式反演:
\(f(n)=\sum\limits_{i=0}^n \binom{n}{i} g(i) \Rightarrow g(n)=\sum\limits_{i=0}^n (-1)^{n-i}\binom{n}{i} f(i)\)
\(f(k)=\sum\limits_{i=k}^n \binom{i}{k} g(i) \Rightarrow g(k)=\sum\limits_{i=k}^n (-1)^{i-k}\binom{i}{k} f(i)\)

Code force

Note \ (ntt \) problems in the array size.
If the maximum number of two polynomials \ (n-, m \) (respectively \ (n + 1, m + 1 \) the maximum number of times the term), it is by the \ (n-m + \) ( \ (n-+ m + 1 \) item)


Code

#include<cstdio>
#include<iostream>
#include<algorithm>

#define P 998244353

using namespace std;

const int N = 2050;

int n,a,b,c,d;
int f[N],C[N][N],mul[N],inv[N];

int Plus(int x,int y) { return x+y>=P ? x+y-P : x+y; }
int Minus(int x,int y) { return x>=y ? x-y : x-y+P; }

int Pow_mod(int x,int y){
	int ret=1;
	while(y){
		if(y&1) ret=1ll*ret*x%P;
		x=1ll*x*x%P;
		y>>=1;
	}
	return ret;
}
int l,r[N],X[N];
void ntt(int *A,int ty){
	for(int i=0;i<l;i++) X[r[i]]=A[i];
	for(int i=0;i<l;i++) A[i]=X[i];
	for(int i=2;i<=l;i<<=1){
		int wn=Pow_mod(3,(P-1)/i);
		if(ty==-1) wn=Pow_mod(wn,P-2);
		for(int j=0;j<l;j+=i){
			int w=1;
			for(int k=j;k<j+i/2;k++){
				int t=1ll*A[k+i/2]*w%P;
				A[k+i/2]=Minus(A[k],t);
				A[k]=Plus(A[k],t);
				w=1ll*w*wn%P;
			}
		}
	}
	if(ty==1) return;
	int Inv=Pow_mod(l,P-2);
	for(int i=0;i<l;i++) A[i]=1ll*A[i]*Inv%P;
}

int x[N],y[N];
void cal(int i){
	l=1;
	while(l<a+b+c+d-4*i) l<<=1;  /**/
	for(int j=1;j<l;j++) r[j]=(r[j>>1]>>1)|((j&1)*(l>>1));
	//[0,a-x]
	for(int j=0;j<=a-i;j++) x[j]=inv[j];
	for(int j=a-i+1;j<l;j++) x[j]=0;
	ntt(x,1);
	for(int j=0;j<l;j++) y[j]=x[j];
	//[0,b-x]
	for(int j=0;j<=b-i;j++) x[j]=inv[j];
	for(int j=b-i+1;j<l;j++) x[j]=0;
	ntt(x,1);
	for(int j=0;j<l;j++) y[j]=1ll*x[j]*y[j]%P;
	//[0,c-x]
	for(int j=0;j<=c-i;j++) x[j]=inv[j];
	for(int j=c-i+1;j<l;j++) x[j]=0;
	ntt(x,1);
	for(int j=0;j<l;j++) y[j]=1ll*x[j]*y[j]%P;
	//[0,d-x]
	for(int j=0;j<=d-i;j++) x[j]=inv[j];
	for(int j=d-i+1;j<l;j++) x[j]=0;
	ntt(x,1);
	for(int j=0;j<l;j++) y[j]=1ll*x[j]*y[j]%P;
	//re
	ntt(y,-1);
}

int main()
{
	scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);
	int mn=min(min(a,b),min(c,d));
	
	C[0][0]=1;
	for(int i=1;i<=n;i++){
		C[i][0]=C[i][i]=1;
		for(int j=1;j<i;j++)
			C[i][j]=Plus(C[i-1][j-1],C[i-1][j]);
	}
	mul[0]=1;
	for(int i=1;i<=1000;i++) mul[i]=1ll*mul[i-1]*i%P; /**/
	inv[1000]=Pow_mod(mul[1000],P-2);
	for(int i=999;i>=0;i--) inv[i]=1ll*inv[i+1]*(i+1)%P; /**/
	
	for(int i=0;i*4<=n && i<=mn;i++){
		f[i]=1ll*C[n-3*i][i]*mul[n-4*i]%P;
		cal(i);
		f[i]=1ll*f[i]*y[n-4*i]%P;
	}

	int ans=0;
	for(int i=0;i*4<=n && i<=mn;i++){
		if(i&1) ans=Minus(ans,f[i]);
		else ans=Plus(ans,f[i]);
	}
	printf("%d\n",ans);
	
	return 0;
}

Guess you like

Origin www.cnblogs.com/lindalee/p/12488494.html