hdu1402 FFT入门
参考这里:http://www.cnblogs.com/pdev/p/4354705.html
http://www.cnblogs.com/pdev/p/4354629.html
题意:求大数乘法A*B
A和B位数很长。裸高精度时间复杂度是O(nm),会完蛋
不妨回忆下裸高精度的过程:

其实乘法的那一步很类似前面介绍过的多项式快速乘法诶(⊙▽⊙)
所以就可以用前述方法计算咯,时间复杂度O(nlogn)
我是这样理解的:
每个乘数都是都是一坨时域信号(一个大混合物),然后对乘数分别进行DFT(Discrete Fourier Transform)得到频域信号(一堆纯净物)。
然后对纯净物按类别分别相加,就得到了新信号(这里即乘法结果)对应的频域信号(一堆纯净物)
然后再来一次IDFT(Inverse DFT,逆变换)把频域再转成时域(一个大混合物,即真正的乘法结果)就好啦
总结一下本题的模式:
读入向量x、y
int len=1; while(len < lx*2 || len < ly*2)len<<=1; (lx、ly分别是向量x和y的长度)
fft(x),fft(y)
for i=0 to len-1 x[i]=x[i]*y[i]
ifft(x)
for(int i = 0; i < len; i++) sum[i] = (int)(X[i].x+0.5); 变回整数
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
const double PI = acos(-1.0);
//复数结构体
struct Complex
{
double x,y;//实部和虚部 x+yi
Complex(double _x = 0.0,double _y = 0.0)
{
x = _x;
y = _y;
}
Complex operator -(const Complex &b)const
{
return Complex(x-b.x,y-b.y);
}
Complex operator +(const Complex &b)const
{
return Complex(x+b.x,y+b.y);
}
Complex operator *(const Complex &b)const
{
return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
}
}; void change(Complex y[],int len)
{
int i,j,k;
for(i = , j = len/; i <len-; i++)
{
if(i < j)swap(y[i],y[j]);
//交换互为小标反转的元素,i<j保证交换一次
//i做正常的+1,j左反转类型的+1,始终保持i和j是反转的
k = len/;
while(j >= k)
{
j -= k;
k /= ;
}
if(j < k)j += k;
}
} void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h = ; h <= len; h <<= )
{
Complex wn(cos(-on**PI/h),sin(-on**PI/h));
for(int j = ; j < len; j+=h)
{
Complex w(,);
for(int k = j; k < j+h/; k++)
{
Complex u = y[k];
Complex t = w*y[k+h/];
y[k] = u+t;
y[k+h/] = u-t;
w = w*wn;
}
}
}
if(on == -)
for(int i = ; i < len; i++)
y[i].x /= len;
} const int MAXN = ;
Complex x1[MAXN],x2[MAXN];
char str1[MAXN/],str2[MAXN/];
int sum[MAXN];
int main()
{
while(scanf("%s%s",str1,str2)==)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = ;
while(len < len1* || len < len2*)len<<=;
for(int i = ; i < len1; i++)
x1[i] = Complex(str1[len1--i]-'',);
for(int i = len1; i < len; i++)
x1[i] = Complex(,);
for(int i = ; i < len2; i++)
x2[i] = Complex(str2[len2--i]-'',);
for(int i = len2; i < len; i++)
x2[i] = Complex(,);
//x1[i]:x1对应的向量
//例如1989就是(9,0)、(8,0)、(9,0)、(1,0)、(0,0)、... fft(x1,len,);
fft(x2,len,); for(int i = ; i < len; i++)
x1[i] = x1[i]*x2[i]; fft(x1,len,-); for(int i = ; i < len; i++)
sum[i] = (int)(x1[i].x+0.5);
/*
for(int i=0;i<len;i++)
cout<<sum[i]<<" ";
cout<<endl;
*/
for(int i = ; i < len; i++) //此时的sum存的东西还没进位,还得处理下
{
sum[i+]+=sum[i]/;
sum[i]%=;
}
len = len1+len2-;
while(sum[len] <= && len > )len--;
for(int i = len; i >= ; i--)
printf("%c",sum[i]+'');
printf("\n");
}
return ;
}
hdu1402 FFT入门的更多相关文章
- TOT 傅立叶变换 FFT 入门
HDU 1402,计算很大的两个数相乘. FFT 只要78ms,这里: 一些FFT 入门资料:http://wenku.baidu.com/view/8bfb0bd476a20029bd642d85. ...
- 洛谷p3803 FFT入门
洛谷p3803 FFT入门 ps:花了我一天的时间弄懂fft的原理,感觉fft的折半很神奇! 大致谈一谈FFT的基本原理: 对于两个多项式的卷积,可以O(n^2)求出来(妥妥的暴力) 显然一个多项式可 ...
- FFT入门
这篇文章会讲讲FFT的原理和代码. 先贴picks博客(又名FFT从入门到精通):http://picks.logdown.com/posts/177631-fast-fourier-transfor ...
- FFT 入门
推荐博客 :https://oi.men.ci/fft-notes/ 卷积的理解 : https://www.zhihu.com/question/22298352?rf=21686447 题目链接 ...
- 模板:快速傅里叶变换(FFT)
参考:http://blog.csdn.net/f_zyj/article/details/76037583 如果公式炸了请去我的csdn博客:http://blog.csdn.net/luyouqi ...
- bzoj2179: FFT快速傅立叶
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- 多项式FFT相关模板
自己码了一个模板...有点辛苦...常数十分大,小心使用 #include <iostream> #include <stdio.h> #include <math.h& ...
- 3-idiots hdu4609 母函数+FFT 组合数学题
http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意:1e5个数,求取三个数能形成三角形的概率. 题解(这怎么会是fft入门题QAQ): 概率的算法就是三 ...
- HDU 1402 大数乘法 FFT、NTT
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- CodeDom
细说CodeDom 在上一篇文章中,老周厚着脸皮给大伙介绍了代码文档的基本结构,以及一些代码对象与CodeDom类型的对应关系. 在评论中老周看到有朋友提到了 Emit,那老周就顺便提一下.严格上说, ...
- [py]给函数传递数组和字典
一 , 1.1传元组 def fun(x): print x t=(1,2) fun(t) 1.2传元组 #传元组 def fun(x,y): print x,y # t=(1,2) t=(1,2,3 ...
- Spring Security笔记:解决CsrfFilter与Rest服务Post方式的矛盾
基于Spring Security+Spring MVC的web应用,为了防止跨站提交攻击,通常会配置csrf,即: <http ...> ... <csrf /> </ ...
- mybatis 3.2.8 + log4j2.0.2 控制台输出sql语句
mybatis3.2.7有一个bug,使用log4j2 (2.0.2)版本时,会找不到类 ,导致启动失败,详见 https://github.com/mybatis/mybatis-3/issues/ ...
- EMV内核使用中的常见问题
EMV内核在使用上会由于调用不当引起的许多问题,本文旨在基于内核LOG(也就是与IC卡交互的指令LOG)的基础上,对一些常见问题作初步的分析与解答,方便不熟悉EMV规范的同学参考. 本文的前提是你已经 ...
- 百度Android定位SDK获取位置
http://gis.sunxianlei.cn/2013/01/27/%E7%99%BE%E5%BA%A6android%E5%AE%9A%E4%BD%8Dsdk%E8%8E%B7%E5%8F%96 ...
- BPR: Bayesian Personalized Ranking from Implicit Feedback-CoRR 2012——20160421
1.Information publication:CoRR 2012 2.What 商品推荐中常用的方法矩阵因子分解(MF),协同过滤(KNN)只考虑了用户购买的商品,文章提出利用购买与未购买的偏序 ...
- SharePoint配置搜索服务和指定搜索范围
转载:http://constforce.blog.163.com/blog/static/163881235201201211843334/ 一.配置SharePoint Foundation搜索 ...
- 软件工程(FZU2015)增补作业
说明 张老师为FZU软件工程2015班级添加了一次增补作业,总分10分,deadline是2016/01/01-2016/01/03 前11次正式作业和练习的迭代评分见:http://www.cnbl ...
- 把php上传sae问题要使用IO
应用移植指南 一,为什么要移植应用 SAE禁止IO写操作,代码目录不能写入.这意味着普通程序的上传图片.生成缓存等操作都不能在SAE上正常运行,这时候你需要对这些代码进行修改后才能让你的程序运行在SA ...