diff --git a/LibreOJ/108/108.cpp b/LibreOJ/108/108.cpp index 96950668..8ae2b361 100644 --- a/LibreOJ/108/108.cpp +++ b/LibreOJ/108/108.cpp @@ -1,28 +1,16 @@ #include #include -#include +#include +#include +#include using std::cin; using std::cout; const char endl = '\n'; -const int mod = 998244353; +const double PI = std::acos(-1); -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; -} - -void number_theoretic_transform(std::valarray& a) { +void fast_fourier_transform(std::vector>& a) { if (a.size() == 1) return; // assert(a.size() == 1 << std::__lg(a.size())); @@ -41,25 +29,35 @@ void number_theoretic_transform(std::valarray& a) { } for (int len = 2; len <= a.size(); len <<= 1) { - int m = len >> 1; - - long long wlen = binpow(3, (mod - 1) / len); + std::complex wlen(std::cos(2 * PI / len), std::sin(2 * PI / len)); for (int i = 0; i < a.size(); i += len) { - long long w = 1; + std::complex w(1); - for (int j = 0; j < m; j++) { - long long u = a[i + j], - v = a[i + j + m] * w % mod; + for (int j = 0; j < len / 2; j++) { + std::complex u = a[i + j], + v = a[i + j + len / 2] * w; - a[i + j] = (u + v) % mod; - a[i + j + m] = ((u - v) % mod + mod) % mod; - w = w * wlen % mod; + a[i + j] = u + v; + a[i + j + len / 2] = u - v; + w *= wlen; } } } } +void dft(std::vector>& a) { + fast_fourier_transform(a); +} + +void idft(std::vector>& a) { + fast_fourier_transform(a); + std::reverse(a.begin() + 1, a.end()); + std::transform(a.begin(), a.end(), a.begin(), [&](std::complex x) { + return static_cast(std::round(x.real() / a.size())); + }); +} + int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); @@ -68,9 +66,8 @@ int main() { cin >> n >> m; - int k = 1 << (std::__lg(n + m) + 1), - inv = binpow(k, mod - 2); - std::valarray f(k), g(k); + int k = 1 << (std::__lg(n + m) + 1); + std::vector> f(k), g(k); for (int i = 0; i <= n; i++) { cin >> f[i]; @@ -80,18 +77,17 @@ int main() { cin >> g[i]; } - number_theoretic_transform(f); - number_theoretic_transform(g); + dft(f); + dft(g); for (int i = 0; i < k; i++) { f[i] *= g[i]; } - number_theoretic_transform(f); - std::reverse(std::begin(f) + 1, std::end(f)); + idft(f); for (int i = 0; i <= n + m; i++) { - cout << f[i] * inv % mod << ' '; + cout << static_cast(f[i].real()) << ' '; } cout << endl;