結果
問題 | No.2272 多項式乗算 mod 258280327 |
ユーザー |
|
提出日時 | 2023-04-15 02:19:00 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 1,799 ms / 2,000 ms |
コード長 | 2,110 bytes |
コンパイル時間 | 2,089 ms |
コンパイル使用メモリ | 199,736 KB |
最終ジャッジ日時 | 2025-02-12 08:41:42 |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 33 |
ソースコード
#include <bits/stdc++.h> using namespace std; #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) { assert(a.size() == b.size()); const int n = (int)a.size(); if (n <= 60) { 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 n2 = (n + 1) / 2; V<long long> a0(n2), a1(n2), b0(n2), b1(n2); for (int i = 0; i < n2; i++) { a0[i] = a[i]; b0[i] = b[i]; } for (int i = n2; i < n; i++) { a1[i - n2] = a[i]; b1[i - n2] = b[i]; } V<long long> a01(n2), b01(n2); REP(i, n2) { a01[i] = a0[i] + a1[i]; b01[i] = b0[i] + b1[i]; if (a01[i] >= P) a01[i] -= P; if (b01[i] >= P) b01[i] -= P; } auto c0 = convolution(a0, b0); auto c1 = convolution(a1, b1); auto c2 = convolution(a01, b01); V<long long> c(n2 + n2 + n2 + n2 - 1); REP(i, n2 + n2 - 1) { c[i] += c0[i]; c[i + n2] += c2[i] - c0[i] - c1[i]; c[i + n2 + n2] += c1[i]; } REP(i, n2 + n2 + n2 + n2 - 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); 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; }