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): 12665    Accepted Submission(s): 2248

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

Author

DOOM III

快速傅里叶转换算法.....

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 using namespace std;
 6 #define N 50500*2
 7 const double PI=acos(-1.0);
 8 struct Vir
 9 {
10     double re,im;
11     Vir(double _re=0.,double _im=0.):re(_re),im(_im){}
12     Vir operator*(Vir r) { return Vir(re*r.re-im*r.im,re*r.im+im*r.re);}
13     Vir operator+(Vir r) { return Vir(re+r.re,im+r.im);}
14     Vir operator-(Vir r) { return Vir(re-r.re,im-r.im);}
15 };
16 void bit_rev(Vir *a,int loglen,int len)
17 {
18     for(int i=0;i<len;++i)
19     {
20         int t=i,p=0;
21         for(int j=0;j<loglen;++j)
22         {
23             p<<=1;
24             p=p|(t&1);
25             t>>=1;
26         }
27         if(p<i)
28         {
29             Vir temp=a[p];
30             a[p]=a[i];
31             a[i]=temp;
32         }
33     }
34 }
35 void FFT(Vir *a,int loglen,int len,int on)
36 {
37     bit_rev(a,loglen,len);
38
39     for(int s=1,m=2;s<=loglen;++s,m<<=1)
40     {
41         Vir wn=Vir(cos(2*PI*on/m),sin(2*PI*on/m));
42         for(int i=0;i<len;i+=m)
43         {
44             Vir w=Vir(1.0,0);
45             for(int j=0;j<m/2;++j)
46             {
47                 Vir u=a[i+j];
48                 Vir v=w*a[i+j+m/2];
49                 a[i+j]=u+v;
50                 a[i+j+m/2]=u-v;
51                 w=w*wn;
52             }
53         }
54     }
55     if(on==-1)
56     {
57         for(int i=0;i<len;++i){
58           a[i].re/=len;
59           a[i].im/=len;
60         }
61     }
62 }
63 char a[N*2],b[N*2];
64 Vir pa[N*2],pb[N*2];
65 int ans[N*2];
66 int main ()
67 {
68     while(scanf("%s%s",a,b)!=EOF)
69     {
70         int lena=strlen(a);
71         int lenb=strlen(b);
72         int n=1,loglen=0;
73         while(n<lena+lenb) n<<=1,loglen++;
74         for(int i=0,j=lena-1;i<n;++i,--j)
75             pa[i]=Vir(j>=0?a[j]-‘0‘:0.,0.);
76         for(int i=0,j=lenb-1;i<n;++i,--j)
77             pb[i]=Vir(j>=0?b[j]-‘0‘:0.,0.);
78          memset(ans,0,sizeof(int)*(n+1));
79         FFT(pa,loglen,n,1);
80         FFT(pb,loglen,n,1);
81         for(int i=0;i<n;++i)
82             pa[i]=pa[i]*pb[i];
83         FFT(pa,loglen,n,-1);
84
85         for(int i=0;i<n;++i)
86             ans[i]=pa[i].re+0.5;
87         for(int i=0;i<n;++i)
88         {
89              ans[i+1]+=ans[i]/10;
90              ans[i]%=10;
91         }
92         int pos=lena+lenb-1;
93         for(;pos>0&&ans[pos]<=0;--pos) ;
94         for(;pos>=0;--pos)
95             printf("%d",ans[pos]);
96         puts("");
97     }
98     return 0;
99 }

时间: 2024-10-13 19:18:02

hdu----(1402)A * B Problem Plus(FFT模板)的相关文章

HDU - 1402 A * B Problem Plus FFT裸题

http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟的大数或者压位的大数是无法胜任这种计算的.... 然后,2个大整数相乘,可以理解为卷积,所以就用快速傅里叶变换(FFT)来加速他 模板题 简单总结一下对FFT的认知: FFT用于算卷积,卷积可以理解为两个多项式相乘显然复杂度是$O(n^2)$的 但是fft可以优化为$O(nlogn)$如何优化,考虑

HDU 1402 A * B Problem Plus FFT

A * B Problem Plus 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

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. Output For each case, output A * B in one line. Sample Input 1 2 1000 2 Sample Output 2 2000 唉,模板题,膜的邝

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): 13456    Accepted Submission(s): 2401 Problem Description Calculate A * B. Input Each line will contain two integers A and B.

HDU 1402 A * B Problem Plus ——(大数乘法,FFT)

因为刚学fft,想拿这题练练手,结果WA了个爽= =. 总结几点犯的错误: 1.要注意处理前导零的问题. 2.一定要注意数组大小的问题.(前一个fft的题因为没用到b数组,所以b就没管,这里使用了b数组,结果忘记给其大小乘以4倍了) 代码如下: 1 #include<bits/stdc++.h> 2 using namespace std; 3 const double pi = atan(1.0)*4; 4 const int N = 5e4 + 5; 5 typedef long long

HDU 1402:A * B Problem Plus

A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16932    Accepted Submission(s): 3558 Problem Description Calculate A * B. Input Each line will contain two integers A and B.

HDU 1402 A * B Problem Plus(快速傅里叶变换)

题意:两个数相乘,每个数的长度不超过10^5: 思路:FFT第一题.通过将系数表达式转换为点值表达式,降低复杂度:算导是个好东西!!! 用DFT实现单位复根计算点值表达式,逆DFT则将点值表达式转为系数表达式,即计算插值:复杂度均为O(n^2); FFT采用分治的思想,将奇偶分开处理,优化DFT:复杂度为O(nlogn); FFT处理时,(将点值扩展到2^n)->(计算点值)->(点乘运算)->(计算插值); 由于奇偶分治时结果的位置发生变化,需要在傅里叶变换时进行二进制位置互换: 每经

fft模板 HDU 1402

1 // fft模板 HDU 1402 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <math.h> 9 #include <memory.h> 10 #include <bits/stdc++.h> 11 using

A * B Problem Plus HDU - 1402 (FFT)

A * B Problem Plus HDU - 1402 第一道FFT... 1 #include <iostream> 2 #include <complex> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 9 const int maxn = 200010; 1

hdu 1402 大数A*B模板(FFT)

hdu 1402 大数A*B模板(FFT) 题目链接 参考博客 #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <complex> #include <iostream> using namespace std; typedef long long ll; template <class T> void