【BZOJ3684】—大朋友和多叉树(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
#define poly vector<int>
#define bg begin
cs int mod=950009857,G=7;
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 void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=400005,C=21;
poly w[C+1];
int rev[N],inv[N];
inline void init_w(){
for(int i=1;i<=C;i++)w[i].resize(1<<(i-1));
w[C][0]=1;
int wn=ksm(G,(mod-1)/(1<<C));
for(int i=1;i<(1<<(C-1));i++)w[C][i]=mul(w[C][i-1],wn);
for(int i=C-1;i;i--)
for(int j=0;j<(1<<(i-1));j++)
w[i][j]=w[i+1][j<<1];
}
inline void init_inv(){
inv[0]=inv[1]=1;
for(int i=2;i<N;i++)inv[i]=mul(mod-mod/i,inv[mod%i]);
}
inline void init_rev(int lim){
for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
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,l=1;mid<lim;mid<<=1,l++)
for(int i=0;i<lim;i+=(mid<<1))
for(int j=0;j<mid;j++)
a0=f[i+j],a1=mul(w[l][j],f[i+j+mid]),f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
if(kd==-1){reverse(f.bg()+1,f.bg()+lim);for(int i=0;i<lim;i++)Mul(f[i],inv[lim]);}
}
inline poly operator +(poly a,cs poly &b){
if(a.size()<b.size())a.resize(b.size());
for(int i=0;i<b.size();i++)Add(a[i],b[i]);
return a;
}
inline poly operator -(poly a,cs poly &b){
if(a.size()<b.size())a.resize(b.size());
for(int i=0;i<b.size();i++)Dec(a[i],b[i]);
return a;
}
inline poly operator *(poly a,int b){
for(int i=0;i<a.size();i++)Mul(a[i],b);
return a;
}
inline poly operator *(poly a,poly b){
int deg=a.size()+b.size()-1,lim=1;
if(deg<=128){
poly c(deg,0);
for(int i=0;i<a.size();i++)
for(int j=0;j<b.size();j++)
Add(c[i+j],mul(a[i],b[j]));
return c;
}
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;
}
inline poly Inv(cs poly &a,int deg){
poly b(1,ksm(a[0],mod-2)),c;
for(int lim=4;lim<(deg<<2);lim<<=1){
c=a,c.resize(lim>>1);
init_rev(lim);
b.resize(lim),ntt(b,lim,1);
c.resize(lim),ntt(c,lim,1);
for(int i=0;i<lim;i++)Mul(b[i],dec(2,mul(b[i],c[i])));
ntt(b,lim,-1),b.resize(lim>>1);
}b.resize(deg);return b;
}
inline poly integ(poly f){
for(int i=0;i<f.size()-1;i++)f[i]=mul(f[i+1],i+1);
f.pop_back();return f;
}
inline poly deriv(poly f){
f.pb(0);
for(int i=f.size()-1;i;i--)f[i]=mul(f[i-1],inv[i]);
f[0]=0;return f;
}
inline poly Ln(poly f,int deg){
f=deriv(integ(f)*Inv(f,deg));f.resize(deg);
return f;
}
inline poly exp(poly b,int deg){
poly a(1,1),c;
for(int lim=2;lim<(deg<<1);lim<<=1){
c=Ln(a,lim),c=b-c,c[0]++;
a=a*c,a.resize(lim);
}a.resize(deg);return a;
}
inline poly ksm(poly a,int b,int deg){
a=exp(Ln(a,deg)*b,deg);return a;
}
int s,m;
poly g;
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
init_w(),init_inv();
s=read(),m=read();
g.resize(s);
for(int i=1;i<=m;i++)g[read()-1]=mod-1;
g[0]++,g=ksm(Inv(g,s),s,s);
cout<<mul(g[s-1],ksm(s,mod-2));
}