【LOJ # 6268】—分拆数(生成函数+多项式Ln/Exp+NTT)

传送门

我开始的思路是构建f(x)=x+x2+x3....f(x)=x+x^2+x^3....
Ans(x)=11f(x)Ans(x)=\frac{1}{1-f(x)}

但是这样考虑的问题是不同顺序会算重

考虑对每个数字构建生成函数fi(x)=j=1xijf_i(x)=\sum_{j=1}^{\infty}x^{ij}

那么Ans(x)=i=1j=1xijAns(x)=\prod_{i=1}^{\infty}\sum_{j=1}^{\infty}x^{ij}
考虑取对数
Ln(Ans(x))=i=1Ln(fi(x))Ln(Ans(x))=\sum_{i=1}^{\infty}Ln(f_i(x))

gi(x)=Ln(fi(x))g_i(x)=Ln(f_i(x))
那么gi(x)=fi(x)fi(x)=j=1ijxij111xi=j=1ixij1g_i'(x)=\frac{f_i'(x)}{f_i(x)}=\frac{\sum_{j=1}^{\infty}ijx^{ij-1}}{\frac{1}{1-x^i}}=\sum_{j=1}^{\infty}ix^{ij-1}

gi(x)=j=1xijjg_i(x)=\sum_{j=1}^{\infty}\frac{x^{ij}}{j}

Ln(Ans(x))=i=1j=1xijjLn(Ans(x))=\sum_{i=1}^{\infty}\sum_{j=1}^{\infty}\frac{x^{ij}}{j}

ExpExp即可

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
#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 ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define cs const
#define bg begin
#define poly vector<int>  
cs int mod=998244353,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;}
inline void Mul(int &a,int b){a=1ll*a*b%mod;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int C=19;
poly w[C+1];
inline void init_w(){
	for(int i=1;i<=C;i++)w[i].resize(1<<(i-1));
	int wn=ksm(G,(mod-1)/(1<<C));
	w[C][0]=1;
	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];
}
int rev[(1<<C)|5],inv[(1<<C)|5];
inline void init_inv(){
	inv[0]=inv[1]=1;
	for(int i=2;i<=(1<<C);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 mid=1,l=1,a0,a1;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(f[i+j+mid],w[l][j]),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,poly b){
	int deg=a.size()+b.size()-1,lim=1;
	if(deg<=32){
		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),ntt(a,lim,1);
	b.resize(lim),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 operator -(poly a,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 Inv(poly a,int deg){
	poly b(1,Inv(a[0])),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 deriv(poly a){
	for(int i=0;i<(int)a.size()-1;i++)a[i]=mul(a[i+1],i+1);
	a.pop_back();return a;
}
inline poly integ(poly a){
	a.pb(0);
	for(int i=(int)a.size()-1;i;i--)a[i]=mul(a[i-1],inv[i]);
	a[0]=0;return a;
}
inline poly Ln(poly a,int deg){
	a=integ(deriv(a)*Inv(a,deg)),a.resize(deg);return a;
}
inline poly Exp(poly a,int deg){
	poly b(1,1),c;int n=a.size();
	for(int lim=2;lim<(deg<<1);lim<<=1){
		c=Ln(b,lim);
		for(int i=0;i<lim;i++)c[i]=dec(i<n?a[i]:0,c[i]);
		Add(c[0],1),b=b*c;
		b.resize(lim);
	}b.resize(deg);return b;
}

poly f;
int n;
int main(){
	n=read(),init_w(),init_inv();
	f.resize(n+1);
	for(int i=1;i<=n;i++)
	for(int j=1;i*j<=n;j++)
	Add(f[i*j],inv[j]);
	f=Exp(f,n+1);
	for(int i=1;i<=n;i++)cout<<f[i]<<'\n';
}
posted @ 2019-09-29 21:58  Stargazer_cykoi  阅读(119)  评论(0编辑  收藏  举报