【BZOJ4555】【TJOI2016】【HEOI2016】—求和(第二类斯特林数+NTT)
题意:求
由于时
由于
令
注意特判的和的情况
卷积一下就完了
#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;
}