提交记录 27449


用户 题目 状态 得分 用时 内存 语言 代码长度
ADeadLoss 1002. 测测你的多项式乘法 Accepted 100 283.216 ms 48428 KB C++ 3.13 KB
提交时间 评测时间
2024-11-30 22:22:37 2024-11-30 22:22:39
#include<bits/stdc++.h>
using namespace std;
namespace jly{constexpr int P = 998244353;

int power(int a, int b) {
    int res = 1;
    for (; b; b /= 2, a = 1LL * a * a % P) {
        if (b % 2) {
            res = 1LL * res * a % P;
        }
    }
    return res;
}

std::vector<int> rev, roots {0, 1};

void dft(std::vector<int> &a) {
    int n = a.size();
    if (int(rev.size()) != n) {
        int k = __builtin_ctz(n) - 1;
        rev.resize(n);
        for (int i = 0; i < n; i++) {
            rev[i] = rev[i >> 1] >> 1 | (i & 1) << k;
        }
    }
    for (int i = 0; i < n; i++) {
        if (rev[i] < i) {
            std::swap(a[i], a[rev[i]]);
        }
    }
    if (roots.size() < n) {
        int k = __builtin_ctz(roots.size());
        roots.resize(n);
        while ((1 << k) < n) {
            int e = power(31, 1 << (__builtin_ctz(P - 1) - k - 1));
            for (int i = 1 << (k - 1); i < (1 << k); i++) {
                roots[2 * i] = roots[i];
                roots[2 * i + 1] = 1LL * roots[i] * e % P;
            }
            k++;
        }
    }

    for (int k = 1; k < n; k *= 2) {
        for (int i = 0; i < n; i += 2 * k) {
            for (int j = 0; j < k; j++) {
                int u = a[i + j];
                int v = 1LL * a[i + j + k] * roots[k + j] % P;
                a[i + j] = (u + v) % P;
                a[i + j + k] = (u +P- v) % P;
            }
        }
    }
}

void idft(std::vector<int> &a) {
    int n = a.size();
    std::reverse(a.begin() + 1, a.end());
    dft(a);
    int inv = (1 - P) / n+P;
    for (int i = 0; i < n; i++) {
        a[i] = 1LL * a[i] * inv % P;
    }
}

std::vector<int> mul(std::vector<int> a, std::vector<int> b) {
    int n = 1, tot = a.size() + b.size() - 1;
    while (n < tot) {
        n *= 2;
    }
    if (tot < 128) {
        std::vector<int> c(a.size() + b.size() - 1);
        for (int i = 0; i < a.size(); i++) {
            for (int j = 0; j < b.size(); j++) {
                c[i + j] = (c[i + j] + 1LL * a[i] * b[j]) % P;
            }
        }
        return c;
    }
    a.resize(n);
    b.resize(n);
    dft(a);
    dft(b);
    for (int i = 0; i < n; i++) {
        a[i] = 1LL * a[i] * b[i] % P;
    }
    idft(a);
    a.resize(tot);
    return a;
}
template<class InputIt,class OutputIt>
OutputIt mul(InputIt afirst,InputIt alast,InputIt bfirst,InputIt blast,OutputIt dfirst){
	static std::vector<int>a,b;
	a.assign(afirst,alast);b.assign(bfirst,blast);
    int n = 1, tot = a.size() + b.size() - 1;
    auto dlast=dfirst+tot;
    while (n < tot) {
        n *= 2;
    }
    if (tot < 128) {
//        std::vector<int> c(a.size() + b.size() - 1);
        for (int i = 0; i < a.size(); i++) {
            for (int j = 0; j < b.size(); j++) {
                dfirst[i + j] = (dfirst[i + j] + 1LL * a[i] * b[j]) % P;
            }
        }
        return dlast;
    }
    a.resize(n);
    b.resize(n);
    dft(a);
    dft(b);
    for (int i = 0; i < n; i++) {
        a[i] = 1LL * a[i] * b[i] % P;
    }
    idft(a);
    a.resize(tot);
    std::copy(a.begin(),a.end(),dfirst);
    return dlast;
}
}
void poly_multiply(unsigned *a, int n, unsigned *b, int m, unsigned *c){
	jly::mul(a,a+n+1,b,b+m+1,c);
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #1283.216 ms47 MB + 300 KBAcceptedScore: 100


Judge Duck Online | 评测鸭在线
Server Time: 2024-12-27 04:44:48 | Loaded in 0 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠