BZOJ4555 HEOI2016/TJOI2016求和(NTT+斯特林数)

  S(i,j)=Σ(-1)j-k(1/j!)·C(j,k)·ki=Σ(-1)j-k·ki/k!/(j-k)!。原式=ΣΣ(-1)j-k·ki·2j·j!/k!/(j-k)! (i,j=0~n)。可以发现i只在式中出现了一次且与j不相关,如果对每个k求出其剩余部分的答案,各自乘一下即可。而剩余部分显然是一个卷积。

#include<bits/stdc++.h>
using namespace std;
int getbit(){char c=getchar();while (c<'0'||c>'9') c=getchar();return c^48;}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f; 
}
#define N 100010
#define P 998244353
#define inv3 332748118
int n,r[N<<2],fac[N],inv[N],f[N<<2],g[N<<2];
int ksm(int a,int k)
{
    int s=1;
    for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P;
    return s;
}
int C(int n,int m){if (m>n) return 0;return 1ll*fac[n]*inv[m]%P*inv[n-m]%P;}
void DFT(int *a,int n,int g)
{
    for (int i=0;i<n;i++) if (i<r[i]) swap(a[i],a[r[i]]);
    for (int i=2;i<=n;i<<=1)
    {
        int wn=ksm(g,(P-1)/i);
        for (int j=0;j<n;j+=i)
        {
            int w=1;
            for (int k=j;k<j+(i>>1);k++,w=1ll*w*wn%P)
            {
                int x=a[k],y=1ll*w*a[k+(i>>1)]%P;
                a[k]=(x+y)%P,a[k+(i>>1)]=(x-y+P)%P;
            }
        }
    }
}
void mul(int *f,int *g)
{
    int t=1;while (t<=(n<<1)) t<<=1;
    for (int i=0;i<t;i++) r[i]=(r[i>>1]>>1)|(i&1)*(t>>1);
    DFT(f,t,3),DFT(g,t,3);
    for (int i=0;i<t;i++) f[i]=1ll*f[i]*g[i]%P;
    DFT(f,t,inv3);
    int u=ksm(t,P-2);
    for (int i=0;i<t;i++) f[i]=1ll*f[i]*u%P;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("b.in","r",stdin);
    freopen("b.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read();
    fac[0]=1;for (int i=1;i<=n;i++) fac[i]=1ll*fac[i-1]*i%P;
    inv[0]=inv[1]=1;for (int i=2;i<=n;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
    for (int i=2;i<=n;i++) inv[i]=1ll*inv[i]*inv[i-1]%P;
    for (int i=0;i<=n;i++) g[i]=1ll*ksm(2,i)*fac[i]%P;reverse(g,g+n+1);
    for (int i=0;i<=n;i++) if (i&1) f[i]=P-inv[i];else f[i]=inv[i];
    mul(f,g);
    reverse(f,f+n+1);
    for (int i=0;i<=n;i++) f[i]=1ll*f[i]*inv[i]%P;
    f[1]=1ll*f[1]*(n+1)%P;
    for (int k=2;k<=n;k++)
    f[k]=1ll*f[k]*(ksm(k,n+1)-1)%P*ksm(k-1,P-2)%P;
    int ans=0;for (int k=0;k<=n;k++) ans=(ans+f[k])%P;
    cout<<ans;
    return 0;
}

 

posted @ 2019-01-15 16:00  Gloid  阅读(116)  评论(0编辑  收藏  举报