HDU1402:A * B Problem Plus(FFT与大数乘法)
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 .
Output
For each case, output A * B in one line.
Sample Input Sample Output
把每一位看成ai*10^i,然后就是两个多项式相乘。利用FFT,把复杂度降到O(nlogn)。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const double pi=acos(-1.0);
struct complex
{
double r,i;
complex(){};
complex(double rr,double ii):r(rr),i(ii){}
complex friend operator +(complex a,complex b){ return complex(a.r+b.r,a.i+b.i); }
complex friend operator -(complex a,complex b) { return complex(a.r-b.r,a.i-b.i);}
complex friend operator *(complex a,complex b) { return complex(a.r*b.r-a.i*b.i,a.r*b.i+a.i*b.r);}
}tmp[maxn];
struct DFT{
complex a[maxn];
void fft(int sz,int bg,int step,int opt){
if(sz==) return; int m=sz>>;
fft(m,bg,step<<,opt); fft(m,bg+step,step<<,opt);
complex w=complex(,),t=complex(cos(2.0*pi/sz),sin(2.0*pi*opt/sz));
for(int k=;k<m;k++)
{
int pos=*step*k;
tmp[k]=a[pos+bg]+w*a[pos+bg+step];
tmp[k+m]=a[pos+bg]-w*a[pos+bg+step];
w=w*t;
}
for(int i=;i!=sz;i++) a[i*step+bg]=tmp[i];
}
}A,B;
char c[maxn]; int ans[maxn+];
int main()
{
while(~scanf("%s",c)){
int L1=strlen(c),L2,len=;
for(int i=;i<L1;i++) A.a[i].r=c[L1-i-]-'',A.a[i].i=;
scanf("%s",c); L2=strlen(c);
for(int i=;i<L2;i++) B.a[i].r=c[L2-i-]-'',B.a[i].i=; while(len<L1+L2+) len<<=; for(int i=L1;i<=len;i++) A.a[i].r=A.a[i].i=;
for(int i=L2;i<=len;i++) B.a[i].r=B.a[i].i=; A.fft(len,,,); B.fft(len,,,);
for(int i=;i<len;i++) A.a[i]=A.a[i]*B.a[i];
A.fft(len,,,-); int head=;
for(int i=;i<len;i++) ans[i]=(int)(A.a[i].r/len+0.5);
for(int i=;i<len;i++){
ans[i+]+=ans[i]/; ans[i]%=;
if(ans[i]>) head=i;
} for(int i=head;i>=;i--) printf("%d",ans[i]);
printf("\n");
}
return ;
}
HDU1402:A * B Problem Plus(FFT与大数乘法)的更多相关文章
- HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...
- HDU1402 A * B Problem Plus FFT
分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...
- [hdu1402]A * B Problem Plus(FFT模板题)
解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...
- HDU 1402 A * B Problem Plus (FFT求高精度乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...
- FFT之大数乘法
#include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 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 ...
- A * B Problem Plus(fft)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 hdu_1402:A * B Problem Plus Time Limit: 2000/100 ...
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
随机推荐
- Toad Oracle 本地/远程数据库导入/导出 数据库备份
1. Toad进入数据库后,选择 Database ==> Export ===> Export Utility Wizard ,选择export user(按用户导出),选择Toa ...
- jQuery根据属性模糊匹配元素
1.查看带有指定属性的元素: [attribute] 例如: $("div[id]") 2.查看属性值是某个特定值的元素: [attribute=value] 例如: $(&quo ...
- CentOS6 Install kafka
https://www.cnblogs.com/caoguo/p/5958608.html
- Spring Boot中使用logback日志框架
说明:Spring Boot在最新的版本中默认使用了logback框架.一般来说使用时只需在classpath下创建logback.xml即可,而官方推荐使用logback-spring.xml替代, ...
- ArcGIS Engine三维动画开发 来自:http://www.iarcgis.com/?p=826
ArcGIS Engine 三维开发 来自:http://www.iarcgis.com/?p=826 在三维中,经常使用的一个功能就是播放动画,也就是我们要对一条动画轨迹进行播放,而在ArcGIS ...
- 利用WiFi Pineapple Nano渗透客户端获取SHELL
前言: 前两篇文章介绍了The WiFi Pineapple Nano设备的一些主要功能模块,例如PineAP.SSLsplit和Ettercap等.今天给大家实际场景演示下如何利用Pineapple ...
- LightOj 1027 A Dangerous Maze【概率】
题目链接:http://www.lightoj.com/volume_showproblem.php? problem=1027 题意: 你面前有n个门,每一个相应一个数字,若为正xi.代表xi分钟后 ...
- :>/dev/null 2>&1 的作用
shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 /dev/null 代表空设备文件 > 代表重定向到哪里,例如:ec ...
- Python中的列表,元组,字符串之间的相互转化
Python中的列表元组和字符串之间的相互转化需要利用,tuple(),list(),str(). 示例如下: >>> the_string = "hello I'am x ...
- 关于java及多线程
http://www.w3cschool.cc/java/java-multithreading.html