結果

問題 No.2272 多項式乗算 mod 258280327
ユーザー ruthenruthen
提出日時 2023-04-17 13:30:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,130 ms / 2,000 ms
コード長 2,460 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 211,040 KB
実行使用メモリ 23,748 KB
最終ジャッジ日時 2024-04-20 18:35:50
合計ジャッジ時間 8,216 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 20 ms
6,940 KB
testcase_26 AC 22 ms
6,940 KB
testcase_27 AC 56 ms
6,940 KB
testcase_28 AC 72 ms
6,940 KB
testcase_29 AC 637 ms
16,368 KB
testcase_30 AC 1,130 ms
23,748 KB
testcase_31 AC 1,076 ms
23,488 KB
testcase_32 AC 1,083 ms
23,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#ifdef _RUTHEN
#include <debug.hpp>
#else
#define show(...) true
#endif

#define REP(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

constexpr int P = 258280327;

V<long long> convolution(V<long long> &a, V<long long> &b, const int n) {
    assert(a.size() == b.size());
    if (n <= 120) {
        V<long long> c(n + n - 1);
        REP(i, n) REP(j, n) c[i + j] += a[i] * b[j];
        REP(i, n + n - 1) c[i] %= P;
        return c;
    }
    const int n0 = (n + 1) / 2;
    const int n1 = n - n0;
    V<long long> a0(n0), a1(n1), b0(n0), b1(n1);
    copy(a.begin(), a.begin() + n0, a0.begin());
    copy(b.begin(), b.begin() + n0, b0.begin());
    copy(a.begin() + n0, a.end(), a1.begin());
    copy(b.begin() + n0, b.end(), b1.begin());
    V<long long> a01(n0), b01(n0);
    copy(a.begin(), a.begin() + n0, a01.begin());
    copy(b.begin(), b.begin() + n0, b01.begin());
    REP(i, n1) {
        a01[i] += a1[i];
        b01[i] += b1[i];
        if (a01[i] >= P) a01[i] -= P;
        if (b01[i] >= P) b01[i] -= P;
    }
    auto c0 = convolution(a0, b0, n0);
    auto c1 = convolution(a1, b1, n1);
    auto c2 = convolution(a01, b01, n0);
    V<long long> c(n0 + n0 + n0 + n0 - 1);
    REP(i, n0 + n0 - 1) {
        c[i] += c0[i];
        c[i + n0] += c2[i] - c0[i];
    }
    REP(i, n1 + n1 - 1) {
        c[i + n0] -= c1[i];
        c[i + n0 + n0] += c1[i];
    }
    REP(i, n0 + n0 + n0 + n0 - 1) {
        c[i] %= P;
        if (c[i] < 0) c[i] += P;
    }
    return c;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    N++;
    V<long long> a(N);
    REP(i, N) cin >> a[i];
    int M;
    cin >> M;
    M++;
    V<long long> b(M);
    REP(i, M) cin >> b[i];
    if (N == 1 and a[0] == 0) {
        cout << 0 << '\n';
        cout << 0 << '\n';
        return 0;
    }
    if (M == 1 and b[0] == 0) {
        cout << 0 << '\n';
        cout << 0 << '\n';
        return 0;
    }
    REP(i, N) a[i] %= P;
    REP(i, M) b[i] %= P;
    int K = max(N, M);
    while (a.size() < K) a.push_back(0);
    while (b.size() < K) b.push_back(0);
    auto c = convolution(a, b, (int)a.size());
    while (c.size() > N + M - 1) c.pop_back();
    cout << c.size() - 1 << '\n';
    REP(i, c.size()) cout << c[i] << " \n"[i + 1 == c.size()];
    return 0;
}
0