【HDU1402】【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
2
1000
2
Sample Output
2
2000
Author
DOOM III
Recommend
DOOM III
 
【分析】
模板,不过我想说的是,这居然是06年的题目。。。太恐怖了。
 /*
宋代苏轼
《临江仙·夜饮东坡醒复醉》
夜饮东坡醒复醉,归来仿佛三更。家童鼻息已雷鸣。敲门都不应,倚杖听江声。
长恨此身非我有,何时忘却营营。夜阑风静縠纹平。小舟从此逝,江海寄余生。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const double Pi = acos(-1.0);
const int MAXN = + ;
using namespace std;
typedef long long ll;
struct Num {
double a , b;
//构造函数
Num(double x = ,double y = ) {a = x; b = y;}
//复数的三种运算
Num operator + (const Num &c) {return Num(a + c.a, b + c.b);}
Num operator - (const Num &c) {return Num(a - c.a, b - c.b);}
Num operator * (const Num &c) {return Num(a * c.a - b * c.b, a * c.b + b * c.a);}
}x1[MAXN] , x2[MAXN]; //注意loglen为log后的长度
void change(Num *t, int len, int loglen){
//蝶形变换后的序列编号
for (int i = ; i < len; i++){
int x = i, k = ;
for (int j = ; j < loglen; j++, x >>= ) k = (k << ) | (x & );
if (k < i) swap(t[k], t[i]);
}
}
//基2-FFT
void FFT(Num *x, int len, int loglen){
change(x, len, loglen);
int t = ;
//t为长度
for (int i = ; i < loglen; i++, (t <<= )){
int l = , r = l + t;
while (l < len){
//初始化
Num a, b, wo(cos(Pi / t), sin(Pi / t)), wn(, );
for (int j = l; j < l + t; j++){
a = x[j];
b = x[j + t] * wn;
//蝶形计算
x[j] = a + b;
x[j + t] = a - b;
wn = wn * wo;
}
//注意是合并所以要走2t步
l = r + t;
r = l + t;
}
}
}
//离散傅里叶变换
void DFT(Num *x, int len, int loglen){
int t = (<<loglen);
for (int i = ; i < loglen; i++){
t >>= ;
int l = , r = l + t;
while (l < len){
Num a, b, wn(, ), wo(cos(Pi / t), -sin(Pi / t));
for (int j = l; j < l + t; j++){
a = x[j] + x[j + t];
b = (x[j] - x[j + t]) * wn;
x[j] = a;
x[j + t] = b;
wn = wn * wo;
}
l = r + t;
r = l + t;
}
}
change(x, len, loglen);
for (int i= ; i < len; i++) x[i].a /= len;
}
int solve(char *a, char *b){
int len1, len2, len, loglen;
int t, over;
len1 = strlen(a) << ;
len2 = strlen(b) << ;
len = ;
loglen = ;
while (len < len1) len <<= , loglen++;
while (len < len2) len <<= , loglen++;
//处理len1
for (int i = ; i < len; i++){
if (a[i]) x1[i].a = a[i] - '', x1[i].b = ;
else x1[i].a = x1[i].b = ;
}
for (int i = ; i < len; i++){
if (b[i]) x2[i].a = b[i] - '', x2[i].b = ;
else x2[i].a = x2[i].b = ;
}
FFT(x1, len, loglen);
FFT(x2, len, loglen);
for (int i = ; i < len; i++) x1[i] = x1[i] * x2[i]; DFT(x1, len, loglen);
over = len = ;
//转换成十进制的整数
for (int i = ((len1 + len2) / ) - ; i >= ; i--){
t = x1[i].a + over + 0.5;
a[len++] = t % ;
over = t / ;
}
while (over){
a[len++] = over % ;
over /= ;
}
return len;
}
//输出
void print(char *str, int len){
for(len--; len>= && !str[len];len--);
if(len < ) putchar('');
else for(;len>=;len--) putchar(str[len]+'');
printf("\n");
}
char a[MAXN] , b[MAXN]; int main() { //char a[MAXN], b[MAXN];
while(scanf("%s%s", a, b) != EOF) {
print(a, solve(a, b));
memset(a, , sizeof(a));
memset(b, , sizeof(b));
}
//printf("%.10lf\n", Pi);
return ;
}
上一篇:mongodb .net driver


下一篇:ECE 595: Machine Learning