NTT模板

//一个n项和一个m项求卷积
typedef long long LL;
const int p = 998244353, G = 3, Gi = 332748118;//这里的Gi是G的除法逆元
const int N = 5000007;
const double PI = acos(-1);
int n, m;
int res, ans[N];
int len = 1;//
int L;//二进制的位数
int RR[N];
LL a[N], b[N];
inline int qread()
{ 
    int x = 0, y = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')
            y = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * y;
}
LL ksm(LL a, LL b, LL p)
{
    LL ans = 1;
    while (b) {
        if (b & 1) ans = ans * a % p;
        b >>= 1;
        a = a * a % p;
    }
    return ans;
}
LL inv(LL x) { return ksm(x, p - 2, p); }
void NTT(LL* A, int type)
{
    for (int i = 0; i < len; ++i)
        if (i < RR[i])
            swap(A[i], A[RR[i]]);
    for (int mid = 1; mid < len; mid <<= 1) {
        LL wn = ksm(G, (p - 1) / (mid * 2), p);
        if (type == -1) wn = ksm(wn, p - 2, p);
        //如果超时了上面if这句话删掉,在下面的if(type == -1)里加上下面这个循环
        /*for (int i = 1; i < len / 2; i ++)
        swap(A[i], A[len - i]); */
        for (int j = mid << 1, pos = 0; pos < len; pos += j) {
            LL w = 1;
            for (int k = 0; k < mid; ++k, w = (w * wn) % p) {
                int x = A[pos + k], y = w * A[pos + mid + k] % p;
                A[pos + k] = (x + y) % p;
                A[pos + k + mid] = (x - y + p) % p;

            }
        }
    }
    if (type == -1) {
        LL limit_inv = inv(len);
        for (int i = 0; i < len; ++i)
            A[i] = (A[i] * limit_inv) % p;
    }
}
void poly_mul(LL* a, LL* b, int deg)//多项式乘法
{
    for (len = 1, L = 0; len <= deg; len <<= 1) L++;
    for (int i = 0; i < len; ++i) {
        RR[i] = (RR[i >> 1] >> 1) | ((i & 1) << (L - 1));
    }
    NTT(a, 1);
    NTT(b, 1);
    for (int i = 0; i < len; ++i) a[i] = a[i] * b[i] % p;
    NTT(a, -1);
}
int main()
{
    n = qread(), m = qread();
    for (int i = 0; i <= n; ++i) a[i] = (qread() + p) % p;//取模
    for (int i = 0; i <= m; ++i) b[i] = (qread() + p) % p;
    poly_mul(a, b, n + m);
    for (int i = 0; i <= n + m; ++i)
        printf("%d ", a[i]);
    return 0;
}

附带原根表

p=r \cdot 2^k + 1$r$$k$$g$
3112
5122
17143
97355
193365
257183
768115917
1228931211
409615133
655371163
78643331810
576716911193
73400337203
2306867311213
10485760125223
1677721615253
4697620497263
998244353119233
1004535809479213
2013265921152731
228170137717273
32212254733305
7516192768135313
773094113299337
20615843020933622
206158430208115377
27487790694415393
65970697666573415
395824185999379425
791648371998739435
26388279066624115447
123145302310912135453
133700613937561719463
379991218559385727475
4222124650659841154819
78812993478983697506
315251973915934737523
1801439850948198415556
194555503902405427327565
417934045419982028929573
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值