結果

問題 No.2272 多項式乗算 mod 258280327
ユーザー shinchanshinchan
提出日時 2024-02-22 18:16:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 10,926 bytes
コンパイル時間 2,898 ms
コンパイル使用メモリ 225,660 KB
実行使用メモリ 46,404 KB
最終ジャッジ日時 2024-09-29 04:28:24
合計ジャッジ時間 6,309 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 1 ms
6,824 KB
testcase_06 AC 2 ms
6,820 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,816 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 2 ms
6,816 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1 ms
6,816 KB
testcase_19 AC 2 ms
6,820 KB
testcase_20 AC 2 ms
6,820 KB
testcase_21 AC 2 ms
6,820 KB
testcase_22 AC 2 ms
6,820 KB
testcase_23 AC 2 ms
6,824 KB
testcase_24 AC 6 ms
6,820 KB
testcase_25 AC 24 ms
6,816 KB
testcase_26 AC 24 ms
6,816 KB
testcase_27 AC 51 ms
8,832 KB
testcase_28 AC 87 ms
13,184 KB
testcase_29 AC 375 ms
43,148 KB
testcase_30 AC 435 ms
46,320 KB
testcase_31 AC 429 ms
46,404 KB
testcase_32 AC 436 ms
46,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define pb(a) push_back(a)
#define rep(i, n) for(int i=0;i<n;i++)
#define foa(e, v) for(auto& e : v)
using ll = long long;
const ll MOD7 = 1000000007, MOD998 = 998244353, INF = (1LL << 60);
#define dout(a) cout<<fixed<<setprecision(10)<<a<<endl;

template<int MOD> struct Modint {
    long long val;
    constexpr Modint(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; }
    constexpr int mod() const { return MOD; }
    constexpr long long value() const { return val; }
    constexpr Modint operator - () const noexcept { return val ? MOD - val : 0; }
    constexpr Modint operator + (const Modint& r) const noexcept { return Modint(*this) += r; }
    constexpr Modint operator - (const Modint& r) const noexcept { return Modint(*this) -= r; }
    constexpr Modint operator * (const Modint& r) const noexcept { return Modint(*this) *= r; }
    constexpr Modint operator / (const Modint& r) const noexcept { return Modint(*this) /= r; }
    constexpr Modint& operator += (const Modint& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Modint& operator -= (const Modint& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Modint& operator *= (const Modint& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Modint& operator /= (const Modint& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b, swap(a, b);
            u -= t * v, swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Modint& r) const noexcept { return this->val == r.val; }
    constexpr bool operator != (const Modint& r) const noexcept { return this->val != r.val; }
    friend constexpr istream& operator >> (istream& is, Modint<MOD>& x) noexcept {
        is >> x.val;
        x.val %= MOD;
        if (x.val < 0) x.val += MOD;
        return is;
    }
    friend constexpr ostream& operator << (ostream& os, const Modint<MOD>& x) noexcept {
        return os << x.val;
    }
    constexpr Modint<MOD> pow(long long n) noexcept {
        if (n == 0) return 1;
        if (n < 0) return this->pow(-n).inv();
        Modint<MOD> ret = pow(n >> 1);
        ret *= ret;
        if (n & 1) ret *= *this;
        return ret;
    }
    constexpr Modint<MOD> inv() noexcept {
        long long a = this->val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b, swap(a, b);
            u -= t * v, swap(u, v);
        }
        return Modint<MOD>(u);
    }
};

const int MOD = 258280327;
using mint = Modint<MOD>;
long long modinv(long long a, long long MOD) {
    long long b = MOD, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; std::swap(a, b);
        u -= t * v; std::swap(u, v);
    }
    u %= MOD; 
    if (u < 0) u += MOD;
    return u;
}

long long modpow(long long a, long long n, long long MOD) {
    long long res = 1;
    a %= MOD;
    if(n < 0) {
        n = -n;
        a = modinv(a, MOD);
    }
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

namespace NTT {
    int calc_primitive_root(int MOD) {
        if (MOD == 2) return 1;
        if (MOD == 167772161) return 3;
        if (MOD == 469762049) return 3;
        if (MOD == 754974721) return 11;
        if (MOD == 998244353) return 3;
        int divs[20] = {};
        divs[0] = 2;
        int cnt = 1;
        long long x = (MOD - 1) >> 1;
        while (x % 2 == 0) x >>= 1;
        for (long long i = 3; i * i <= x; i += 2) {
            if (x % i == 0) {
                divs[cnt ++] = i;
                while (x % i == 0) x /= i;
            }
        }
        if (x > 1) divs[cnt++] = x;
        for (int g = 2;; ++ g) {
            bool ok = true;
            for (int i = 0; i < cnt; i++) {
                if (modpow(g, (MOD - 1) / divs[i], MOD) == 1) {
                    ok = false;
                    break;
                }
            }
            if (ok) return g;
        }
    }

    int get_fft_size(int N, int M) {
        int size_a = 1, size_b = 1;
        while (size_a < N) size_a <<= 1;
        while (size_b < M) size_b <<= 1;
        return std::max(size_a, size_b) << 1;
    }

    template<class mint> void trans(std::vector<mint>& v, bool inv = false) {
        if (v.empty()) return;
        int N = (int) v.size();
        int MOD = v[0].mod();
        int PR = calc_primitive_root(MOD);
        static bool first = true;
        static std::vector<long long> vbw(30), vibw(30);
        if (first) {
            first = false;
            for (int k = 0; k < 30; ++ k) {
                vbw[k] = modpow(PR, (MOD - 1) >> (k + 1), MOD);
                vibw[k] = modinv(vbw[k], MOD);
            }
        }
        for (int i = 0, j = 1; j < N - 1; ++ j) {
            for (int k = N >> 1; k > (i ^= k); k >>= 1);
            if (i > j) std::swap(v[i], v[j]);
        }
        for (int k = 0, t = 2; t <= N; ++ k, t <<= 1) {
            long long bw = vbw[k];
            if (inv) bw = vibw[k];
            for (int i = 0; i < N; i += t) {
                mint w = 1;
                for (int j = 0; j < (t >> 1); ++ j) {
                    int j1 = i + j, j2 = i + j + (t >> 1);
                    mint c1 = v[j1], c2 = v[j2] * w;
                    v[j1] = c1 + c2;
                    v[j2] = c1 - c2;
                    w *= bw;
                }
            }
        }
        if (inv) {
            long long invN = modinv(N, MOD);
            for (int i = 0; i < N; ++ i) v[i] = v[i] * invN;
        }
    }

    static constexpr int MOD0 = 754974721;
    static constexpr int MOD1 = 167772161;
    static constexpr int MOD2 = 469762049;
    using mint0 = Modint<MOD0>;
    using mint1 = Modint<MOD1>;
    using mint2 = Modint<MOD2>;
    static const mint1 imod0 = 95869806; // modinv(MOD0, MOD1);
    static const mint2 imod1 = 104391568; // modinv(MOD1, MOD2);
    static const mint2 imod01 = 187290749; // imod1 / MOD0;

    // small case (T = mint, long long)
    template<class T> std::vector<T> naive_mul 
    (const std::vector<T>& A, const std::vector<T>& B) {
        if (A.empty() || B.empty()) return {};
        int N = (int) A.size(), M = (int) B.size();
        std::vector<T> res(N + M - 1);
        for (int i = 0; i < N; ++ i)
            for (int j = 0; j < M; ++ j)
                res[i + j] += A[i] * B[j];
        return res;
    }
};

// mint
template<class mint> std::vector<mint> convolution
(const std::vector<mint>& A, const std::vector<mint>& B) {
    if (A.empty() || B.empty()) return {};
    int N = (int) A.size(), M = (int) B.size();
    if (std::min(N, M) < 30) return NTT::naive_mul(A, B);
    int MOD = A[0].mod();
    int size_fft = NTT::get_fft_size(N, M);
    if (MOD == 998244353) {
        std::vector<mint> a(size_fft), b(size_fft), c(size_fft);
        for (int i = 0; i < N; ++i) a[i] = A[i];
        for (int i = 0; i < M; ++i) b[i] = B[i];
        NTT::trans(a), NTT::trans(b);
        std::vector<mint> res(size_fft);
        for (int i = 0; i < size_fft; ++i) res[i] = a[i] * b[i];
        NTT::trans(res, true);
        res.resize(N + M - 1);
        return res;
    }
    std::vector<NTT::mint0> a0(size_fft, 0), b0(size_fft, 0), c0(size_fft, 0);
    std::vector<NTT::mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);
    std::vector<NTT::mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);
    for (int i = 0; i < N; ++ i) {
        a0[i] = A[i].value();
        a1[i] = A[i].value();
        a2[i] = A[i].value();
    }
    for (int i = 0; i < M; ++ i) {
        b0[i] = B[i].value();
        b1[i] = B[i].value();
        b2[i] = B[i].value();
    }
    NTT::trans(a0), NTT::trans(a1), NTT::trans(a2), 
    NTT::trans(b0), NTT::trans(b1), NTT::trans(b2);
    for (int i = 0; i < size_fft; ++i) {
        c0[i] = a0[i] * b0[i];
        c1[i] = a1[i] * b1[i];
        c2[i] = a2[i] * b2[i];
    }
    NTT::trans(c0, true), NTT::trans(c1, true), NTT::trans(c2, true);
    static const mint mod0 = NTT::MOD0, mod01 = mod0 * NTT::MOD1;
    std::vector<mint> res(N + M - 1);
    for (int i = 0; i < N + M - 1; ++ i) {
        int y0 = c0[i].value();
        int y1 = (NTT::imod0 * (c1[i] - y0)).value();
        int y2 = (NTT::imod01 * (c2[i] - y0) - NTT::imod1 * y1).value();
        res[i] = mod01 * y2 + mod0 * y1 + y0;
    }
    return res;
}

// long long
std::vector<long long> convolution_ll
(const std::vector<long long>& A, const std::vector<long long>& B) {
    if (A.empty() || B.empty()) return {};
    int N = (int) A.size(), M = (int) B.size();
    if (std::min(N, M) < 30) return NTT::naive_mul(A, B);
    int size_fft = NTT::get_fft_size(N, M);
    std::vector<NTT::mint0> a0(size_fft, 0), b0(size_fft, 0), c0(size_fft, 0);
    std::vector<NTT::mint1> a1(size_fft, 0), b1(size_fft, 0), c1(size_fft, 0);
    std::vector<NTT::mint2> a2(size_fft, 0), b2(size_fft, 0), c2(size_fft, 0);
    for (int i = 0; i < N; ++ i) {
        a0[i] = A[i];
        a1[i] = A[i];
        a2[i] = A[i];
    }
    for (int i = 0; i < M; ++ i) {
        b0[i] = B[i];
        b1[i] = B[i];
        b2[i] = B[i];
    }
    NTT::trans(a0), NTT::trans(a1), NTT::trans(a2), 
    NTT::trans(b0), NTT::trans(b1), NTT::trans(b2);
    for (int i = 0; i < size_fft; ++ i) {
        c0[i] = a0[i] * b0[i];
        c1[i] = a1[i] * b1[i];
        c2[i] = a2[i] * b2[i];
    }
    NTT::trans(c0, true), NTT::trans(c1, true), NTT::trans(c2, true);
    static const long long mod0 = NTT::MOD0, mod01 = mod0 * NTT::MOD1;
    static const __int128_t mod012 = (__int128_t)mod01 * NTT::MOD2;
    std::vector<long long> res(N + M - 1);
    for (int i = 0; i < N + M - 1; ++ i) {
        int y0 = c0[i].value();
        int y1 = (NTT::imod0 * (c1[i] - y0)).value();
        int y2 = (NTT::imod01 * (c2[i] - y0) - NTT::imod1 * y1).value();
        __int128_t tmp = (__int128_t)mod01 * y2 + (__int128_t)mod0 * y1 + y0;
        if(tmp < (mod012 >> 1)) res[i] = tmp;
        else res[i] = tmp - mod012;
    }
    return res;
}

// depends on {modint.cpp}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n;
    vector<mint> a(n + 1);
    rep(i, n + 1) cin >> a[i];
    cin >> m;
    vector<mint> b(m + 1);
    rep(i, m + 1) cin >> b[i];
    if(n == 0 and a[0] == 0) {
        cout << 0 << endl;
        return 0;
    }
    if(m == 0 and b[0] == 0) {
        cout << 0 << endl;
        return 0;
    }
    auto c = convolution(a, b);
    int x = n + m;
    c.resize(x + 1);
    cout << x << endl;
    foa(e, c) cout << e << " ";
    cout << endl;
    return 0;
}
0