【FFT】 HDU 1402 A * B Problem Plus

通道:http://acm.hdu.edu.cn/showproblem.php?pid=1402

题意:计算A*B

代码:

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 typedef long long ll;
 9 
10 const int MAX_N = 100007; 
11 const int MAX_M = 100007;
12 const long double PI = acos(-1.0);
13 
14 struct Complex {
15     long double r, i;
16     Complex(long double _r, long double _i) {
17         r = _r;
18         i = _i;
19     }
20     Complex operator + (const Complex &c) {
21         return Complex(c.r + r, c.i + i);
22     }
23     Complex operator - (const Complex &c) {
24         return Complex(r - c.r, i - c.i);
25     }
26     Complex operator * (const Complex &c) {
27         return Complex(c.r * r - c.i * i, c.r * i + c.i * r);
28     }
29     Complex operator / (const int &c) {
30         return Complex(r / c, i / c);
31     }
32     Complex(){}
33 };
34 namespace FFT {
35     int rev(int id, int len) {
36         int ret = 0;
37         for(int i = 0; (1 << i) < len; ++i) {
38             ret <<= 1;
39             if(id & (1 << i)) ret |= 1;
40         }
41         return ret;
42     }
43     Complex A[MAX_M << 3];
44     void FFT(Complex *a, int len, int DFT) {
45         for(int i = 0; i < len; ++i) A[rev(i, len)] = a[i];
46         for(int s = 1; (1 << s) <= len; ++s) {
47             int m = (1 << s);
48             Complex wm = Complex(cos(PI * DFT * 2 / m), sin(PI * DFT * 2 / m));
49             for(int k = 0; k < len; k += m) {
50                 Complex w = Complex(1, 0);
51                 for(int j = 0; j < (m >> 1); j++) {
52                     Complex t = w * A[k + j + (m >> 1)];
53                     Complex u = A[k + j];
54                     A[k + j] = u + t;
55                     A[k + j + (m >> 1)] = u - t;
56                     w = w * wm;
57                 }
58             }
59         }
60         if(DFT == -1) for(int i = 0; i < len; ++i) A[i] = A[i] / len;
61         for(int i = 0; i < len; i++) a[i] = A[i];
62     }
63 };
64 
65 char a[MAX_M], b[MAX_M];
66 Complex X[MAX_M << 2], Y[MAX_M << 2];
67 int res[MAX_M << 2];
68 
69 int main() {
70     while (2 == scanf("%s%s", a, b)) {
71         int la = strlen(a), lb = strlen(b);
72         for (int i = 0; i < la; ++i) X[i] = Complex(a[la - i - 1] - '0', 0);
73         for (int i = 0; i < lb; ++i) Y[i] = Complex(b[lb - i - 1] - '0', 0);
74         int len = 1;
75         while (len < la || len < lb) len <<= 1; len <<= 1;
76         for (int i = la; i < len; ++i) X[i] = Complex(0, 0);
77         for (int i = lb; i < len; ++i) Y[i] = Complex(0, 0);
78         FFT::FFT(X, len, 1); FFT::FFT(Y, len, 1);
79         for (int i = 0; i < len; ++i) X[i] = X[i] * Y[i];
80         FFT::FFT(X, len, -1);
81         for (int i = 0; i < len; ++i) res[i] = (int)(X[i].r + 0.5);
82         for (int i = 0; i < len; ++i) {
83             res[i + 1] += res[i] / 10;
84             res[i] %= 10;
85         }
86         len = la + lb - 1;
87         while (res[len] <= 0 && len > 0) --len;
88         for (int i = len; i >= 0; --i) printf("%d", res[i]); puts("");
89     }
90     return 0;
91 }
View Code

 

posted @ 2015-07-24 16:06  mithrilhan  阅读(148)  评论(0编辑  收藏  举报