結果
問題 | No.931 Multiplicative Convolution |
ユーザー | pekempey |
提出日時 | 2019-11-23 06:46:47 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 72 ms / 2,000 ms |
コード長 | 4,685 bytes |
コンパイル時間 | 1,989 ms |
コンパイル使用メモリ | 183,816 KB |
実行使用メモリ | 12,080 KB |
最終ジャッジ日時 | 2024-10-11 06:46:34 |
合計ジャッジ時間 | 4,079 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 9 ms
7,424 KB |
testcase_01 | AC | 8 ms
7,424 KB |
testcase_02 | AC | 8 ms
7,424 KB |
testcase_03 | AC | 9 ms
7,424 KB |
testcase_04 | AC | 9 ms
7,552 KB |
testcase_05 | AC | 9 ms
7,424 KB |
testcase_06 | AC | 8 ms
7,680 KB |
testcase_07 | AC | 15 ms
7,808 KB |
testcase_08 | AC | 72 ms
12,068 KB |
testcase_09 | AC | 56 ms
11,952 KB |
testcase_10 | AC | 69 ms
11,896 KB |
testcase_11 | AC | 58 ms
11,936 KB |
testcase_12 | AC | 44 ms
10,116 KB |
testcase_13 | AC | 70 ms
11,948 KB |
testcase_14 | AC | 72 ms
12,060 KB |
testcase_15 | AC | 72 ms
12,060 KB |
testcase_16 | AC | 70 ms
12,080 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define repe(i, l, r) for (int i = (l); i < (r); i++) #define reper(i, l, r) for (int i = (r) - 1; i >= (l); i--) #define repi(i, l, r) for (int i = (l); i <= (r); i++) #define repir(i, l, r) for (int i = (r); i >= (l); i--) #define range(a) a.begin(), a.end() void initio() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } constexpr int MOD = 998244353; constexpr int ROOT = 3; class mint { int n; public: mint(int n_ = 0) : n(n_) {} explicit operator int() { return n; } friend mint operator-(mint a) { return -a.n + MOD * (a.n != 0); } friend mint operator+(mint a, mint b) { int x = a.n + b.n; return x - (x >= MOD) * MOD; } friend mint operator-(mint a, mint b) { int x = a.n - b.n; return x + (x < 0) * MOD; } friend mint operator*(mint a, mint b) { return (long long)a.n * b.n % MOD; } friend mint &operator+=(mint &a, mint b) { return a = a + b; } friend mint &operator-=(mint &a, mint b) { return a = a - b; } friend mint &operator*=(mint &a, mint b) { return a = a * b; } friend bool operator==(mint a, mint b) { return a.n == b.n; } friend bool operator!=(mint a, mint b) { return a.n != b.n; } friend istream &operator>>(istream &i, mint &a) { return i >> a.n; } friend ostream &operator<<(ostream &o, mint a) { return o << a.n; } }; mint modpow(mint a, long long b) { mint res = 1; while (b > 0) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } int modpow2(int a, long long b, int p) { int res = 1; while (b > 0) { if (b & 1) res = (ll)res * a % p; a = (ll)a * a % p; b >>= 1; } return res; } mint modinv(mint n) { int a = (int)n, b = MOD; int s = 1, t = 0; while (b != 0) { int q = a / b; a -= q * b; s -= q * t; swap(a, b); swap(s, t); } return s >= 0 ? s : s + MOD; } template<int N> struct NTT { mint rots[N]; NTT() { mint w = modpow(ROOT, (MOD - 1) / N); mint ws = 1; for (int i = 0; i < N / 2; i++) { rots[i + N / 2] = ws; ws *= w; } for (int i = N / 2 - 1; i >= 1; i--) { rots[i] = rots[i * 2]; } } void ntt(vector<mint> &a) { const int n = a.size(); int i = 0; for (int j = 1; j < n - 1; j++) { for (int k = n >> 1; k > (i ^= k); k >>= 1); if (j < i) swap(a[i], a[j]); } for (int i = 1; i < n; i *= 2) { for (int j = 0; j < n; j += i * 2) { for (int k = 0; k < i; k++) { mint s = a[j + k]; mint t = a[j + k + i] * rots[i + k]; a[j + k ] = s + t; a[j + k + i] = s - t; } } } } void invntt(vector<mint> &a) { const int n = a.size(); ntt(a); reverse(a.begin() + 1, a.end()); mint inv_n = modinv(n); for (int i = 0; i < n; i++) { a[i] *= inv_n; } } vector<mint> convolution(vector<mint> a, vector<mint> b) { const int n = a.size() + b.size() - 1; int t = 1; while (t < n) t *= 2; a.resize(t); b.resize(t); ntt(a); ntt(b); for (int i = 0; i < t; i++) { a[i] *= b[i]; } invntt(a); a.resize(n); return a; } }; NTT<1 << 20> ntt; vector<long long> divisors(long long n) { vector<long long> res; for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { res.push_back(i); if (i * i != n) { res.push_back(n / i); } } } sort(range(res)); return res; } int primitive_root(int p) { auto ds = divisors(p - 1); ds.pop_back(); for (int i = 1; i <= p - 1; i++) { bool all = true; for (int d : ds) { all &= modpow2(i, d, p) != 1; } if (all) return i; } abort(); } int main() { initio(); int P; cin >> P; vector<mint> A(P - 1), B(P - 1); rep(i, P - 1) cin >> A[i]; rep(i, P - 1) cin >> B[i]; int g = primitive_root(P); vector<int> table(P - 1); table[0] = 1; for (int i = 1; i <= P - 2; i++) { table[i] = (ll)table[i - 1] * g % P; } vector<int> inv(P); for (int i = 0; i <= P - 2; i++) { inv[table[i]] = i; } vector<mint> F(P - 1); vector<mint> G(P - 1); for (int i = 0; i <= P - 2; i++) { F[inv[i + 1]] = A[i]; G[inv[i + 1]] = B[i]; } auto ans = ntt.convolution(F, G); for (int i = P - 1; i < ans.size(); i++) { ans[i % (P - 1)] += ans[i]; } vector<mint> true_ans(P); for (int i = 0; i < P - 1; i++) { true_ans[table[i]] = ans[i]; } for (int i = 1; i < true_ans.size(); i++) { cout << true_ans[i] << " \n"[i == true_ans.size() - 1]; } }