题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402
代码转载:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210389.html
题解转载:http://blog.csdn.net/jtjy568805874/article/details/49493667
题意:就是个大叔乘法,但是值有n<=50000,传统的n2是过不了的… 要用fft进行加速.
个人感想:
我看了2晚的fft,重复一遍一遍看,可是,一旦看到公式那些根本看不下去,我只理解大概意思,但好多东西还是很模糊… - -这对我这种蒟蒻来说,感觉能力有限啊…我已经尽力了…暂时只要知道能怎么用就好了…
个人还是比较喜欢用bin神的代码…怎么说,喜欢他的风格,写的代码干干净净又看起来舒服,好好理解…
这是我的fft开始啊~很久想学习就是没时间了.
分析:快速傅里叶转换.
代码:
/* Author:GavinjouElephant
* Title:
* Number:
* main meanning:
*
*
*
*/
#include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
#include <set>
#include <cstdlib>
#include <map>
#include <queue>
//#include<initializer_list>
//#include <windows.h>
//#include <fstream>
//#include <conio.h>
#define MaxN 0x7fffffff
#define MinN -0x7fffffff
#define lson 2*k
#define rson 2*k+1
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=2e5+10;
const double PI = acos(-1.0);
int Scan()//读入整数外挂.
{
int res = 0, ch, flag = 0;
if((ch = getchar()) == '-') //判断正负
flag = 1;
else if(ch >= '0' && ch <= '9') //得到完整的数
res = ch - '0';
while((ch = getchar()) >= '0' && ch <= '9' )
res = res * 10 + ch - '0';
return flag ? -res : res;
}
void Out(int a) //输出外挂
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
//复数结构体
struct Complex
{
double r,i;
Complex(double _r = 0.0,double _i = 0.0)
{
r = _r; i = _i;
}
Complex operator +(const Complex &b)
{
return Complex(r+b.r,i+b.i);
}
Complex operator -(const Complex &b)
{
return Complex(r-b.r,i-b.i);
}
Complex operator *(const Complex &b)
{
return Complex(r*b.r-i*b.i,r*b.i+i*b.r);
}
};
/*
* 进行FFT和IFFT前的反转变换。
* 位置i和 (i二进制反转后位置)互换
* len必须去2的幂
*/
void change(Complex y[],int len)
{
int i,j,k;
for(i = 1, j = len/2;i < len-1; i++)
{
if(i < j)swap(y[i],y[j]);
//交换互为小标反转的元素,i<j保证交换一次
//i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
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].r /= len;
}
Complex x1[maxn];
Complex x2[maxn];
char str1[maxn/2];
char str2[maxn/2];
int sum[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("coco.txt","r",stdin);
freopen("lala.txt","w",stdout);
#endif
while(scanf("%s%s",str1,str2)!=EOF)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = 1;
while(len < len1*2 || len < len2*2)len<<=1;
for(int i = 0;i < len1;i++)
x1[i] = Complex(str1[len1-1-i]-'0',0);
for(int i = len1;i < len;i++)
x1[i] = Complex(0,0);
for(int i = 0;i < len2;i++)
x2[i] = Complex(str2[len2-1-i]-'0',0);
for(int i = len2;i < len;i++)
x2[i] = Complex(0,0);
//求DFT
fft(x1,len,1);
fft(x2,len,1);
for(int i = 0;i < len;i++)
x1[i] = x1[i]*x2[i];
fft(x1,len,-1);
for(int i = 0;i < len;i++)
sum[i] = (int)(x1[i].r+0.5);
for(int i = 0;i < len;i++)
{
sum[i+1]+=sum[i]/10;
sum[i]%=10;
}
len = len1+len2-1;
while(sum[len] <= 0 && len > 0)len--;
for(int i = len;i >= 0;i--)
printf("%d",sum[i]);
printf("\n");
}
return 0;
}