分治 FFT學習筆記

先給一道luogu板子題:P4721 【模板】分治 FFT


今天模擬有道題的部分分作法是分治fft,因而就學了一下。感受不是很難,國賽上若是推出式子的話應該能寫出來。


分治fft用來解決這麼一個式子\[f(i) = \sum _ {j = 1} ^ {i} f(j) * g(i - j)\]
若是暴力fft的話,複雜度\(O(n ^ 2logn)\)尚未暴力優秀。


咱們能夠用cdq分治的思想,對於區間\([L, R]\),假設\([L, mid]\)已經求出,如今要算\([mid + 1, R]\)
那麼咱們考慮對於\([mid + 1, R]\)中的某一項\(x\),前面\([L, mid]\)對他的貢獻:\[f(x) = \sum _{i = L} ^ {mid} f(i) * g(x - i)\]
爲了方便,咱們把循環到\(mid\)改成\(x - 1\),反正\(mid\)~\(x - 1\)這些項尚未被計算,暫且爲0。因而有
\[\begin{align*} f(x) &= \sum _ {i = L} ^ {x - 1} f(i)* g(x - i) \\ &= \sum _ {i = 0} ^ {x - L - 1} f(i + L) * g(x - L - i) \end{align*}\]
咱們令\(A(i) = f(i +L), B(i) = g(i + 1)\),因而式子就變成了\[f(x) = C(x - L - 1) = \sum _ {i = 0} ^{x - L - 1} A(i) *B(x - L - 1 - i)\]
這個時候能看出後面是一個卷積的形式,直接FFT便可。而後把\(C(x - L - 1)\)加到\(f(x)\)上,左邊對右邊的貢獻就算完了。再遞歸右邊。


遞歸的每一層是\(O(len log len)\)的(區間長度),有\(logn\)層,所以總時間複雜度爲\(O(nlog ^ 2n)\)ios

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cctype>
#include<map>
#include<queue>
#include<vector>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 6e5 + 5;
const ll G = 3;
const ll mod = 998244353;
In ll read()
{
    ll ans = 0;
    char ch = getchar(), las = ' ';
    while(!isdigit(ch)) las = ch, ch = getchar();
    while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    if(las == '-') ans = -ans;
    return ans;
}
In void write(ll x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
    freopen("ha.in", "r", stdin);
    freopen("ha.out", "w", stdout);
#endif
}

int n;
ll g[maxn], f[maxn];

In ll inc(ll a, ll b) {return a + b < mod ? a + b : a + b - mod;}
In ll quickpow(ll a, ll b)
{
    ll ret = 1;
    for(; b; b >>= 1, a = a * a % mod)
        if(b & 1) ret = ret * a % mod;
    return ret;
}

int rev[maxn];
ll A[maxn], B[maxn];
In void ntt(ll* a, int len, bool flg)
{
    for(int i = 0; i < len; ++i) if(i < rev[i]) swap(a[i], a[rev[i]]);
    for(int i = 1; i < len; i <<= 1)
    {
        ll gn = quickpow(G, (mod - 1) / (i << 1));
        for(int j = 0; j < len; j += (i << 1))
        {
            ll g = 1;
            for(int k = 0; k < i; ++k, g = g * gn % mod)
            {
                ll tp1 = a[j + k], tp2 = g * a[j + i + k] % mod;
                a[j + k] = inc(tp1, tp2), a[j + i + k] = inc(tp1, mod - tp2);
            }
        }
    }
    if(flg) return;
    reverse(a + 1, a + len); ll inv = quickpow(len, mod - 2);
    for(int i = 0; i < len; ++i) a[i] = a[i] * inv % mod;
}
In void NTT(ll* a, ll* b, int len, int lim)
{
    for(int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (lim - 1));
    ntt(a, len, 1), ntt(b, len, 1);
    for(int i = 0; i < len; ++i) a[i] = a[i] * b[i] % mod;
    ntt(a, len, 0);
}
In void fftSolve(int L, int R)
{
    if(L == R) return;
    int mid = (L + R) >> 1;
    fftSolve(L, mid);
    int n = R - L - 1, len = 1, lim = 0;
    while(len <= n + n) len <<= 1, ++lim;
    fill(A, A + len, 0), fill(B, B + len, 0);
    for(int i = L; i <= mid; ++i) A[i - L] = f[i];
    for(int i = 0; i <= n; ++i) B[i] = g[i + 1];
    NTT(A, B, len, lim);
    for(int i = mid + 1; i <= R; ++i) f[i] = inc(f[i], A[i - L - 1]);
    fftSolve(mid + 1, R);
}

int main()
{
//  MYFILE();
    n = read();
    for(int i = 1; i < n; ++i) g[i] = read();
    f[0] = 1, fftSolve(0, n - 1);  //記得賦初值
    for(int i = 0; i < n; ++i) write(f[i]), space; enter;
    return 0;
}
相關文章
相關標籤/搜索