【BZOJ5306】【HAOI2018】—染色(组合数学+NTT)
由于恰好不好要求
考虑表示至少个颜色数量为的方案数
这个式子还是比较显然的
令表示恰好个的方案数
即最多的颜色数
有
有那个组合数的原因是中任意个都会在中计算到一次
二项式反演得
把组合数拆开
令
则
就完了
#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;
}