码迷,mamicode.com
首页 > 其他好文 > 详细

NTT

时间:2020-01-22 12:32:48      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:printf   iostream   amp   +=   ||   ring   ++i   space   turn   

存一发模板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

标签:printf   iostream   amp   +=   ||   ring   ++i   space   turn   

原文地址:https://www.cnblogs.com/wxyww/p/NTT.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!