結果
問題 | No.931 Multiplicative Convolution |
ユーザー | risujiroh |
提出日時 | 2019-10-02 04:11:25 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 84 ms / 2,000 ms |
コード長 | 4,754 bytes |
コンパイル時間 | 1,877 ms |
コンパイル使用メモリ | 183,300 KB |
実行使用メモリ | 7,316 KB |
最終ジャッジ日時 | 2024-10-11 02:39:28 |
合計ジャッジ時間 | 4,632 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,816 KB |
testcase_05 | AC | 2 ms
6,820 KB |
testcase_06 | AC | 3 ms
6,816 KB |
testcase_07 | AC | 10 ms
6,816 KB |
testcase_08 | AC | 84 ms
7,316 KB |
testcase_09 | AC | 71 ms
7,228 KB |
testcase_10 | AC | 81 ms
7,192 KB |
testcase_11 | AC | 71 ms
7,132 KB |
testcase_12 | AC | 48 ms
6,820 KB |
testcase_13 | AC | 82 ms
7,104 KB |
testcase_14 | AC | 84 ms
7,316 KB |
testcase_15 | AC | 84 ms
7,312 KB |
testcase_16 | AC | 82 ms
7,108 KB |
コンパイルメッセージ
main.cpp:11:20: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 11 | constexpr ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {} | ^~~~ main.cpp:38:9: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 38 | M pow(auto n) const { | ^~~~ main.cpp:44:22: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 44 | friend M operator*(auto l, M r) { return M(l) *= r; } | ^~~~ main.cpp:45:22: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 45 | friend M operator/(auto l, M r) { return M(l) /= r; } | ^~~~ main.cpp:46:22: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 46 | friend M operator+(auto l, M r) { return M(l) += r; } | ^~~~ main.cpp:47:22: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 47 | friend M operator-(auto l, M r) { return M(l) -= r; } | ^~~~ main.cpp:50:26: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 50 | friend bool operator==(auto l, M r) { return M(l) == r; } | ^~~~ main.cpp:51:26: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts' 51 | friend bool operator!=(auto l, M r) { return !(l == r); } | ^~~~
ソースコード
#include <bits/stdc++.h> using namespace std; using lint = long long; template<class T = int> using V = vector<T>; template<class T = int> using VV = V< V<T> >; template<unsigned P> struct ModInt { using M = ModInt; unsigned v; constexpr ModInt() : v(0) {} constexpr ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {} constexpr ModInt(unsigned _v, int) : v(_v) {} static constexpr unsigned p() { return P; } M operator+() const { return *this; } M operator-() const { return {v ? P - v : 0, 0}; } explicit operator bool() const noexcept { return v; } bool operator!() const noexcept { return !(bool)*this; } M operator*(M r) const { return M(*this) *= r; } M operator/(M r) const { return M(*this) /= r; } M operator+(M r) const { return M(*this) += r; } M operator-(M r) const { return M(*this) -= r; } bool operator==(M r) const { return v == r.v; } bool operator!=(M r) const { return !(*this == r); } M& operator*=(M r) { v = (uint64_t)v * r.v % P; return *this; } M& operator/=(M r) { return *this *= r.inv(); } M& operator+=(M r) { if ((v += r.v) >= P) v -= P; return *this; } M& operator-=(M r) { if ((v += P - r.v) >= P) v -= P; return *this; } M inv() const { int a = v, b = P, x = 1, u = 0; while (b) { int q = a / b; swap(a -= q * b, b); swap(x -= q * u, u); } assert(a == 1); return x; } M pow(auto n) const { if (n < 0) return pow(-n).inv(); M res = 1; for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a; return res; } friend M operator*(auto l, M r) { return M(l) *= r; } friend M operator/(auto l, M r) { return M(l) /= r; } friend M operator+(auto l, M r) { return M(l) += r; } friend M operator-(auto l, M r) { return M(l) -= r; } friend ostream& operator<<(ostream& os, M r) { return os << r.v; } friend istream& operator>>(istream& is, M& r) { lint x; is >> x; r = x; return is; } friend bool operator==(auto l, M r) { return M(l) == r; } friend bool operator!=(auto l, M r) { return !(l == r); } }; using Mint = ModInt<998244353>; template<unsigned P, unsigned g> void ntt(V< ModInt<P> >& a, bool inv = false) { int n = a.size(); assert(__builtin_popcount(n) == 1); int j = 0; for (int i = 1; i < n; ++i) { int w = n >> 1; while (j >= w) j -= w, w >>= 1; j += w; if (i < j) swap(a[i], a[j]); } assert((P - 1) % n == 0); auto xi = ModInt<P>(g).pow((P - 1) / n); if (inv) xi = xi.inv(); for (int k = 0; 1 << k < n; ++k) { const int w = 1 << k; const auto dt = xi.pow(n >> k + 1); for (int s = 0; s < n; s += 2 * w) { ModInt<P> t = 1; for (int i = s; i < s + w; ++i) { auto p = a[i], q = a[i + w] * t; a[i] = p + q, a[i + w] = p - q; t *= dt; } } } } template<unsigned P, unsigned g = 6420> V< ModInt<P> > multiply(const V< ModInt<P> >& a, const V< ModInt<P> >& b) { if (a.empty() or b.empty()) return {}; int sz = a.size() + b.size() - 1, n = 1 << __lg(2 * sz - 1); auto _a = a, _b = b; _a.resize(n), _b.resize(n); ntt<P, g>(_a), ntt<P, g>(_b); for (int i = 0; i < n; ++i) _a[i] *= _b[i]; ntt<P, g>(_a, true); _a.resize(sz); const auto inv_n = ModInt<P>(n).inv(); for (auto&& e : _a) e *= inv_n; return _a; } lint tmod(lint a, lint p) { return (a %= p) < 0 ? a + p : a; } lint mod_pow(lint a, lint n, lint p) { assert(n >= 0); a = tmod(a, p); lint res = 1; while (n) { if (n & 1) (res *= a) %= p; (a *= a) %= p; n >>= 1; } return res; } template<class Z> map<Z, int> factorize(Z n) { map<Z, int> res; for (Z i = 2; i * i <= n; ++i) while (n % i == 0) ++res[i], n /= i; if (n != 1) ++res[n]; return res; } template<class Z> Z rng(Z a, Z b) { static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count()); return uniform_int_distribution<Z>(a, b - 1)(mt); } lint primitive_root(lint p) { auto mp = factorize(p - 1); while (true) { lint g = rng(1LL, p); bool ok = true; for (const auto& e : mp) { if (mod_pow(g, (p - 1) / e.first, p) == 1) { ok = false; break; } } if (!ok) continue; return g; } } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); lint p; cin >> p; V<Mint> a(p), b(p); for (int i = 1; i < p; ++i) cin >> a[i]; for (int i = 1; i < p; ++i) cin >> b[i]; lint g = primitive_root(p); V<Mint> na(p - 1), nb(p - 1); lint t = 1; for (int i = 0; i < p - 1; ++i) { na[i] = a[t]; nb[i] = b[t]; (t *= g) %= p; } auto nc = multiply(na, nb); V<Mint> c(p); t = 1; for (int i = 0; i < (int)nc.size(); ++i) { c[t] += nc[i]; (t *= g) %= p; } for (int i = 1; i < p; ++i) { cout << c[i] << " \n"[i == p - 1]; } }