【洛谷 P5396】【模板】—第二类斯特林数·列(生成函数+倍增+NTT)

传送门

斯特林数学习笔记


考虑构建列的生成函数
Fn=i=0S(i,n)xiF_n=\sum_{i=0}^{\infty}S(i,n)x^i

由第二类斯特林数递推式
S(i,j)=S(i1,j1)+jS(i1,j)S(i,j)=S(i-1,j-1)+j*S(i-1,j)

Fn=xFn1+xjFnF_n=xF_{n-1}+x*j*F_{n}
就是Fn=x1xjFn1F_n=\frac{x}{1-x*j}F_{n-1}

Fn=xni=1n(1ix)F_n=\frac{x^n}{\prod_{i=1}^{n}(1-ix)}

g(x)=i=1n(1ix)g(x)=\prod_{i=1}^n(1-ix)

这是一个nn次多项式

gR(x)=xni=1n(1ix)=i=1n(xi)=(x1)n=xn+1xg^R(x)=x^n\prod_{i=1}^n(1-\frac i x)=\prod_{i=1}^{n}(x-i)=(x-1)^{\underline n}=\frac{x^{\underline {n+1}}}{x}

xnx^{\underline n}这样一个东西可以倍增O(nlogn)O(nlogn)
然后翻转回来求个逆就完了

不过翻转操作好像不能直接对FnF_n做,好像是因为模意义下求逆之后并不清楚FF是个几次多项式(雾)

#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=167772161,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?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=(1<<20)|5,C=20;
poly w[C+1];
int rev[N],fac[N],ifac[N],inv[N];
inline void init(cs int len=N-5){
	fac[0]=ifac[0]=inv[0]=inv[1]=1;
	for(int i=1;i<=len;i++)fac[i]=mul(fac[i-1],i);
	ifac[len]=ksm(fac[len],mod-2);
	for(int i=len-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
	for(int i=2;i<=len;i++)inv[i]=mul(mod-mod/i,inv[mod%i]);
}
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];
}
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,l=1,mid=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,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<=64){
		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 Inv(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);
		c.resize(lim),ntt(c,lim,1);
		b.resize(lim),ntt(b,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 calc_down(int n){
	poly res;
	if(n==1){res.pb(0),res.pb(1);return res;}
	if(n&1){
		res=calc_down(n-1);
		res.pb(0);
		for(int i=res.size()-1;i;i--)res[i]=add(mul(res[i],dec(1,n)),res[i-1]);
		res[0]=0;return res;
	}
	int mid=n>>1;
	poly a=calc_down(mid),b(mid+1),c(mid+1);
	for(int i=0;i<=mid;i++)c[i]=mul(a[i],fac[i]);
	for(int i=0,t=dec(0,mid),mt=1;i<=mid;i++,Mul(mt,t))b[i]=mul(mt,ifac[i]);
	reverse(b.bg(),b.bg()+mid+1);
	b=b*c;for(int i=0;i<=mid;i++)b[i]=mul(b[i+mid],ifac[i]);
	b.resize(mid+1),res=a*b;return res;
}
poly f,g;
int n,k;
int main(){
	init_w(),init();
	n=read(),k=read();
	f=calc_down(k+1);
	for(int i=0;i<f.size()-1;i++)f[i]=f[i+1];f.pop_back();
	reverse(f.bg(),f.end());
	f=Inv(f,n+1);
	f.resize(n+1);
	for(int i=n;i>=k;i--)f[i]=f[i-k];
	for(int i=0;i<k;i++)f[i]=0;
	for(int i=0;i<=n;i++)cout<<f[i]<<" ";
}
posted @ 2019-08-16 10:17  Stargazer_cykoi  阅读(120)  评论(0编辑  收藏  举报