0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 15:16:28 +00:00

#108. 多项式乘法

https://loj.ac/s/1647241
This commit is contained in:
Baoshuo Ren 2022-12-02 16:42:58 +08:00
parent 3ea3640209
commit 013b0d96ca
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

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