快速数论变换 NTT

FFT

有道云笔记

阶的定义

a m ,且 m > 1 ,对于 a x 1 ( m o d   m ) 最小的 x ,我们称之为 a m 的阶,记做 δ m ( a )

原根定义

δ m ( a ) = φ ( m ) ,则称 a 为模 m 的一个原根。

NTT

摘自Menci的博客

考虑一个质数 p = q n + 1 (其中 n 2 的幂)。定义其原根 g 为使得 g i ( 0 i p 1 ) 互不相同的数。

【模板】多项式乘法

精度比FFT高

#include <cstdio>

typedef long long LL;
const int N = 2097152+5, P = 998244353, G = 3, Gi = 332748118; //G是P的原根,Gi是G的逆元

LL a[N], b[N], R[N], n, m, L;

int read() {
    int x = 0; char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c ^ 48);
        c = getchar();
    }
    return x;
}
void swap(LL &x, LL &y) {
    x ^= y, y ^= x, x ^= y;
}
LL fast_pow(LL a, int p) {
    LL res = 1;
    for (; p; p >>= 1, a = a * a % P)
        if (p & 1) res = res * a % P;
    return res;
}
void ntt(LL *A, int f) {
    for (int i = 0; i < n; ++i) if (i < R[i]) swap(A[i], A[R[i]]);
    for (int i = 1; i < n; i <<= 1) {
        LL wn = fast_pow(f == 1 ? G : Gi, (P - 1) / (i << 1));
        for (int j = 0, r = i << 1; j < n; j += r) {
            LL w = 1;
            for (int k = 0; k < i; ++k, w = w * wn % P) {
                int x = A[j+k], y = w * A[i+j+k] % P;
                A[j+k] = (x + y) % P, A[i+j+k] = (x - y + P) % P;
            }
        }
    }
}

int main() {
    n = read(), m = read();
    for (int i = 0; i <= n; ++i) a[i] = read();
    for (int i = 0; i <= m; ++i) b[i] = read();
    m += n; for (n = 1; n <= m; n <<= 1) ++L;
    for (int i = 0; i < n; ++i) R[i] = (R[i>>1] >> 1) | ((i & 1) << (L - 1));
    ntt(a, 1); ntt(b, 1);
    for (int i = 0; i < n; ++i) a[i] = (a[i] * b[i]) % P;
    ntt(a, -1);
    LL inv = fast_pow(n, P - 2);
    for (int i = 0; i <= m; ++i) printf("%lld ", a[i] * inv % P);
    return 0;
}

任意模数NTT

FFT用到的各种素数

题意:两个多项式做乘法, n 10 5 , a i 10 9

我们发现这样的最大值是 10 9 2 × 10 5 = 10 23

那么可以选三个满足NTT性质且乘起来 > 10 23 的模数分别NTT,最后中国剩余定理合并。

e x c r t

x 为卷积的结果,有

{ x c 1 ( mod m 1 ) x c 2 ( mod m 2 ) x c 3 ( mod m 3 )

先用 e x c r t 合并前两个,

【模板】任意模数NTT

猜你喜欢

转载自blog.csdn.net/Milkyyyyy/article/details/81913859