迭代FFT
1 int reverse(int x, int len){ 2 int t = 0; 3 for (int i = 1; i < len; i <<= 1){ 4 t <<= 1; 5 if (x & i) t |= 1; 6 } 7 return t; 8 } 9 10 Complex A[200010]; 11 void FFT(Complex *a, int n, int DFT){ 12 for (int i = 0; i < n; ++i) A[reverse(i, n)] = a[i]; 13 for (int i = 2; i <= n; i <<= 1){ 14 Complex wn = Complex(cos(2 * pi / i), DFT * sin(2 * pi / i)); 15 for (int j = 0; j < n; j += i){ 16 Complex w = Complex(1, 0); 17 for (int k = 0; k < (i >> 1); ++k){ 18 Complex x = A[j + k]; 19 Complex y = w * A[j + k + (i >> 1)]; 20 A[j + k] = x + y; 21 A[j + k + (i >> 1)] = x - y; 22 w = w * wn; 23 } 24 } 25 } 26 if (DFT == -1) for (int i = 0; i < n; ++i) A[i] = A[i] / n; 27 for (int i = 0; i < n; ++i) a[i] = A[i]; 28 }