From cde051aa863f4b303b1163ecde61c51d8556b835 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 1 Dec 2022 22:21:58 +0800 Subject: [PATCH] =?UTF-8?q?P4238=20=E3=80=90=E6=A8=A1=E6=9D=BF=E3=80=91?= =?UTF-8?q?=E5=A4=9A=E9=A1=B9=E5=BC=8F=E4=B9=98=E6=B3=95=E9=80=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/96259440 --- Luogu/P4238/P4238.cpp | 129 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 Luogu/P4238/P4238.cpp diff --git a/Luogu/P4238/P4238.cpp b/Luogu/P4238/P4238.cpp new file mode 100644 index 00000000..63fa4f78 --- /dev/null +++ b/Luogu/P4238/P4238.cpp @@ -0,0 +1,129 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int mod = 998244353; + +constexpr long long binpow(long long a, long long b) { + a %= mod; + + long long res = 1; + + while (b) { + if (b & 1) res = res * a % mod; + a = a * a % mod; + b >>= 1; + } + + return res; +} + +std::vector number_theoretic_transform(std::vector a) { + // assert(a.size() == (1 << std::__lg(a.size()))); + int k = std::__lg(a.size()); + + for (int i = 0; i < a.size(); i++) { + int t = 0; + + for (int j = 0; j < k; j++) { + if (i & (1 << j)) { + t |= 1 << (k - j - 1); + } + } + + if (i < t) std::swap(a[i], a[t]); + } + + for (int len = 2; len <= a.size(); len <<= 1) { + int m = len >> 1; + long long wn = binpow(3, (mod - 1) / len); + + for (int i = 0; i < a.size(); i += len) { + long long w = 1; + + for (int j = 0; j < m; j++) { + long long u = a[i + j], + v = a[i + j + m] * w % mod; + + a[i + j] = ((u + v) % mod + mod) % mod; + a[i + j + m] = ((u - v) % mod + mod) % mod; + w = w * wn % mod; + } + } + } + + return a; +} + +class Poly : public std::vector { + private: + public: + using std::vector::vector; + + Poly() = default; + + Poly(const std::vector &__v) + : std::vector(__v) {} + + Poly(std::vector &&__v) + : std::vector(std::move(__v)) {} +} poly; + +Poly inv(Poly a) { + if (a.size() == 1) return Poly{binpow(a[0], mod - 2)}; + + int n = a.size(), + k = 1 << (std::__lg(n << 1) + 1); + Poly b{a}; + + a.resize(k); + a = number_theoretic_transform(a); + + b.resize(n + 1 >> 1); + b = inv(b); + b.resize(k); + b = number_theoretic_transform(b); + + for (int i = 0; i < k; i++) { + b[i] = (2 - a[i] * b[i] % mod + mod) % mod * b[i] % mod; + } + + long long inv_k = binpow(k, mod - 2); + b = number_theoretic_transform(b); + std::transform(b.begin(), b.end(), b.begin(), [&](long long x) { + return x * inv_k % mod; + }); + std::reverse(b.begin() + 1, b.end()); + b.resize(n); + + return b; +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + + cin >> n; + + Poly f(n); + + for (int i = 0; i < n; i++) { + cin >> f[i]; + } + + auto g = inv(f); + + for (int i = 0; i < n; i++) { + cout << g[i] << ' '; + } + + cout << endl; + + return 0; +}