Submit Info #53573

Problem Lang User Status Time Memory
Polynomial Interpolation cpp-acl risujiroh AC 557 ms 52.25 MiB

ケース詳細
Name Status Time Memory
example_00 AC 1 ms 0.71 MiB
example_01 AC 1 ms 0.61 MiB
max_random_00 AC 557 ms 52.23 MiB
max_random_01 AC 554 ms 52.23 MiB
random_00 AC 552 ms 52.25 MiB
random_01 AC 536 ms 50.88 MiB
random_02 AC 253 ms 25.47 MiB

#include <bits/stdc++.h> #include <atcoder/convolution> using Fp = atcoder::modint998244353; std::istream& operator>>(std::istream& is, Fp& a) { int v; is >> v; assert(0 <= v && v < Fp::mod()); a = Fp::raw(v); return is; } std::ostream& operator<<(std::ostream& os, Fp a) { return os << a.val(); } using Fps = std::vector<Fp>; int sz(const Fps& a) { return a.size(); } Fps operator-(Fps a) { for (auto&& e : a) e = -e; return a; } Fps& operator+=(Fps& a, const Fps& b) { if (sz(a) < sz(b)) a.reserve(sz(b)), a.resize(sz(b)); for (int i = 0; i < sz(b); ++i) a[i] += b[i]; return a; } Fps operator+(Fps a, const Fps& b) { return std::move(a += b); } Fps& operator-=(Fps& a, const Fps& b) { if (sz(a) < sz(b)) a.reserve(sz(b)), a.resize(sz(b)); for (int i = 0; i < sz(b); ++i) a[i] -= b[i]; return a; } Fps operator-(Fps a, const Fps& b) { return std::move(a -= b); } Fps& operator*=(Fps& a, Fp b) { for (auto&& e : a) e *= b; return a; } Fps operator*(Fps a, Fp b) { return std::move(a *= b); } Fps operator*(Fp a, Fps b) { return std::move(b *= a); } Fps& operator/=(Fps& a, Fp b) { b = b.inv(); for (auto&& e : a) e *= b; return a; } Fps operator/(Fps a, Fp b) { return std::move(a /= b); } Fps operator*(const Fps& a, const Fps& b) { Fps res = atcoder::convolution(a, b); res.resize(std::max(sz(a), sz(b))); return res; } Fps& operator*=(Fps& a, const Fps& b) { return a = a * b; } Fps inv(const Fps& a) { Fps res{a[0].inv()}; for (res.reserve(sz(a)); sz(res) < sz(a);) { res.resize(std::min(2 * sz(res), sz(a))); res *= Fps{2} - Fps(a.begin(), a.begin() + sz(res)) * res; } return res; } Fps& operator/=(Fps& a, const Fps& b) { return a *= inv(b); } Fps operator/(Fps a, const Fps& b) { return std::move(a /= b); } using Poly = std::vector<Fp>; Poly mul(const Poly& a, const Poly& b) { return atcoder::convolution(a, b); } Poly mid_prod(const Poly& a, const Poly& b) { assert(sz(a) >= sz(b) && !b.empty()); if (std::min(sz(b), sz(a) - sz(b) + 1) <= 60) { Poly res(sz(a) - sz(b) + 1); for (int i = 0; i < sz(res); ++i) res[i] = std::inner_product(b.begin(), b.end(), a.begin() + i, Fp(0)); return res; } int n = 1 << std::__lg(2 * sz(a) - 1); std::vector<Fp> fa(n), fb(n); std::copy(a.begin(), a.end(), fa.begin()); std::copy(b.rbegin(), b.rend(), fb.begin()); atcoder::internal::butterfly(fa); atcoder::internal::butterfly(fb); for (int i = 0; i < n; ++i) fa[i] *= fb[i]; atcoder::internal::butterfly_inv(fa); fa.resize(sz(a)); fa.erase(fa.begin(), fa.begin() + (sz(b) - 1)); fa /= n; return fa; } struct Tree { int m; std::vector<Poly> f; explicit Tree(const std::vector<Fp>& x) : m(sz(x)), f(2 << std::__lg(2 * m - 1)) { for (int i = 0; i < size(); ++i) f[size() + i] = {i < m ? x[i] : 0}; for (int i = size(); i-- > 1;) { f[i].resize(2 * sz(f[2 * i])); f[i] += f[2 * i] + f[2 * i + 1]; Poly prod = mul(f[2 * i], f[2 * i + 1]); for (int j = 0; j < sz(prod); ++j) f[i][j + 1] -= prod[j]; } } int size() const { return f.size() / 2; } Poly get(int i) const { Poly res(sz(f[i]) + 1); for (int j = 0; j < sz(res); ++j) res[j] = j ? -f[i][j - 1] : 1; return res; } std::vector<Fp> power_sum(const std::vector<Fp>& w, int n) { assert(sz(w) == m); std::vector<Poly> t(2 * size()); for (int i = 0; i < size(); ++i) t[size() + i] = {i < m ? w[i] : 0}; for (int i = size(); i-- > 1;) t[i] = mul(t[2 * i], get(2 * i + 1)) + mul(t[2 * i + 1], get(2 * i)); Fps num = t[1], den = get(1); num.resize(n), den.resize(n); return num / den; } std::vector<Fp> multi_eval(Poly a) { int n = sz(a); a.resize(2 * n - 1); std::vector<Poly> t(2 * size()); Fps den = get(1); den.resize(n); t[1] = mid_prod(a, inv(den)); t[1].resize(size()); for (int i = 1; i < size(); ++i) { t[2 * i] = mid_prod(t[i], get(2 * i + 1)); t[2 * i + 1] = mid_prod(t[i], get(2 * i)); } std::vector<Fp> res(m); for (int i = 0; i < m; ++i) res[i] = t[size() + i][0]; return res; } Poly interpolate(const std::vector<Fp>& y) { assert(sz(y) == m); Poly a(m); for (int i = 1; i < m; ++i) a[i - 1] = -f[1][m - i - 1] * i; a.back() = m; a = multi_eval(a); std::vector<Poly> t(2 * size()); for (int i = 0; i < size(); ++i) t[size() + i] = {i < m ? y[i] / a[i] : 0}; for (int i = size(); i-- > 1;) t[i] = mul(t[2 * i], get(2 * i + 1)) + mul(t[2 * i + 1], get(2 * i)); t[1].resize(m); std::reverse(t[1].begin(), t[1].end()); return t[1]; } }; int main() { using namespace std; cin.tie(nullptr)->sync_with_stdio(false); int n; cin >> n; std::vector<Fp> x(n), y(n); for (auto&& e : x) cin >> e; for (auto&& e : y) cin >> e; Tree t(x); Poly c = t.interpolate(y); for (int i = 0; i < n; ++i) cout << c[i] << " \n"[i + 1 == n]; }