[洛谷P4721]分治FFT

NTT入门,放个板子

// luogu-judger-enable-o2
#include 
using namespace std;
#define fr(i, a, b) for ( int i = a; i <= b; ++ i)
#define mid ( l + r >> 1)
typedef long long ll;
const ll P = 998244353, mod = 998244353, Ge = 3;
template <class T> void G(T &x) {
    x = 0; char o = getchar(); int f = 1; for ( ; !isdigit(o); o = getchar()) if( o == '-') f = -1;
    for ( ; isdigit(o); o = getchar()) x = (x << 1) + (x << 3) + (o & 15); x *= f;
}
ll n, g[400010], f[400010], a[400010], b[400010], rev[400010];
ll w[2][400010];
ll _pow(ll x, ll n) { ll ans = 1; for ( ; n; n>>=1, x = x * x % P) if( n&1) ans = ans * x % P; return ans;}
inline void NTT(ll *a, int n, int f) {
    for ( int i = 0; i < n; ++ i) if( i < rev[i]) swap(a[i], a[rev[i]]);
    w[0][0] = w[1][0] = 1;
    for (int i = 1; i < n; i++) {
        w[0][i] = w[0][i - 1] * _pow(Ge, (P - 1) / n) % P;
        w[1][i] = _pow(w[0][i], P - 2);
    }
 
    for (int i = 1; i < n; i <<= 1)
    for (int j = 0, l = n / (i << 1); j < n; j += (i << 1))
    for (int k = 0, t = 0; k < i; k += 1, t += l) {
        ll x = a[j + k], y = w[f][t] * a[i + j + k] % P;
        a[j + k] = (x + y) % P, a[i + j + k] = (x - y + P) % P;
    }
 
    for (int i = 0; f && i < n; i++)
        a[i] = a[i] * _pow(n, P - 2) % P;
}
inline void cdq(int l, int r) {
    if( l == r) return ;
    cdq(l, mid); 
    int Len = 1, L = 0;
    for ( ; Len < (r - l + 1) << 1; Len <<= 1) ++ L;
    fr(i, 1, Len) rev[i] = (rev[i >> 1] >> 1)|((i&1) << L-1);
    fr(i, 0, Len) a[i] = b[i] = 0;
    fr(i, l, mid) a[i-l] = f[i];
    fr(i, 0, r-l) b[i] = g[i];
    NTT(a, Len, 0); NTT(b, Len, 0);
    fr(i, 0, Len) a[i] = a[i] * b[i] % P;
    NTT(a, Len, 1);
    fr(i, mid+1, r) f[i] = (f[i] + a[i-l]) % mod;
    cdq(mid+1, r);
}

int main() {
    G(n); fr(i, 1, n-1) G(g[i]);
    f[0] = 1; cdq(0, n-1);
    fr(i, 0, n-1) printf("%lld ", f[i]);
}
View Code

 

转载于:https://www.cnblogs.com/miecoku/p/9757357.html

你可能感兴趣的:([洛谷P4721]分治FFT)