贴过来做个模板。
C为复数类,newArr和at为内存池。
void FFT(int n, C *x) { C W = C(cos(2 * M_PI / n), sin(2 * M_PI / n)), w = C(1), *u = newArr(n >> 1), *v = newArr(n >> 1); for (int i = 0; i < n >> 1; ++i) u[i] = x[i << 1], v[i] = x[i << 1 | 1]; if (n > 1) FFT(n >> 1, u), FFT(n >> 1, v); for (int i = 0; i < n >> 1; ++i, w *= W) x[i] = u[i] + w * v[i], x[i + (n >> 1)] = u[i] - w * v[i]; at -= n + 5; }