NTT

Posted wxyww

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NTT相关的知识,希望对你有一定的参考价值。

存一发模板233

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<ctime>
using namespace std;
typedef long long ll;
const int N = 4000100,mod = 998244353;
ll read() {
    ll 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*10+c-'0';
        c=getchar();
    }
    return x*f;
}
int qm(ll x,int y) {
    ll ret = 1;
    for(;y;y >>= 1,x = x * x % mod)
        if(y & 1) ret = ret * x % mod;

    return ret;
}
ll A[N],B[N];
int rev[N];
void NTT(ll *a,int n,int xs) {
    for(int i = 0;i <= n;++i)
        if(rev[i] > i) swap(a[i],a[rev[i]]);
    for(int m = 2;m <= n;m <<= 1) {
        ll w1 = qm(3,(mod - 1) / m);
        if(xs == -1) w1 = qm(w1,mod - 2);

        for(int i = 0;i < n;i += m) {
            ll w = 1;
            for(int k = 0;k < (m >> 1);++k) {
                ll u = a[i + k],t = w * a[i + k + (m >> 1)] % mod;
                a[i + k] = (u + t) % mod;a[i + k + (m >> 1)] = (u - t) % mod;
                w = w * w1 % mod;
            }
        }
    }
}
int main() {
    int n = read(),m = read();
    for(int i = 0;i <= n;++i) A[i] = read();
    for(int i = 0;i <= m;++i) B[i] = read();

    int tot = 1;
    while(tot <= n + m) tot <<= 1;
    for(int i = 0;i <= tot;++i) 
        rev[i] = (rev[i >> 1] >> 1) | (i & 1 ? (tot >> 1) : 0);

    NTT(A,tot,1);NTT(B,tot,1);
    for(int i = 0;i <= tot;++i) A[i] = A[i] * B[i] % mod;
    NTT(A,tot,-1);
    int tmp = qm(tot,mod - 2);
    for(int i = 0;i <= n + m;++i)
        printf("%lld ",(A[i] + mod) % mod * tmp % mod);

    return 0;
}

以上是关于NTT的主要内容,如果未能解决你的问题,请参考以下文章

NTT注意事项

NTT小结及原根求法

P4245 模板任意模数多项式乘法(NTT)

[生成函数][DFT][NTT] Hdu P6067 Big Integer

MTT:任意模数NTT

NTT模板 神奇的迷宫 NTT加点分治