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

FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus

时间:2016-02-29 21:14:28      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

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
  
  唉,模板题,膜的邝斌的模板。
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 using namespace std;
  7 
  8 const double PI = acos(-1.0);
  9 
 10 struct complex{
 11     double r,i;
 12     complex(double r_=0.0,double i_=0.0)
 13     {
 14         r=r_;i=i_;
 15     }
 16     complex operator +(const complex &b)
 17     {
 18         return complex(r+b.r,i+b.i);
 19     }
 20     complex operator -(const complex &b)
 21     {
 22         return complex(r-b.r,i-b.i);
 23     }
 24     complex operator *(const complex &b)
 25     {
 26         return complex(r*b.r-i*b.i,i*b.r+r*b.i);
 27     }
 28 };
 29  
 30 void Rader(complex *a,int len)
 31 {
 32     int k;
 33     for(int i=1,j=len/2;i<len-1;i++)
 34     {
 35         if(i<j)swap(a[i],a[j]);
 36         k=len/2;
 37         while(j>=k)
 38         {
 39             j-=k;
 40             k>>=1;
 41         }
 42         j+=k;
 43     }
 44 }
 45 
 46 void FFT(complex *a,int len,int on)
 47 {
 48     Rader(a,len);
 49     for(int h=2;h<=len;h<<=1)
 50     {
 51         complex wn(cos(-on*2*PI/h),sin(-on*2*PI/h));
 52         for(int j=0;j<len;j+=h)
 53         {
 54             complex w(1,0);
 55             for(int k=j;k<j+h/2;k++)
 56             {
 57                 complex u=a[k];
 58                 complex v=a[k+h/2]*w;
 59                 a[k]=u+v;
 60                 a[k+h/2]=u-v;
 61                 w=w*wn;
 62             }
 63         } 
 64     }
 65     if(on==-1)
 66         for(int i=0;i<len;i++)
 67             a[i].r/=len;
 68 }
 69 
 70 const int maxn = 200010;
 71 complex Array1[maxn],Array2[maxn];
 72 char str1[maxn],str2[maxn];
 73 int sum[maxn],len,len1,len2;
 74 
 75 int main()
 76 {
 77     while(~scanf("%s%s",str1,str2))
 78     {
 79         len1=strlen(str1);
 80         len2=strlen(str2);    
 81         len=1;
 82         while(len<len1*2||len<len2*2)len<<=1;
 83         for(int i=0;i<len1;i++)
 84             Array1[i]=complex(str1[len1-i-1]-0,0);
 85         for(int i=0;i<len2;i++)
 86             Array2[i]=complex(str2[len2-i-1]-0,0);
 87         
 88         for(int i=len1;i<len;i++)
 89             Array1[i]=complex(0,0);
 90         for(int i=len2;i<len;i++)
 91             Array2[i]=complex(0,0);
 92         
 93         FFT(Array1,len,1);
 94         FFT(Array2,len,1);
 95         for(int i=0;i<len;i++)
 96             Array1[i]=Array1[i]*Array2[i];
 97         FFT(Array1,len,-1);
 98         memset(sum,0,sizeof(sum));    
 99         for(int i=0;i<len;i++){
100             sum[i]+=(int)(Array1[i].r+0.5);
101             sum[i+1]+=sum[i]/10;
102             sum[i]%=10;
103         }
104         int p=len;
105         while(!sum[p]&&p)p--;
106         for(;p!=-1;p--)
107             printf("%d",sum[p]);
108         printf("\n");                    
109     }
110     return 0;
111 }

 

FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus

标签:

原文地址:http://www.cnblogs.com/TenderRun/p/5228697.html

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