bzoj3684: 大朋友和多叉树(拉格朗日反演+多项式全家桶)

题面

传送门

题解

首先你得知道什么是拉格朗日反演->这里

我们列出树的个数的生成函数

\[T(x)=x+\prod_{i\in D}T^i(x) \]

\[T(x)-\prod_{i\in D}T^i(x)=x \]

我们记\(F(x)=T(x)\)\(G(x)=x-\prod_{i\in D}x^i\),那么有\(G(F(x))=x\)

根据拉格朗日反演,可得

\[[x^n]F(x)=\frac{1}{n}[x^{-1}]\frac{1}{G(x)^n} \]

//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
    R int res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
const int N=5e5+5,P=950009857,g=7,Gi=135715694;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
	R int res=1;
	for(;y;y>>=1,x=mul(x,x))if(y&1)res=mul(res,x);
	return res;
}
int r[N],O[N],F[N],G[N],inv[N],lim,l,n,m;
void init(R int len){
	lim=1,l=0;while(lim<len)lim<<=1,++l;
	fp(i,0,lim-1)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
}
void NTT(int *A,int ty){
	fp(i,0,lim-1)if(i<r[i])swap(A[i],A[r[i]]);
	for(R int mid=1;mid<lim;mid<<=1){
		int I=(mid<<1),Wn=ksm(ty==1?g:Gi,(P-1)/I);O[0]=1;
		fp(i,1,mid-1)O[i]=mul(O[i-1],Wn);
		for(R int j=0;j<lim;j+=I)fp(k,0,mid-1){
			int x=A[j+k],y=mul(O[k],A[j+k+mid]);
			A[j+k]=add(x,y),A[j+k+mid]=dec(x,y);
		}
	}
	if(ty==-1)for(R int i=0,inv=ksm(lim,P-2);i<lim;++i)A[i]=mul(A[i],inv);
}
void Inv(int *a,int *b,int len){
	if(len==1)return b[0]=ksm(a[0],P-2),void();
	Inv(a,b,len>>1);static int A[N],B[N];init(len<<1);
	fp(i,0,len-1)A[i]=a[i],B[i]=b[i];
	fp(i,len,lim-1)A[i]=B[i]=0;
	NTT(A,1),NTT(B,1);
	fp(i,0,lim-1)A[i]=mul(A[i],mul(B[i],B[i]));
	NTT(A,-1);
	fp(i,0,len-1)b[i]=dec(add(b[i],b[i]),A[i]);
	fp(i,len,lim-1)b[i]=0;
}
void Ln(int *a,int *b,int len){
	static int A[N],B[N];
	fp(i,1,len-1)A[i-1]=mul(a[i],i);A[len-1]=0;
	Inv(a,B,len),init(len<<1);
	fp(i,len,lim-1)A[i]=B[i]=0;
	NTT(A,1),NTT(B,1);
	fp(i,0,lim-1)A[i]=mul(A[i],B[i]);
	NTT(A,-1);
	fp(i,1,len-1)b[i]=mul(A[i-1],inv[i]);b[0]=0;
	fp(i,len,lim-1)b[i]=0;
}
void Exp(int *a,int *b,int len){
	if(len==1)return b[0]=1,void();
	Exp(a,b,len>>1);static int A[N];
	Ln(b,A,len),init(len<<1);
	A[0]=dec(a[0]+1,A[0]);
	fp(i,1,len-1)A[i]=dec(a[i],A[i]);
	fp(i,len,lim-1)A[i]=b[i]=0;
	NTT(A,1),NTT(b,1);
	fp(i,0,lim-1)b[i]=mul(b[i],A[i]);
	NTT(b,-1);
	fp(i,len,lim-1)b[i]=0;
}
void ksm(int *a,int *b,int len,int k){
	static int A[N];
	Ln(a,A,len);
	fp(i,0,len-1)A[i]=mul(A[i],k);
	Exp(A,b,len);
}
int Lagrange(int *a,int len,int k){
	static int A[N],B[N];
	Inv(a,A,len),ksm(A,B,len,k);
	return mul(B[k-1],inv[k]);
}
int main(){
//	freopen("testdata.in","r",stdin);
	n=read(),m=read();
	int len=1;while(len<=n)len<<=1;
	inv[0]=inv[1]=1;fp(i,2,len)inv[i]=1ll*(P-P/i)*inv[P%i]%P;
	++F[0];while(m--)--F[read()-1];
	printf("%d\n",Lagrange(F,len,n));
	return 0;
}
posted @ 2019-03-05 13:12  bztMinamoto  阅读(538)  评论(0编辑  收藏  举报
Live2D