###### HDU 1402 A * B Problem Plus （FFT求高精度乘法）

#include <bits/stdc++.h>
#define LL long long
#define FOR(i,x,y)  for(int i = x;i < y;++ i)
#define IFOR(i,x,y) for(int i = x;i > y;-- i)

using namespace std;

//FFT copy from kuangbin
const double pi = acos (-1.0);
// Complex  z = a + b * i
struct Complex {
double a, b;

Complex(double _a=0.0,double _b=0.0):a(_a),b(_b){}

Complex operator + (const Complex &rhs) const {
return Complex(a + rhs.a , b + rhs.b);
}

Complex operator - (const Complex &rhs) const {
return Complex(a - rhs.a , b - rhs.b);
}

Complex operator * (const Complex &rhs) const {
return Complex(a * rhs.a - b * rhs.b , a * rhs.b + b * rhs.a);
}
};

//len = 2 ^ k
void change (Complex y[] , int len) {
for (int i = 1 , j = len / 2 ; i < len -1 ; i ++) {
if (i < j) swap(y[i] , y[j]);
int k = len / 2;
while (j >= k) {
j -= k;
k /= 2;
}
if(j < k) j += k;
}
}

// FFT
// len = 2 ^ k
// on = 1  DFT    on = -1 IDFT
void FFT (Complex y[], int len , int on) {
change (y , len);
for (int h = 2 ; h <= len ; h <<= 1) {
Complex wn(cos (-on * 2 * pi / h), sin (-on * 2 * pi / h));
for (int j = 0 ; j < len ; j += h) {
Complex w(1 , 0);
for (int k = j ; k < j + h / 2 ; k ++) {
Complex u = y[k];
Complex t = w * y [k + h / 2];
y[k] = u + t;
y[k + h / 2] = u - t;
w = w * wn;
}
}
}
if (on == -1) {
for (int i = 0 ; i < len ; i ++) {
y[i].a /= len;
}
}
}

const int maxn = 50050;
const int mm = maxn<<2;

Complex a[maxn<<2],b[maxn<<2],c[maxn<<2];
LL num[maxn<<2];
int ans[maxn<<2];
char s1[maxn<<1],s2[maxn<<1];
int len;
int flag;

void work(){
memset(num,0,sizeof(num));
FFT(a,len,1);
FFT(b,len,1);
FOR(i,0,len)    c[i] = a[i]*b[i];
FFT(c,len,-1);
FOR(i,0,len)    num[i] = (LL)(c[i].a+0.5);
LL car = 0;
FOR(i,0,mm){
ans[i] = (num[i]+car)%10;
car = (num[i]+car)/10;
}
int tot = 0;
IFOR(i,mm-1,-1){
if(ans[i])  {tot = i;break;}
}
if(!flag)   printf("-");
IFOR(i,tot,-1){
printf("%d",ans[i]);
}
printf("\n");
}

int main()
{
//freopen("test.in","r",stdin);
while(~scanf("%s%s",s1,s2)){
flag = 1;
int len1 = strlen(s1),len2 = strlen(s2);
FOR(i,1,len1)   a[len1-1-i] = Complex(s1[i] - '0',0);
FOR(i,1,len2)   b[len2-1-i] = Complex(s2[i] - '0',0);
if(s1[0] == '-') {flag ^= 1;-- len1;}
else    a[len1-1] = s1[0] - '0';
if(s2[0] == '-') {flag ^= 1;-- len2;}
else    b[len2-1] = s2[0] - '0';
int mx_len = 1+max(len1,len2);
mx_len <<= 1;
len = 1;
while(len < mx_len) len <<= 1;
FOR(i,len1,len) a[i] = Complex(0,0);
FOR(i,len2,len) b[i] = Complex(0,0);
work();
}
return 0;
}


#### HDU 1402 A * B Problem Plus(FFT模版题)

2016-10-08 10:50:37

#### HDU 1402——A * B Problem Plus

2016-08-09 15:20:07

#### hdu1402A * B Problem Plus

2016-03-20 21:45:39

#### HDU 1402 FFT 求 大数乘法

2013-08-06 10:13:43

#### HDU 1402 A * B Problem Plus(FFT实现高精度乘法)

2015-09-01 17:26:39

#### HDU 1402 A * B Problem Plus (FFT求高精度乘法)

2017-08-21 15:59:47

#### HDU 1402 A * B Problem Plus 高精度乘法

2010-09-26 14:27:00

#### HDU 1402 A * B Problem Plus (FFT入门，高精度乘法)

2016-10-30 23:17:54

#### HDOJ 1402. A * B Problem Plus (FFT快速傅里叶变换)

2016-09-24 20:24:03

#### HDU 1402 A * B Problem Plus（FFT）

2017-10-14 23:47:37

## 不良信息举报

HDU 1402 A * B Problem Plus （FFT求高精度乘法）