【BZOJ4555】【TJOI2016】【HEOI2016】求和 第二类斯特林数 NTT
题目大意
求\(f(n)=\sum_{i=0}^n\sum_{j=0}^i2^j\times j!\times S(i,j)\\\)
对\(998244353\)取模
\(n\leq 100000\)。
题解
\[\begin{align}
S(n,k)&=\frac1{k!}\sum_{i=0}^k{(-1)}^i\binom{k}{i}{(k-i)}^n\\
&=\frac1{k!}\sum_{i=0}^k{(-1)}^i\frac{k!}{i!(k-i)!}(k-i)^n\\
&=\sum_{i=0}^k\frac{{(-1)}^i}{i!}\frac{{(k-i)}^n}{(k-i)!}
\end{align}
\]
因为\(S(i,j)=0~(i<j)\),所以
\[\begin{align}
f(n)&=\sum_{i=0}^n\sum_{j=0}^n2^j\times j!\times S(i,j)\\
&=\sum_{j=0}^n2^j\times j!\times\sum_{i=0}^nS(i,j)\\
&=\sum_{j=0}^n2^j\times j!\times\sum_{i=0}^n\sum_{l=0}^j\frac{{(-1)}^i}{l!}\frac{{(j-l)}^i}{(j-l)!}\\
&=\sum_{j=0}^n2^j\times j!\times\sum_{l=0}^j\frac{{(-1)}^i}{l!}\sum_{i=0}^n\frac{{(j-l)}^i}{(j-l)!}
\end{align}
\]
设
\[A(x)=\frac{{(-1)}^i}{i!},B(x)=\sum_{i=0}^n\frac{x^i}{x!}
\]
所以
\[B(x)=\frac{x^{n+1}-1}{x!(x-1)}
\]
\[f(n)=\sum_{j=0}^n2^j\times j!\times \sum_{i=0}^jA(i)B(j-i)
\]
直接上NTT
时间复杂度:\(O(n\log n)\)
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
ll p=998244353;
ll fp(ll a,ll b)
{
ll s=1;
while(b)
{
if(b&1)
s=s*a%p;
a=a*a%p;
b>>=1;
}
return s;
}
namespace ntt
{
ll w1[1000010];
ll w2[1000010];
int rev[1000010];
int n;
void init()
{
n=262144;
int i;
for(i=2;i<=n;i<<=1)
{
w1[i]=fp(3,(p-1)/i);
w2[i]=fp(w1[i],p-2);
}
rev[0]=0;
for(i=1;i<n;i++)
rev[i]=(rev[i>>1]>>1)|(i&1?n>>1:0);
}
void ntt(ll *a,int t)
{
int i,j,k;
ll w,wn,u,v;
for(i=0;i<n;i++)
if(rev[i]<i)
swap(a[i],a[rev[i]]);
for(i=2;i<=n;i<<=1)
{
wn=(t==1?w1[i]:w2[i]);
for(j=0;j<n;j+=i)
{
w=1;
for(k=j;k<j+i/2;k++)
{
u=a[k];
v=a[k+i/2]*w%p;
a[k]=(u+v)%p;
a[k+i/2]=(u-v+p)%p;
w=w*wn%p;
}
}
}
if(t==-1)
{
ll inv=fp(n,p-2);
for(i=0;i<n;i++)
a[i]=a[i]*inv%p;
}
}
};
ll a[500010];
ll b[500010];
ll fac[200010];
int main()
{
ntt::init();
int n;
scanf("%d",&n);
int i;
fac[0]=1;
for(i=1;i<=n;i++)
fac[i]=fac[i-1]*i%p;
a[0]=1;
for(i=1;i<=n;i++)
a[i]=(((i&1?-1:1)*fp(fac[i],p-2))%p+p)%p;
b[0]=1;
b[1]=n+1;
for(i=2;i<=n;i++)
b[i]=((fp(i,n+1)-1)*fp(fac[i]*(i-1)%p,p-2)%p+p)%p;
ntt::ntt(a,1);
ntt::ntt(b,1);
for(i=0;i<ntt::n;i++)
a[i]=a[i]*b[i]%p;
ntt::ntt(a,-1);
ll ans=0;
for(i=0;i<=n;i++)
ans=(ans+fp(2,i)*fac[i]%p*a[i]%p)%p;
printf("%lld\n",ans);
return 0;
}