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

第三周 7.24-7.30

时间:2016-07-24 10:33:49      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

7.24

HDU 1402 A * B Problem Plus

抄板。

技术分享
  1 // HDU 1402 A * B Problem Plus
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cmath>
  5 #include <cstring>
  6 #include <algorithm>
  7 using namespace std;
  8 const int maxn = 5e4 + 10;
  9 int a[maxn], b[maxn], c[maxn<<2];
 10 char s1[maxn], s2[maxn];
 11 
 12 // FFT
 13 const double Pi = acos(-1.0);
 14 
 15 struct complex
 16 {
 17     double r, i;
 18     complex(double r = 0, double i = 0): r(r), i(i) {}
 19     complex operator + (const complex &ot) const
 20     {
 21         return complex(r + ot.r, i + ot.i);
 22     }
 23     complex operator - (const complex &ot) const
 24     {
 25         return complex(r - ot.r, i - ot.i);
 26     }
 27     complex operator * (const complex &ot) const
 28     {
 29         return complex(r * ot.r - i * ot.i, r * ot.i + i * ot.r);
 30     }
 31 } x1[maxn<<2], x2[maxn<<2];
 32 
 33 void change(complex * y, int len)
 34 {
 35     for(int i = 1, j = len >> 1; i < len - 1; i++)
 36     {
 37         if(i < j) swap(y[i], y[j]);
 38         int k = len >> 1;
 39         while(j >= k) j -= k, k >>= 1;
 40         j += k;
 41     }
 42 }
 43 
 44 void FFT(complex * y, int len, int on)
 45 {
 46     change(y, len);
 47     for(int h = 2; h <= len; h <<= 1)
 48     {
 49         complex wn = complex(cos(on * 2 * Pi / h), sin(on * 2 * Pi / h));
 50         for(int j = 0; j < len; j += h)
 51         {
 52             complex w = complex(1, 0);
 53             for(int k = j; k < j + h / 2; k++)
 54             {
 55                 complex u = y[k];
 56                 complex t = y[k+h/2] * w;
 57                 y[k] = u + t;
 58                 y[k+h/2] = u - t;
 59                 w = w * wn;
 60             }
 61         }
 62     }
 63     if(on == -1)
 64     {
 65         for(int i = 0; i < len; i++)
 66         {
 67             y[i].r /= len;
 68         }
 69     }
 70 }
 71 
 72 void cal(int * a, int * b, int * c, int l)
 73 {
 74     int len = 1;
 75     while(len < l * 2) len <<= 1;
 76     for(int i = 0; i < l; i++)
 77     {
 78         x1[i] = complex(a[i], 0);
 79         x2[i] = complex(b[i], 0);
 80     }
 81     for(int i = l; i < len; i++) x1[i] = x2[i] = complex(0, 0);
 82     FFT(x1, len, 1);
 83     FFT(x2, len, 1);
 84     for(int i = 0; i < len; i++) x1[i] = x1[i] * x2[i];
 85     FFT(x1, len, -1);
 86     for(int i = 0; i < len; i++)
 87         c[i] = x1[i].r + 0.5;
 88 }
 89 
 90 int main(void)
 91 {
 92     while(~scanf("%s %s", s1, s2))
 93     {
 94         int l1 = strlen(s1), l2 = strlen(s2);
 95         int l = max(l1, l2);
 96         for(int i = 0; i < l; i++)
 97         {
 98             a[i] = l1 - i - 1 >= 0 ? s1[l1-i-1] - 0 : 0;
 99             b[i] = l2 - i - 1 >= 0 ? s2[l2-i-1] - 0 : 0;
100         }
101 
102         memset(c, 0, sizeof(c));
103         cal(a, b, c, l);
104         for(int i = 0; i < l1 + l2; i++)
105         {
106             c[i+1] += c[i] / 10;
107             c[i] %= 10;
108         }
109 
110         int st = 0;
111         for(int i = l1 + l2; i >= 0; i--)
112         {
113             if(!st && !c[i] && i) continue;
114             st = 1;
115             printf("%d", c[i]);
116         }
117         puts("");
118     }
119     return 0;
120 }
Aguin

 

第三周 7.24-7.30

标签:

原文地址:http://www.cnblogs.com/Aguin/p/5700190.html

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