[原创]HDU 1402 A * B Problem Plus [FFT]【数论】

2017-01-13 23:52:02 Tabris_ 阅读数:636


博客爬取于2020-06-14 22:42:13
以下为正文

版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/54413679


题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402
---------------------------------------------------------------------------------.
A * B Problem Plus

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

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
---------------------------------------------------------------------------------.

就是个FFT入门题 。
因为题目给的字符串长度达到了5000050000,如果普通的大数乘法复杂度为O(lenAlenB)O(lenA*lenB)必定会超时.
FFT其实就是一个快速求取卷积的过程.
而一个乘法计算其实也就是一个卷积
对于个数a0a1a2a3...ana_0a_1a_2a_3...a_n
也就是A(x=10)=a0x0+a1x1+a2x2+a3x3+...anxnA(x=10)=a_0*x^0+a_1*x^1+a_2*x^2+a_3*x^3+...a_n*x^n
所以这个题就可以用FFT来计算了。。
复杂度能降到O(Nlog2N)O(Nlog_2N)

附本题代码
----------------------------------------------------------------.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# include <bits/stdc++.h>
using namespace std;
const int N = 50000+5;
const double PI = acos(-1.0);
/***********************************************************************/
struct Complex{
double real, image;
Complex(double _real, double _image){
real = _real;
image = _image;
}
Complex(){}
};

Complex *AA = new Complex[N<<2],*BB = new Complex[N<<2];
Complex operator + (const Complex &c1, const Complex &c2){
return Complex(c1.real + c2.real, c1.image + c2.image);
}
Complex operator - (const Complex &c1, const Complex &c2){
return Complex(c1.real - c2.real, c1.image - c2.image);
}
Complex operator * (const Complex &c1, const Complex &c2){
return Complex(c1.real*c2.real - c1.image*c2.image, c1.real*c2.image + c1.image*c2.real);
}

int rev(int id, int len){
int ret = 0;
for(int i = 0; (1 << i) < len; i++){
ret <<= 1;
if(id & (1 << i)) ret |= 1;
}
return ret;
}
Complex A[N<<2];
void FFT(Complex *a, int len, int DFT)
{
for(int i = 0; i < len; i++)
A[rev(i, len)] = a[i];
for(int s = 1; (1 << s) <= len; s++)
{
int m = (1 << s);
Complex wm = Complex(cos(DFT*2*PI/m), sin(DFT*2*PI/m));
for(int k = 0; k < len; k += m)
{
Complex w = Complex(1, 0);
for(int j = 0; j < (m >> 1); j++)
{
Complex t = w*A[k + j + (m >> 1)];
Complex u = A[k + j];
A[k + j] = u + t;
A[k + j + (m >> 1)] = u - t;
w = w*wm;
}
}
}
if(DFT == -1) for(int i = 0; i < len; i++) A[i].real /= len, A[i].image /= len;
for(int i = 0; i < len; i++) a[i] = A[i];
return;
}

char a[N],b[N];
int ans[N<<2];

int main(){
while(~scanf("%s",a)){
scanf("%s",b);
int la = strlen(a);
int lb = strlen(b);

int sa,sb;
sa=sb=0;
while((1<<sa)<la) sa++;
while((1<<sb)<lb) sb++;
int len = (1<<(max(sa,sb)+1));
for(int i=0;i<len;i++){
AA[i]=Complex(((i<la)?(a[la-i-1]-'0'):0),0);
BB[i]=Complex(((i<lb)?(b[lb-i-1]-'0'):0),0);
}
FFT(AA,len,1);
FFT(BB,len,1);
for(int i=0;i<len;i++) AA[i]=AA[i]*BB[i],ans[i]=0;
FFT(AA,len,-1);
for(int i=0;i<len ;i++) ans[i]+=(int)(AA[i].real+0.5);
for(int i=0;i<len-1;i++) ans[i+1]+=ans[i]/10,ans[i]%=10;
bool flag = false;
for(int i=len-1;i>=0;--i){
if(ans[i]) printf("%d",ans[i]),flag=true;
else if(flag||0==i) printf("0");
}
puts("");
}
return 0;
}