FFT 实际是 DFT 的一种快速实现方法
可以将多项式的乘法从 O(n^2) 优化到 O(nlogn)
暂时没有看到很好的科普文章、原理自行百度吧

#define L(x) (1 << (x)) const double PI = acos(-1.0); const int maxn = (1<<17) + (int)1e3; double ax[maxn], ay[maxn], bx[maxn], by[maxn]; int revv(int x, int bits) { int ret = 0; for (int i = 0; i < bits; i++){ ret <<= 1; ret |= x & 1; x >>= 1; } return ret; } void fft(double * a, double * b, int n, bool rev) { int bits = 0; while (1 << bits < n) ++bits; for (int i = 0; i < n; i++){ int j = revv(i, bits); if (i < j) swap(a[i], a[j]), swap(b[i], b[j]); } for (int len = 2; len <= n; len <<= 1){ int half = len >> 1; double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len); if (rev) wmy = -wmy; for (int i = 0; i < n; i += len){ double wx = 1, wy = 0; for (int j = 0; j < half; j++){ double cx = a[i + j], cy = b[i + j]; double dx = a[i + j + half], dy = b[i + j + half]; double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx; a[i + j] = cx + ex, b[i + j] = cy + ey; a[i + j + half] = cx - ex, b[i + j + half] = cy - ey; double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx; wx = wnx, wy = wny; } } } if (rev){ for (int i = 0; i < n; i++) a[i] /= n, b[i] /= n; } } int Convolution(int a[],int na,int b[],int nb,int ans[]) //两个数组求卷积,有时ans数组要开成long long { int len = max(na, nb), ln; for(ln=0; L(ln)<len; ++ln); len=L(++ln); for (int i = 0; i < len ; ++i){ if (i >= na) ax[i] = 0, ay[i] =0; else ax[i] = a[i], ay[i] = 0; } fft(ax, ay, len, 0); for (int i = 0; i < len; ++i){ if (i >= nb) bx[i] = 0, by[i] = 0; else bx[i] = b[i], by[i] = 0; } fft(bx, by, len, 0); for (int i = 0; i < len; ++i){ double cx = ax[i] * bx[i] - ay[i] * by[i]; double cy = ax[i] * by[i] + ay[i] * bx[i]; ax[i] = cx, ay[i] = cy; } fft(ax, ay, len, 1); for (int i = 0; i < len; ++i) ans[i] = (int)(ax[i] + 0.5); return len; } int Convolution_self(long long a[], int na, int ans[]) //自己跟自己求卷积,有时候ans数组要开成long long { int len = na, ln; for(ln = 0; L(ln) < na; ++ln); len=L(++ln); for(int i = 0; i < len; ++i){ if (i >= na) ax[i] = 0, ay[i] = 0; else ax[i] = a[i], ay[i] = 0; } fft(ax, ay, len, 0); for(int i=0; i<len; ++i){ double cx = ax[i] * ax[i] - ay[i] * ay[i]; double cy = 2 * ax[i] * ay[i]; ax[i] = cx, ay[i] = cy; } fft(ax, ay, len, 1); for(int i=0; i<len; ++i) ans[i] = ax[i] + 0.5; return len; }

#include<bits/stdc++.h> #define LL long long #define ULL unsigned long long #define scl(i) scanf("%lld", &i) #define scll(i, j) scanf("%lld %lld", &i, &j) #define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k) #define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l) #define scs(i) scanf("%s", i) #define sci(i) scanf("%d", &i) #define scd(i) scanf("%lf", &i) #define scIl(i) scanf("%I64d", &i) #define scii(i, j) scanf("%d %d", &i, &j) #define scdd(i, j) scanf("%lf %lf", &i, &j) #define scIll(i, j) scanf("%I64d %I64d", &i, &j) #define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k) #define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k) #define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k) #define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l) #define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l) #define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l) #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define lowbit(i) (i & (-i)) #define mem(i, j) memset(i, j, sizeof(i)) #define fir first #define sec second #define VI vector<int> #define ins(i) insert(i) #define pb(i) push_back(i) #define pii pair<int, int> #define VL vector<long long> #define mk(i, j) make_pair(i, j) #define all(i) i.begin(), i.end() #define pll pair<long long, long long> #define _TIME 0 #define _INPUT 0 #define _OUTPUT 0 clock_t START, END; void __stTIME(); void __enTIME(); void __IOPUT(); using namespace std; #define L(x) (1 << (x)) const double PI = acos(-1.0); const int maxn = (1<<20) + (int)1e3; double ax[maxn], ay[maxn], bx[maxn], by[maxn]; int revv(int x, int bits) { int ret = 0; for (int i = 0; i < bits; i++){ ret <<= 1; ret |= x & 1; x >>= 1; } return ret; } void fft(double * a, double * b, int n, bool rev) { int bits = 0; while (1 << bits < n) ++bits; for (int i = 0; i < n; i++){ int j = revv(i, bits); if (i < j) swap(a[i], a[j]), swap(b[i], b[j]); } for (int len = 2; len <= n; len <<= 1){ int half = len >> 1; double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len); if (rev) wmy = -wmy; for (int i = 0; i < n; i += len){ double wx = 1, wy = 0; for (int j = 0; j < half; j++){ double cx = a[i + j], cy = b[i + j]; double dx = a[i + j + half], dy = b[i + j + half]; double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx; a[i + j] = cx + ex, b[i + j] = cy + ey; a[i + j + half] = cx - ex, b[i + j + half] = cy - ey; double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx; wx = wnx, wy = wny; } } } if (rev){ for (int i = 0; i < n; i++) a[i] /= n, b[i] /= n; } } int Convolution(int a[],int na,int b[],int nb,int ans[]) //两个数组求卷积,有时ans数组要开成long long { int len = max(na, nb), ln; for(ln=0; L(ln)<len; ++ln); len=L(++ln); for (int i = 0; i < len ; ++i){ if (i >= na) ax[i] = 0, ay[i] =0; else ax[i] = a[i], ay[i] = 0; } fft(ax, ay, len, 0); for (int i = 0; i < len; ++i){ if (i >= nb) bx[i] = 0, by[i] = 0; else bx[i] = b[i], by[i] = 0; } fft(bx, by, len, 0); for (int i = 0; i < len; ++i){ double cx = ax[i] * bx[i] - ay[i] * by[i]; double cy = ax[i] * by[i] + ay[i] * bx[i]; ax[i] = cx, ay[i] = cy; } fft(ax, ay, len, 1); for (int i = 0; i < len; ++i) ans[i] = (int)(ax[i] + 0.5); return len; } int Convolution_self(long long a[], int na, int ans[]) //自己跟自己求卷积,有时候ans数组要开成long long { int len = na, ln; for(ln = 0; L(ln) < na; ++ln); len=L(++ln); for(int i = 0; i < len; ++i){ if (i >= na) ax[i] = 0, ay[i] = 0; else ax[i] = a[i], ay[i] = 0; } fft(ax, ay, len, 0); for(int i=0; i<len; ++i){ double cx = ax[i] * ax[i] - ay[i] * ay[i]; double cy = 2 * ax[i] * ay[i]; ax[i] = cx, ay[i] = cy; } fft(ax, ay, len, 1); for(int i=0; i<len; ++i) ans[i] = ax[i] + 0.5; return len; } int a[maxn], b[maxn], c[maxn]; int main(void){__stTIME();__IOPUT(); int n, m; scii(n, m); for(int i=0; i<=n; i++) sci(a[i]); for(int i=0; i<=m; i++) sci(b[i]); int len = Convolution(a, n+1, b, m+1, c); for(int i=0; i<n+m+1; i++) printf("%d ", c[i]); puts(""); __enTIME();return 0;} void __stTIME() { #if _TIME START = clock(); #endif } void __enTIME() { #if _TIME END = clock(); cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl; #endif } void __IOPUT() { #if _INPUT freopen("in.txt", "r", stdin); #endif #if _OUTPUT freopen("out.txt", "w", stdout); #endif }