【洛谷 P5408】【模板】—第一类斯特林数·行(倍增+NTT)

25 篇文章 0 订阅
5 篇文章 0 订阅

传送门

斯特林数学习笔记


考虑构造生成函数 F n = ∑ i = 0 ∞ s ( n , i ) x i F_n=\sum_{i=0}^{\infty}s(n,i)x^i Fn=i=0s(n,i)xi

由第一类斯特林数的递推式可得

F n = x F n − 1 + ( n − 1 ) F ( n − 1 ) = ( x + n − 1 ) F ( n − 1 ) = x n ‾ F_n=xF_{n-1}+(n-1)F(n-1)=(x+n-1)F(n-1)=x^{\overline n} Fn=xFn1+(n1)F(n1)=(x+n1)F(n1)=xn

考虑倍增

如果 n n n为奇数,递归求解 n − 1 n-1 n1,乘一个 x + n − 1 x+n-1 x+n1

如果 n n n为偶数
x 2 n ‾ = x n ‾ ( x + n ) n ‾ x^{\overline {2n}}=x^{\overline n}(x+n)^{\overline n} x2n=xn(x+n)n
假设已经求出了 x n ‾ = f 1 ( x ) = ∑ i = 0 n a i x i x^{\overline n}=f1(x)=\sum_{i=0}^{n}a_ix^i xn=f1(x)=i=0naixi
考虑如何求出
f 2 ( x ) = ∑ i = 0 n a i ( x + n ) i f2(x)=\sum_{i=0}^n a_i(x+n)^i f2(x)=i=0nai(x+n)i

f 2 ( x ) = ∑ i = 0 n a i ∑ j = 0 i x j n i − j ( i j ) f2(x)=\sum_{i=0}^{n}a_i\sum_{j=0}^{i}x^jn^{i-j}{i\choose j} f2(x)=i=0naij=0ixjnij(ji)

            = ∑ j = 0 n x j j ! ∑ i = j n a i i ! n i − j ( i − j ) ! \ \ \ \ \ \ \ \ \ \ \ =\sum_{j=0}^{n}\frac{x^j}{j!}\sum_{i=j}^na_ii!\frac{n^{i-j}}{(i-j)!}            =j=0nj!xji=jnaii!(ij)!nij

            = ∑ j = 0 n x j j ! ∑ i = 0 n − j a i + j ( i + j ) ! n i i ! \ \ \ \ \ \ \ \ \ \ \ =\sum_{j=0}^{n}\frac{x^j}{j!}\sum_{i=0}^{n-j}a_{i+j}(i+j)!\frac{n^{i}}{i!}            =j=0nj!xji=0njai+j(i+j)!i!ni

A ( x ) = ∑ i = 0 n a i i ! x i , B ( x ) = ∑ i = 0 n n n − i ( n − i ) ! x i A(x)=\sum_{i=0}^n a_ii!x^i,B(x)=\sum_{i=0}^n\frac{n^{n-i}}{(n-i)!}x^i A(x)=i=0naii!xi,B(x)=i=0n(ni)!nnixi

( A ∗ B ) ( x ) (A*B)(x) (AB)(x)乘出来左移 n n n位即可

复杂度 T ( n ) = T ( n 2 ) + O ( n l o g n ) = O ( n l o g n ) T(n)=T(\frac n 2)+O(nlogn)=O(nlogn) T(n)=T(2n)+O(nlogn)=O(nlogn)

#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,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 calc_s(int n){
	poly res;
	if(n==1){res.pb(0),res.pb(1);return res;}
	if(n&1){
		res=calc_s(n-1);
		res.resize(n+1);
		for(int i=n;i;i--)res[i]=add(res[i-1],mul(res[i],n-1));
		return res;
	}
	int mid=n>>1;
	poly a=calc_s(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,p=1;i<=mid;i++,Mul(p,mid))b[i]=mul(p,ifac[i]);
	reverse(b.bg(),b.bg()+mid+1);
	c=c*b;for(int i=0;i<=mid;i++)c[i]=mul(c[i+mid],ifac[i]);
	c.resize(mid+1),res=a*c;return res;
}
int n;
int main(){
	n=read();
	init_w(),init();
	poly ans=calc_s(n);
	for(int i=0;i<=n;i++)cout<<ans[i]<<" ";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值