Submit Info #57993

Problem Lang User Status Time Memory
Multipoint Evaluation cpp-acl SSRS AC 4600 ms 83.39 MiB

ケース詳細
Name Status Time Memory
example_00 AC 1 ms 0.45 MiB
example_01 AC 1 ms 0.41 MiB
max_random_00 AC 4593 ms 82.64 MiB
max_random_01 AC 4600 ms 82.62 MiB
random_00 AC 1102 ms 76.91 MiB
random_01 AC 795 ms 45.84 MiB
random_02 AC 4246 ms 83.39 MiB

#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; long long modpow(long long a, long long b){ long long ans = 1; while (b > 0){ if (b % 2 == 1){ ans *= a; ans %= MOD; } a *= a; a %= MOD; b /= 2; } return ans; } long long modinv(long long a){ return modpow(a, MOD - 2); } vector<long long> mf = {1}; vector<long long> mfi = {1}; long long modfact(int n){ if (mf.size() > n){ return mf[n]; } else { for (int i = mf.size(); i <= n; i++){ long long next = mf.back() * i % MOD; mf.push_back(next); mfi.push_back(modinv(next)); } return mf[n]; } } long long modfactinv(int n){ if (mfi.size() > n){ return mfi[n]; } else { return modinv(modfact(n)); } } vector<long long> ntt(vector<long long> A, bool inv){ int N = A.size(); long long r = 3; if (inv){ r = modinv(r); } vector<long long> B(N); for (int i = N / 2; i > 0; i /= 2){ long long z = modpow(r, (MOD - 1) / (N / i)); long long z2 = 1; for (int j = 0; j < N; j += i * 2){ for (int k = 0; k < i; k++){ A[i + j + k] *= z2; A[i + j + k] %= MOD; B[j / 2 + k] = (A[j + k] + A[i + j + k]) % MOD; B[N / 2 + j / 2 + k] = (A[j + k] - A[i + j + k] + MOD) % MOD; } z2 = z2 * z % MOD; } swap(A, B); } if (inv){ int Ninv = modinv(N); for (int i = 0; i < N; i++){ A[i] = A[i] * Ninv % MOD; } } return A; } vector<long long> convolution(vector<long long> A, vector<long long> B, int d = -1){ int deg = A.size() + B.size() - 1; int N = 1; while (N < deg){ N *= 2; } A.resize(N); B.resize(N); A = ntt(A, false); B = ntt(B, false); vector<long long> ans(N); for (int i = 0; i < N; i++){ ans[i] = A[i] * B[i] % MOD; } ans = ntt(ans, true); if (d != -1){ deg = d; } ans.resize(deg); return ans; } vector<long long> polynomial_inverse(vector<long long> f){ int N = f.size(); vector<long long> ans(1); ans[0] = modinv(f[0]); for (int i = 1; i < N * 2; i *= 2){ vector<long long> ans2 = ans; ans2.resize(i * 4); int N2 = min(N, i * 2); vector<long long> f2(i * 4, 0); for (int j = 0; j < N2; j++){ f2[j] = f[j]; } ans2 = convolution(ans2, ans2, i * 2); ans2 = convolution(ans2, f2, i * 2); for (int j = 0; j < i; j++){ ans2[j] = MOD - ans2[j] + ans[j] * 2; ans2[j] %= MOD; } swap(ans, ans2); } ans.resize(N); return ans; } vector<long long> polynomial_quotient(vector<long long> f, vector<long long> g){ int N = f.size(), M = g.size(); if (N < M){ return {0}; } reverse(g.begin(), g.end()); g.resize(N - M + 1); vector<long long> t = polynomial_inverse(g); reverse(f.begin(), f.end()); vector<long long> q = convolution(f, t, N - M + 1); reverse(q.begin(), q.end()); return q; } vector<long long> polynomial_remainder(vector<long long> f, vector<long long> g){ int N = f.size(); int M = g.size(); if (M <= 300){ for (int i = N - M; i >= 0; i--){ long long q = f[i + M - 1] * modinv(g[M - 1]) % MOD; for (int j = 0; j < M; j++){ f[i + j] += MOD - q * g[j] % MOD; f[i + j] %= MOD; } f.pop_back(); } return f; } else { vector<long long> q = polynomial_quotient(f, g); vector<long long> b = convolution(g, q); for (int i = 0; i < N; i++){ f[i] += MOD - b[i]; f[i] %= MOD; } f.resize(M - 1); return f; } } vector<long long> multipoint_evaluation(vector<long long> &f, vector<long long> x){ int M = x.size(); int M2 = 1; while (M2 < M){ M2 *= 2; } vector<vector<long long>> g(M2 * 2 - 1, {1}); for (int i = 0; i < M; i++){ g[M2 - 1 + i] = vector<long long>{MOD - x[i], 1}; } for (int i = M2 - 2; i >= 0; i--){ g[i] = convolution(g[i * 2 + 1], g[i * 2 + 2]); } g[0] = polynomial_remainder(f, g[0]); for (int i = 1; i < M2 * 2 - 1; i++){ g[i] = polynomial_remainder(g[(i - 1) / 2], g[i]); } vector<long long> ans(M); for (int i = 0; i < M; i++){ ans[i] = g[M2 - 1 + i][0]; } return ans; } int main(){ int N, M; cin >> N >> M; vector<long long> c(N); for (int i = 0; i < N; i++){ cin >> c[i]; } vector<long long> p(M); for (int i = 0; i < M; i++){ cin >> p[i]; } vector<long long> y = multipoint_evaluation(c, p); for (int i = 0; i < M; i++){ cout << y[i]; if (i < M - 1){ cout << ' '; } } cout << endl; }