結果

問題 No.2272 多項式乗算 mod 258280327
ユーザー MarioYCMarioYC
提出日時 2023-04-14 23:10:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,410 bytes
コンパイル時間 2,287 ms
コンパイル使用メモリ 205,144 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2024-04-18 20:48:25
合計ジャッジ時間 29,417 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 748 ms
9,728 KB
testcase_01 AC 754 ms
9,856 KB
testcase_02 AC 757 ms
9,728 KB
testcase_03 AC 745 ms
9,856 KB
testcase_04 AC 729 ms
9,856 KB
testcase_05 AC 733 ms
9,856 KB
testcase_06 AC 721 ms
9,600 KB
testcase_07 AC 757 ms
9,728 KB
testcase_08 AC 789 ms
9,856 KB
testcase_09 AC 765 ms
9,856 KB
testcase_10 AC 742 ms
9,728 KB
testcase_11 AC 731 ms
9,600 KB
testcase_12 AC 715 ms
9,856 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 837 ms
9,728 KB
testcase_17 WA -
testcase_18 AC 783 ms
9,856 KB
testcase_19 AC 797 ms
9,728 KB
testcase_20 AC 803 ms
9,728 KB
testcase_21 AC 779 ms
9,856 KB
testcase_22 AC 756 ms
9,856 KB
testcase_23 AC 749 ms
9,728 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int mod = 258280327;

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,fma") 
namespace {
    template<int n, typename T>
    void mult(const T *__restrict a, const T *__restrict b, T *__restrict res) {
        if (n <= 64) { // if length is small then naive multiplication if faster
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    res[i + j] += a[i] * b[j];
                    res[i + j] %= mod;
                }
            }
        } else {
            // cout << n << endl;
            const int mid = n / 2;
            alignas(64) T btmp[n], E[n] = {};
            auto atmp = btmp + mid;
            for (int i = 0; i < mid; i++) {
                atmp[i] = a[i] + a[i + mid]; // atmp(x) - sum of two halfs a(x)
                atmp[i] %= mod;
                btmp[i] = b[i] + b[i + mid]; // btmp(x) - sum of two halfs b(x)
                btmp[i] %= mod;
            }
            // cout << "sum" << endl;
            mult<mid>(atmp, btmp, E); // Calculate E(x) = (alow(x) + ahigh(x)) * (blow(x) + bhigh(x))
            // cout << "mult1" << endl;
            mult<mid>(a + 0, b + 0, res); // Calculate rlow(x) = alow(x) * blow(x)
            // cout << "mult2" << endl;
            mult<mid>(a + mid, b + mid, res + n); // Calculate rhigh(x) = ahigh(x) * bhigh(x)
            // cout << "mult3" << endl;
            for (int i = 0; i < mid; i++) { // Then, calculate rmid(x) = E(x) - rlow(x) - rhigh(x) and write in memory
                const auto tmp = res[i + mid];
                res[i + mid] += E[i] - res[i] - res[i + 2 * mid];
                res[i + mid] %= mod;
                res[i + 2 * mid] += E[i + mid] - tmp - res[i + 3 * mid];
                res[i + 2 * mid] %= mod;
            }
            // cout << "done" << endl;
        }
    }
}

const int nmax = (1 << 18);
int a[nmax],b[nmax],ret[2 * nmax];

int main(){
    int n,m;
    cin >> n;
    for(int i = 0;i <= n;++i) cin >> a[i];
    cin >> m;
    for(int i = 0;i <= m;++i) cin >> b[i];
    int sz = 1;
    while((1 << sz) < max(n,m)) ++sz;
    mult<nmax, int>(a, b, ret);
    cout << n + m << endl;
    for(int i = 0;i <= n + m;++i){
        auto x = ret[i];
        if(ret[i] < 0) ret[i] += mod;
        cout << x << ' ';
    }
    cout << endl;
    return 0;
}
0