【BZOJ5306】【HAOI2018】—染色(组合数学+NTT)

传送门


由于恰好不好要求
考虑f[i]f[i]表示至少ii个颜色数量为SS的方案数

f[i]=(mi)(nis)(mi)nis(is)!(s!)if[i]={m\choose i}{n\choose i*s}(m-i)^{n-i*s}*\frac{(i*s)!}{(s!)^i}

这个式子还是比较显然的
ans[i]ans[i]表示恰好ii个的方案数
K=min(m,n/s)K=min(m,n/s)即最多的颜色数

f[i]=j=iK(ji)ans[j]f[i]=\sum_{j=i}^{K}{j\choose i}ans[j]
有那个组合数的原因是ans[j]ans[j]中任意ii个都会在f[i]f[i]中计算到一次

二项式反演得ans[i]=j=iK(1)ji(ji)f[j]ans[i]=\sum_{j=i}^{K}(-1)^{j-i}{j\choose i}f[j]
把组合数拆开
ans[i]=1i!j=iK(1)ji(ji)!j!f[j]ans[i]=\frac 1 {i!}\sum_{j=i}^{K}\frac{(-1)^{j-i}}{(j-i)!} j!f[j]

f(x)=i=0K(ki)!f[ki]xif(x)=\sum_{i=0}^{K}(k-i)!f[k-i]x^i
g(x)=i=0K(1)ii!g(x)=\sum_{i=0}^{K}\frac{(-1)^i}{i!}

ans=fgans=f*g
nttntt就完了

#include<bits/stdc++.h>
using namespace std;
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define re register
#define pb push_back
#define cs const
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int mod=1004535809,G=3;
inline int add(int a,int b){return (a+=b)>=mod?a-mod:a;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline int dec(int a,int b){return (a-=b)<0?a+mod:a;}
inline void Dec(int &a,int b){(a-=b)<0?(a+=mod):0;}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){
	for(;b;b>>=1,a=mul(a,a))(b&1)&&(res=mul(res,a));return res;
}
inline int Inv(int x){return ksm(x,mod-2);}
cs int N=100005;
#define poly vector<int>
int rev[N<<2];
inline void ntt(poly &f,int lim,int kd){
	for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
	for(int a0,a1,mid=1;mid<lim;mid<<=1){
		int wn=ksm(G,(mod-1)/(mid<<1));
		for(int i=0;i<lim;i+=(mid<<1)){
			int w=1;
			for(int j=0;j<mid;j++,Mul(w,wn)){
				a0=f[i+j],a1=mul(w,f[i+j+mid]);
				f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
			}
		}
	}
	if(kd==-1){
		reverse(f.begin()+1,f.begin()+lim);
		for(int inv=Inv(lim),i=0;i<lim;i++)Mul(f[i],inv);
	}
}
inline void init_rev(int lim){
	for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
inline poly mul(poly &a,poly &b){
	int deg=a.size()+b.size()-1,lim=1;
	while(lim<deg)lim<<=1;
	init_rev(lim);
	a.resize(lim),b.resize(lim);
	ntt(a,lim,1),ntt(b,lim,1);
	for(int i=0;i<lim;i++)Mul(a[i],b[i]);
	ntt(a,lim,-1),a.resize(deg);
	return a;
}
cs int M=10000005;
int fac[M],ifac[M];
inline void init(int mx){
	fac[0]=ifac[0]=1;
	for(int i=1;i<=mx;i++)fac[i]=mul(fac[i-1],i);
	ifac[mx]=ksm(fac[mx],mod-2);
	for(int i=mx-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
inline int C(int n,int m){
	if(n<m)return 0;
	return mul(fac[n],mul(ifac[m],ifac[n-m]));
}
int n,m,s,w[N],K;
poly f,g;
inline int F(int i){
	int a=mul(C(m,i),C(n,i*s));
	Mul(a,mul(ksm(m-i,n-i*s),fac[i*s]));
	Mul(a,Inv(ksm(fac[s],i)));
	return a;
}
int main(){
	n=read(),m=read(),s=read();
	init(max(max(s,n),m));
	for(int i=0;i<=m;i++)w[i]=read();
	K=min(m,n/s);
	for(int i=0;i<=K;i++)f.pb(mul(F(K-i),fac[K-i]));
	for(int i=0;i<=K;i++)g.pb((i&1)?(mod-ifac[i]):ifac[i]);
	poly t=mul(f,g);
	int res=0;
	for(int i=0;i<=K;i++)Add(res,mul(ifac[i],mul(w[i],t[K-i])));
	cout<<res;
}

posted @ 2019-07-31 16:04  Stargazer_cykoi  阅读(128)  评论(0编辑  收藏  举报