fft模板

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath> 
 4 #include <complex>
 5 using namespace std;
 6 const int N = 4e6 + 5;
 7 const double pi = acos(-1);
 8 typedef complex<double> cd;
 9 int n, m, l, r[N];
10 cd a[N], b[N];
11 void fft(cd *c, int type){
12     for(int i = 0; i < n; i++)
13     if(i < r[i]) swap(c[i], c[r[i]]);
14     for(int i = 1; i < n; i <<= 1){
15         cd x(cos(pi / i), type * sin(pi / i));
16         for(int j = 0; j < n; j += (i << 1)){
17             cd y(1, 0);
18             for(int k = 0; k < i; k++, y *= x){
19                 cd p = c[j + k], q = y * c[i + j + k];
20                 c[j + k] = p + q, c[i + j + k] = p - q;
21             }
22         }
23     }
24 }
25 
26 inline void init(){
27     scanf("%d %d", &n, &m);
28     for(int i = 0; i <= n; i++) {
29         int x; scanf("%d", &x);
30         a[i] = x;
31     }
32     for(int i = 0; i <= m; i++) {
33         int x; scanf("%d", &x);
34         b[i] = x;
35     }
36     for(m += n, n = 1; n <= m; n <<= 1) l++;
37 }
38 
39 inline void cal(){
40     for(int i = 0; i < n; i++)
41         r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
42 }
43 
44 int main(){
45     init();
46     cal();
47     fft(a, 1); fft(b, 1);
48     for(int i = 0; i <= n; i++) a[i] *= b[i];
49     fft(a, -1);
50     for(int i = 0; i <= m; i++)
51         printf("%d ", (int)(a[i].real() / n + 0.5));
52     return 0;    
53 }
fft

 

posted @ 2018-07-05 07:12  hjmmm  阅读(119)  评论(0编辑  收藏  举报