【BZOJ4555】【TJOI2016】【HEOI2016】—求和(第二类斯特林数+NTT)

传送门

题意:求i=0nj=0iS2(i,j)j!2j\sum_{i=0}^n\sum_{j=0}^{i}S_2(i,j)j!2^j

由于S2(i,j)i<jS_2(i,j),i<jS2(i,j)=0S_2(i,j)=0

ans=i=0nj=0nS2(i,j)j!2jans=\sum_{i=0}^{n}\sum_{j=0}^{n}S_2(i,j)j!2^j
由于S2(i,j)=k=0j(1)jk(jk)kij!S_2(i,j)=\frac{\sum_{k=0}^{j}(-1)^{j-k}{j\choose k}k^i}{j!}
ans=i=0nj=0nk=0j(1)jk(jk)ki2jans=\sum_{i=0}^n\sum_{j=0}^n{\sum_{k=0}^{j}(-1)^{j-k}{j\choose k}k^i}2^j

=i=0nj=0nk=0j(1)jk(jk)!×kik!×2jj!=\sum_{i=0}^{n}\sum_{j=0}^{n}\sum_{k=0}^{j}\frac{(-1)^{j-k}}{(j-k)!}\times\frac{k^i}{k!}\times 2^jj!

=j=0n2jj!k=0j(1)jk(jk)!×i=0nkik!=\sum_{j=0}^{n}2^jj!\sum_{k=0}^{j}\frac{(-1)^{j-k}}{(j-k)!}\times\frac{\sum_{i=0}^{n}k^i}{k!}

=j=0n2jj!k=0j(1)jk(jk)!×kn+11k!(k1)=\sum_{j=0}^{n}2^jj!\sum_{k=0}^{j}\frac{(-1)^{j-k}}{(j-k)!}\times\frac{k^{n+1}-1}{k!(k-1)}

f(x)=(1)xx!,g(x)=xn+11x!(x1)f(x)=\frac{(-1)^x}{x!},g(x)=\frac{x^{n+1}-1}{x!*(x-1)}
注意特判ggx=0x=0x=1x=1的情况

卷积一下就完了

#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
const 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?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 int Inv(int x){
	return ksm(x,mod-2);
}
const int N=100005;
int ifac[N],fac[N],n;
int rev[N<<2];
#define poly vector<int>
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;mid<lim;mid<<=1){
        int now=ksm(G,(mod-1)/(mid<<1));
        for(int i=0;i<lim;i+=(mid<<1)){
            int w=1;
            for(int j=0;j<mid;j++,w=mul(w,now)){
                int a0=f[i+j],a1=mul(w,f[i+j+mid]);
                f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
            }
        }
    }
    if(kd==-1&&(reverse(f.begin()+1,f.begin()+lim),1))for(int i=0,inv=ksm(lim,mod-2);i<lim;i++)f[i]=mul(f[i],inv);
}

inline void init_rev(int lim){
	for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
inline poly mul(poly a,poly b){
	int deg=a.size()+b.size()-1,lim=1;
	while(lim<deg)lim<<=1;
	init_rev(lim);
	a.resize(lim),b.resize(lim);
	ntt(a,lim,1),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;
}
poly f,g;
inline void init(){
	fac[0]=ifac[0]=1;
	for(int i=1;i<=n;i++)fac[i]=mul(fac[i-1],i);
	ifac[n]=Inv(fac[n]);
	for(int i=n-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
int main(){
	n=read();
	init();
	for(int i=0;i<=n;i++)
	f.pb((i&1)?mul(mod-1,ifac[i]):ifac[i]);
	g.pb(1),g.pb(n+1);
	for(int i=2;i<=n;i++)
	g.pb(mul(dec(ksm(i,n+1),1),Inv(mul(i-1,fac[i]))));
	poly t=mul(f,g);
	int res=0;
	for(int i=0,bin=1;i<=n;i++,Mul(bin,2))
		Add(res,mul(mul(bin,fac[i]),t[i]));
	cout<<res;
}
posted @ 2019-07-30 21:12  Stargazer_cykoi  阅读(136)  评论(0编辑  收藏  举报