FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FFT(快速傅立叶变换):HDU 1402 A * B Problem Plus相关的知识,希望对你有一定的参考价值。
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.
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的主要内容,如果未能解决你的问题,请参考以下文章
HDU - 1402 A * B Problem Plus FFT裸题