洛谷 P3711 倉鼠的數學題 [伯努利數 fft]

Candy?發表於2017-04-28

P3711 倉鼠的數學題

題意:
\[ S_m(x) = \sum_{k=0}^x k^m, 0^0=1\quad 求 \sum_{m=0}^n S_m(x)a_m \]
的答案多項式\(\sum_{i=0}^{n+1}c_ix^i\)各項係數


一開始用了\(B^-\),然後後面要展開\((x+1)^k\),完全不會做

和出題人fjzzq2002討論了一下,原來標程用的是\(B^+\),不需要展開了

那就很簡單了...不想寫過程了,最後的結果就是
\[ C_t = \frac{1}{t!} \sum_{m=0}^{n+t} f_m h_{m+1-t} \\ f(x) = \sum_{i=0}^n a_i i!,\ g(x) = \sum_{i=0}^n \frac{B_i^+}{i!},\ h_i = g_{n+1-i} \]

然後我發現用\(B^+\)好像沒有常數項啊?但是用\(B^-\)寫暴力計算確實有常數項

然後發現常數項就是\(a_0\),交上就過了...

update:去問了張隊長,然後意識到,\(B^+\)算的和是從1開始...然後常數項就是\(a_0 0^0 = a_0\)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = (1<<20) + 5, P = 998244353, mo = P, inv2 = (P+1)/2;
const double PI = acos(-1.0);
inline int read(){
    char c=getchar(); int x=0,f=1;
    while(c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
    return x*f;
}

inline int Pow(ll a, int b) {
    ll ans = 1;
    for(; b; b>>=1, a=a*a%P)
        if(b&1) ans=ans*a%P;
    return ans;
}
namespace fft {
    int rev[N], g = 3;
    void dft(int *a, int n, int flag) {
        int k = 0; while((1<<k) < n) k++;
        for(int i=0; i<n; i++) {
            rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1));
            if(i < rev[i]) swap(a[i], a[rev[i]]);
        }
        for(int l=2; l<=n; l<<=1) {
            int m = l>>1, wn = Pow(g, flag==1 ? (P-1)/l : P-1-(P-1)/l);
            for(int *p = a; p != a+n; p += l)
                for(int k=0, w=1; k<m; k++, w = (ll)w*wn %P) {
                    int t = (ll) p[k+m] * w %P;
                    p[k+m] = (p[k] - t + P) %P;
                    p[k] = (p[k] + t) %P;
                }
        }
        if(flag == -1) {
            ll inv = Pow(n, P-2);
            for(int i=0; i<n; i++) a[i] = a[i] * inv %P;
        }
    }
    
    int t[N];
    void inverse(int *a, int *b, int l) {
        if(l == 1) {b[0] = Pow(a[0], P-2); return;}
        inverse(a, b, l>>1);
        int n = l<<1;
        for(int i=0; i<l; i++) t[i] = a[i], t[i+l] = 0;
        dft(t, n, 1); dft(b, n, 1);
        for(int i=0; i<n; i++) b[i] = (ll) b[i] * (2 - (ll) b[i] * t[i] %P + P) %P;
        dft(b, n, -1); for(int i=l; i<n; i++) b[i] = 0;
    }

    void mul(int *a, int *b, int n) {
        dft(a, n, 1); dft(b, n, 1);
        for(int i=0; i<n; i++) a[i] = (ll) a[i] * b[i] %P;
        dft(a, n, -1);
    }
}

int n, a[N], len;
ll inv[N], fac[N], facInv[N];
int q[N], b[N], f[N], g[N];
int main() {
    freopen("in", "r", stdin);
    n=read();
    for(int i=0; i<=n; i++) a[i] = read();

    inv[1] = fac[0] = facInv[0] = 1;
    for(int i=1; i<=n+2; i++) {
        if(i != 1) inv[i] = (P - P/i) * inv[P%i] %P;
        fac[i] = fac[i-1] * i %P;
        facInv[i] = facInv[i-1] * inv[i] %P;
    }

    len = 1; while(len <= n+1) len <<= 1;
    for(int i=0; i<=n+1; i++) q[i] = facInv[i+1];
    fft::inverse(q, b, len); 
    for(int i=0; i<=n+1; i++) b[i] = b[i] * fac[i] %P;
    b[1] = inv2;


    for(int i=0; i<=n; i++) f[i] = (ll) a[i] * fac[i] %P;
    for(int i=0; i<=n; i++) g[n+1-i] = (ll) b[i] * facInv[i] %P;
    while(len <= n+n+1) len <<= 1;
    fft::mul(f, g, len);
    printf("%d ", a[0]);
    for(int i=1; i<=n+1; i++) printf("%lld ", f[n+i] * facInv[i] %P);
}

相關文章