#include<bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int maxn = 300000 + 10;
int n, m, base;
int c[maxn], rev[maxn];
complex<double> a[maxn], b[maxn], p[maxn];
int read() {
int n = 0, f = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
while(ch >='0' && ch <='9') { n = n * 10 + ch - 48; ch = getchar(); }
return n * f;
}
void init() {
int dis = 0;
for(base = 1; base <= n + m; base <<= 1) ++dis;
for(int i=0; i<=base; i++)
rev[i] = (rev[i>>1] >> 1) | ((i & 1) << (dis - 1));
}
void FFT(complex<double> x[], int n, int type) {
for(int i=0; i<n; ++i)
if(i < rev[i]) swap(x[i], x[rev[i]]);
for(int s=1; 1 << s <= n; ++s) {
int m = 1 << s;
complex<double> wm(cos(type * 2 * PI / m), sin(type * 2 * PI / m));
for(int k=0; k < n; k += m) {
complex<double> w(1, 0);
for(int j=0; j < m >> 1; ++j) {
complex<double> u = x[k + j];
complex<double> t = x[k + j + m / 2] * w;
x[k + j] = u + t;
x[k + j + m / 2] = u - t;
w *= wm;
}
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.txt", "r", stdin);
freopen("ans.txt", "w", stdout);
#endif
scanf("%d%d", &n, &m);
for(int i=0; i<=n; ++i) a[i] = read();
for(int i=0; i<=m; ++i) b[i] = read();
init();
FFT(a, base, 1); FFT(b, base, 1);
for(int i=0; i<base; ++i) p[i] = a[i] * b[i]; FFT(p, base, -1);
for(int i=0; i<n + m + 1; ++i) c[i] = p[i].real() / base + 0.1;
for(int i=0; i<n + m + 1; ++i) printf("%d%c", c[i], i == n + m ? '\n' : ' ');
return 0;
}
Radix-2 Iterative-FFT
最新推荐文章于 2024-04-06 18:42:36 发布