码迷,mamicode.com
首页 > 其他好文 > 详细

UOJ 34 FFT

时间:2017-05-19 01:00:38      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:dft   wap   code   链接   ble   ref   space   swa   print   

链接:

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

标签:dft   wap   code   链接   ble   ref   space   swa   print   

原文地址:http://www.cnblogs.com/baocong/p/6876270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!