hdu 1402 A * B Problem Plus fft

题目链接

A * B Problem Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16382    Accepted Submission(s): 3325


Problem Description
Calculate A * B.
 

 

Input
Each line will contain two integers A and B. Process to end of file.

Note: the length of each integer will not exceed 50000.
 

 

Output
For each case, output A * B in one line.
 

 

Sample Input
1 2 1000 2
 

 

Sample Output
2 2000
 
 
fft模板题。
  1 #include <iostream>
  2 #include <vector>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <cmath>
  7 #include <map>
  8 #include <set>
  9 #include <string>
 10 #include <queue>
 11 #include <stack>
 12 #include <bitset>
 13 using namespace std;
 14 #define pb(x) push_back(x)
 15 #define ll long long
 16 #define mk(x, y) make_pair(x, y)
 17 #define lson l, m, rt<<1
 18 #define mem(a) memset(a, 0, sizeof(a))
 19 #define rson m+1, r, rt<<1|1
 20 #define mem1(a) memset(a, -1, sizeof(a))
 21 #define mem2(a) memset(a, 0x3f, sizeof(a))
 22 #define rep(i, n, a) for(int i = a; i<n; i++)
 23 #define fi first
 24 #define se second
 25 typedef pair<int, int> pll;
 26 const double PI = acos(-1.0);
 27 const double eps = 1e-8;
 28 const int mod = 1e9+7;
 29 const int inf = 1061109567;
 30 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
 31 struct complex
 32 {
 33     double r,i;
 34     complex(double _r = 0.0,double _i = 0.0)
 35     {
 36         r = _r; i = _i;
 37     }
 38     complex operator +(const complex &b)
 39     {
 40         return complex(r+b.r,i+b.i);
 41     }
 42     complex operator -(const complex &b)
 43     {
 44         return complex(r-b.r,i-b.i);
 45     }
 46     complex operator *(const complex &b)
 47     {
 48         return complex(r*b.r-i*b.i,r*b.i+i*b.r);
 49     }
 50 };
 51 void change(complex y[],int len)
 52 {
 53     int i,j,k;
 54     for(i = 1, j = len/2;i < len-1; i++)
 55     {
 56         if(i < j)swap(y[i],y[j]);
 57         k = len/2;
 58         while( j >= k)
 59         {
 60             j -= k;
 61             k /= 2;
 62         }
 63         if(j < k) j += k;
 64     }
 65 }
 66 void fft(complex y[],int len,int on)
 67 {
 68     change(y,len);
 69     for(int h = 2; h <= len; h <<= 1)
 70     {
 71         complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
 72         for(int j = 0;j < len;j+=h)
 73         {
 74             complex w(1,0);
 75             for(int k = j;k < j+h/2;k++)
 76             {
 77                 complex u = y[k];
 78                 complex t = w*y[k+h/2];
 79                 y[k] = u+t;
 80                 y[k+h/2] = u-t;
 81                 w = w*wn;
 82             }
 83         }
 84     }
 85     if(on == -1)
 86         for(int i = 0;i < len;i++)
 87             y[i].r /= len;
 88 }
 89 const int maxn = 200010;
 90 int ans[maxn];
 91 complex x1[maxn], x2[maxn];
 92 char s1[50005], s2[50005];
 93 int main()
 94 {
 95     while(~scanf("%s%s", s1, s2)) {
 96         int len1 = strlen(s1);
 97         int len2 = strlen(s2);
 98         mem(ans);
 99         int len = 1;
100         while(len<len1*2 || len<len2*2)
101             len<<=1;
102         for(int i = 0; i<len1; i++) {
103             x1[i] = complex(s1[len1-i-1]-'0', 0);
104         }
105         for(int i = len1; i<len; i++)
106             x1[i] = complex(0, 0);
107         for(int i = 0; i<len2; i++) {
108             x2[i] = complex(s2[len2-i-1]-'0', 0);
109         }
110         for(int i = len2; i<len; i++)
111             x2[i] = complex(0, 0);
112         fft(x1, len, 1);
113         fft(x2, len, 1);
114         for(int i = 0; i<len; i++)
115             x1[i] = x1[i]*x2[i];
116         fft(x1, len, -1);
117         for(int i = 0; i<len; i++)
118             ans[i] = (int)(x1[i].r+0.5);
119         for(int i = 0; i<len; i++) {
120             ans[i+1] += ans[i]/10;
121             ans[i]%=10;
122         }
123         len = len1+len2-1;
124         while(len>0&&ans[len]==0)
125             len--;
126         for(int i = len; i>=0; i--)
127             printf("%d", ans[i]);
128         cout<<endl;
129     }
130     return 0;
131 }

 

你可能感兴趣的:(hdu 1402 A * B Problem Plus fft)