FFT(快速傅里叶变换)
新写的FFT,写它的目的可不是为了高精。
#pragma GCC optimize("Ofast,fast-math,unroll-loops")
#include <bits/stdc++.h>
#define N 4000005
using namespace std;
double Pi = acos(-1);
struct cplx {
double x, y;
cplx operator+ (const cplx &b) {
return cplx{x + b.x, y + b.y};
}
cplx operator- (const cplx &b) {
return cplx{x - b.x, y - b.y};
}
cplx operator* (const cplx &b) {
return cplx{x * b.x - y * b.y, x * b.y + y * b.x};
}
} a[N], b[N], ans[N << 1], t[N], wn[50];
void FFT(cplx *f, cplx *t, int d, bool dft) {
if(!~d) return;
int len = 1 << d;
memcpy(t, f, sizeof(cplx) * (len << 1));
for(int i = 0;i < len;i++)
f[i] = t[i << 1], f[i + len] = t[i << 1 | 1];
FFT(f, t, d - 1, dft);
FFT(f + len, t + len, d - 1, dft);
register cplx nw = cplx{1, 0}, nwp = wn[d], fr;
if(!dft) nwp.y = -nwp.y;
for(int i = 0;i < len;i++) {
fr = nw * f[i + len];
t[i] = f[i] + fr;
t[i + len] = f[i] - fr;
nw = nw * nwp;
}
memcpy(f, t, sizeof(cplx) * (len << 1));
}
int read() {
register char c(getchar());
while(!isdigit(c)) c = getchar();
return c ^ 48;
}
int main() {
int n, m, nm, pm = 0;
scanf("%d%d", &n, &m);
for(int i = 0;i <= n;i++) a[i].x = read();
for(int i = 0;i <= m;i++) b[i].x = read();
nm = n + m;
while((1 << pm) <= nm) pm++;
for(int i = 1;i <= pm;i++)
wn[i] = cplx{cos(Pi / (1 << i)), sin(Pi / (1 << i))};
FFT(a, t, pm - 1, 1);
FFT(b, t, pm - 1, 1);
for(int i = 0;i <= (1 << pm);i++)
ans[i] = a[i] * b[i];
FFT(ans, t, pm - 1, 0);
for(int i = 0;i <= n + m;i++)
printf("%d ", (int)(fabs(ans[i].x) / (1 << pm) + 0.49));
return 0;
}