2019.01.03 bzoj3456: 城市规划(生成函数+多项式取对)

传送门

生成函数好题。

题意:求n个点的简单(无重边无自环)无向连通图数目


思路:

对简单无向图构造生成函数f(x)=∑n2Cn2xnn!f(x)=\sum_n2^{C_n^2}\frac{x^n}{n!}f(x)=∑n​2Cn2​n!xn​

然后令答案的生成函数为g(x)=∑ncnxnn!g(x)=\sum_nc_n\frac{x^n}{n!}g(x)=∑n​cn​n!xn​

由于f(x)f(x)f(x)是由g(x)g(x)g(x)平凑而成,所以有f(x)=eg(x)f(x)=e^{g(x)}f(x)=eg(x)所以g(x)=lnf(x)g(x)=lnf(x)g(x)=lnf(x)

然后上一波多项式取对即可。

代码:

#include<bits/stdc++.h>
#define ri register int
#define add(a,b) ((a)+(b)>=mod?(a)+(b)-mod:(a)+(b))
#define dec(a,b) ((a)>=(b)?(a)-(b):(a)-(b)+mod)
#define mul(a,b) ((ll)(a)*(b)%mod)
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
typedef long long ll;
const int mod=1004535809;
int n,lim,tim;
vector<int>A,B,pos,Inv,ifac,fac;
inline void init(const int&up){
	lim=1,tim=0;
	while(lim<=up)lim<<=1,++tim;
	pos.resize(lim),A.resize(lim),B.resize(lim),pos[0]=0;
	for(ri i=0;i<lim;++i)pos[i]=(pos[i>>1]>>1)|((i&1)<<(tim-1));
}
inline int ksm(int a,ll p){int ret=1;for(;p;p>>=1,a=mul(a,a))if(p&1)ret=mul(ret,a);return ret;}
inline void ntt(vector<int>&a,const int&type){
	for(ri i=0;i<lim;++i)if(i<pos[i])swap(a[i],a[pos[i]]);
	for(ri mid=1,wn,mult=(mod-1)/2,typ=type==1?3:(mod+1)/3;mid<lim;mid<<=1,mult>>=1){
		wn=ksm(typ,mult);
		for(ri j=0,len=mid<<1;j<lim;j+=len)for(ri k=0,a0,a1,w=1;k<mid;++k,w=mul(w,wn)){
			a0=a[j+k],a1=mul(w,a[j+k+mid]);
			a[j+k]=add(a0,a1),a[j+k+mid]=dec(a0,a1);
		}
	}
	if(type==-1)for(ri i=0,inv=ksm(lim,mod-2);i<lim;++i)a[i]=mul(a[i],inv);
}
struct poly{
	vector<int>a;
	poly(int k=0,int x=0){a.resize(k+1),a[k]=x;}
	inline int&operator[](const int&k){return a[k];}
	inline const int&operator[](const int&k)const{return a[k];}
	inline int deg()const{return a.size()-1;}
	inline poly extend(const int&k){poly ret=*this;return ret.a.resize(k+1),ret;}
	friend inline poly operator+(const poly&a,const poly&b){
		poly ret(max(a.deg(),b.deg()));
		for(ri i=0;i<=a.deg();++i)ret[i]=add(ret[i],a[i]);
		for(ri i=0;i<=b.deg();++i)ret[i]=add(ret[i],b[i]);
		return ret;
	}
	friend inline poly operator-(const poly&a,const poly&b){
		poly ret(max(a.deg(),b.deg()));
		for(ri i=0;i<=a.deg();++i)ret[i]=add(ret[i],a[i]);
		for(ri i=0;i<=b.deg();++i)ret[i]=dec(ret[i],b[i]);
		return ret;
	}
	friend inline poly operator*(const int&a,const poly&b){
		poly ret(b.deg());
		for(ri i=0;i<=b.deg();++i)ret[i]=mul(a,b[i]);
		return ret;
	}
	friend inline poly operator*(const poly&a,const poly&b){
		int n=a.deg(),m=b.deg();
		init(n+m);
		poly ret;
		for(ri i=0;i<=n;++i)A[i]=a[i];
		for(ri i=0;i<=m;++i)B[i]=b[i];
		for(ri i=n+1;i<lim;++i)A[i]=0;
		for(ri i=m+1;i<lim;++i)B[i]=0;
		ntt(A,1),ntt(B,1);
		for(ri i=0;i<lim;++i)A[i]=mul(A[i],B[i]);
		return ntt(A,-1),ret.a=A,ret;
	}
	inline poly poly_inv(poly a,const int&k){
		if(k==1)return (poly){0,ksm(a[0],mod-2)};
		a=a.extend(k);
		poly f0=poly_inv(a,(k+1)>>1);
		return (2*f0-((f0*f0.extend(k))*a).extend(k)).extend(k);
	}
	inline poly poly_direv(const poly&a){
		poly ret(a.deg()-1);
		for(ri i=0;i<=ret.deg();++i)ret[i]=mul(a[i+1],i+1);
		return ret;
	}
	inline poly poly_inter(const poly&a){
		poly ret(a.deg()+1);
		for(ri i=1;i<=ret.deg();++i)ret[i]=mul(a[i-1],Inv[i]);
		return ret;
	}
	inline poly poly_ln(poly a,const int&k){
		poly ret=a.poly_direv(a);
		return ret=(ret*a.poly_inv(a,k)).extend(k),ret.poly_inter(ret);
	}
};
int main(){
	n=read(),Inv.resize(n*2+1),fac.resize(n*2+1),ifac.resize(n*2+1),Inv[1]=1,fac[0]=fac[1]=ifac[0]=1;
	for(ri i=2;i<=n*2;++i)Inv[i]=mul(Inv[mod%i],mod-mod/i),fac[i]=mul(fac[i-1],i);
	for(ri i=1;i<=n*2;++i)ifac[i]=mul(Inv[i],ifac[i-1]);
	poly a(n);
	a[0]=a[1]=1;
	for(ri i=2;i<=n;++i)a[i]=mul(ksm(2,(ll)i*(i-1)/2),ifac[i]);
	int len=1;
	while(len<=n)len<<=1;
	a=a.poly_ln(a,len);
	cout<<mul(a[n],fac[n]);
	exit(0);
	return 0;
}
上一篇:【nodejs】--express的中间件multer实现图片文件上传--【XUEBIG】


下一篇:React笔记:组件(3)