【BZOJ3684】—大朋友和多叉树(NTT+拉格朗日反演)

传送门


DD的生成函数C(x)=iDxiC(x)=\sum_{i\in D}x^i

则神犇多叉树的OGF:f(x)=iDf(x)i+x=C(f(x))xOGF:f(x)=\sum_{i\in D}f(x)^i+x=C(f(x))-x
那么有C(f(x))f(x)=xC(f(x))-f(x)=x

考虑最后要求的是ff的第ss

拉格朗日反演:

若有f(x),g(x)f(x),g(x)满足:

f(g(x))=xf(g(x))=x

那么有

g(f(x))=xg(f(x))=x

f,gf,g互为复合逆

且有
[xn]f(x)=1n[x1]1g(x)n=1n[xn1](xg(x))n [x^n]f(x)=\frac 1 n[x^{-1}]\frac{1}{g(x)^n} \\ =\frac{1}{n}[x^{n-1}](\frac{x}{g(x)}) ^n


这里相当于设G(x)=C(x)xG(x)=C(x)-x
那么就是G(f(x))=xG(f(x))=x

那么最后求的就是1s[xs1](xG(x))s\frac 1 s[x^{s-1}](\frac x {G(x)})^s

由于C(x)C(x)最低项为x2(d2)x^2(d\ge2)
所以G(x)G(x)没有常数项

就可以把上面的xx消去
然后多项式快速幂就可以了

#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));
}
posted @ 2019-08-14 22:00  Stargazer_cykoi  阅读(117)  评论(0编辑  收藏  举报