1. 程式人生 > >UOJ 34 FFT

UOJ 34 FFT

tar span type amp cnblogs print col div uoj

鏈接:

http://uoj.ac/problem/34

代碼:

31 #include <complex>
32 typedef complex<double> E;
33 E a[MAXN], b[MAXN];
34 int n, m;
35 
36 namespace FFT {
37     const double Pi = acos(-1);
38     int rev[MAXN], L;
39     void DFT(E *a, int f) {
40         for (int i = 0; i < n; i++) if (i < rev[i]) swap(a[i], a[rev[i]]);
41 for (int i = 1; i < n; i <<= 1) { 42 E wn(cos(Pi / i), f*sin(Pi / i)); 43 for (int p = i << 1, j = 0; j < n; j += p) { 44 E w(1, 0); 45 for (int k = 0; k < i; k++, w *= wn) { 46 E x = a[j + k], y = w*a[j + k + i];
47 a[j + k] = x + y; a[j + k + i] = x - y; 48 } 49 } 50 } 51 if (f == -1) for (int i = 0; i < n; i++) a[i] /= n; 52 } 53 void main() { 54 m = n + m; 55 for (n = 1; n <= m; n <<= 1) L++; 56 for
(int i = 0; i < n; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (L - 1)); 57 DFT(a, 1); DFT(b, 1); 58 for (int i = 0; i < n; i++) a[i] = a[i] * b[i]; 59 DFT(a, -1); 60 } 61 } 62 63 int main() { 64 cin >> n >> m; 65 int x; 66 rep(i, 0, n + 1) scanf("%d", &x), a[i] = x; 67 rep(i, 0, m + 1) scanf("%d", &x), b[i] = x; 68 FFT::main(); 69 for (int i = 0; i <= m; i++) printf("%d ", (int)(a[i].real() + 0.5)); 70 return 0; 71 }

UOJ 34 FFT