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的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
随机推荐
- Leetcode 240.搜索二维矩阵II
搜索二维矩阵II 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性: 每行的元素从左到右升序排列. 每列的元素从上到下升序排列. 示例: 现有 ...
- ms sqlserver数据库建索引
索引分类:从物理结构上可分为两种:聚集索引和非聚集索引 (此外还有空间索引.筛选索引.XML索引) 因为聚集索引是索引顺序与物理存储顺序一致,所以只能建一个. 聚集索引就是把数据按主键顺序存储: 因为 ...
- MTK GPIO 新增变量配置
主要涉及的文件: 1.需要配置preloader ,lk ,kernel vendor GPIO_YuSu.cmp文件增加IO别名: 2.需要配置preloader ,lk ,kernel vendo ...
- JPos学习
基于JPos的消息交换系统 消息交换系统需求解读 消息交换系统不不是一个具体的业务系统,而是业务系统的运转的基础框架: 他的运转是体现在报文交换上的: 要定义一个可被不同业务系统使用的报文规范: 报文 ...
- 【收藏】下载Chrome商店插件的方法,万恶的gwd
以下是下载离线插件包的方法: 第一步: 每个Google Chrome扩展都有一个固定的ID,例如https://chrome.google.com/webstore/detail/bfbmjmiod ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- linux date 格式化时间和日期
[root@108test ~]# date -d today +"%Y-%m-%d" 2008-05-07 [root@108test ~]# date -d today + ...
- hashlib-sha摘要算法模块
摘要:hashlib: 摘要算法的模块 用处: 1.查看某两个文件是否完全一致 "abcdefggg" "abcdefhhg" 2.加密认证 把密码加密后写入文 ...
- python学习之 - XML
xml模块定义:实现不同语言或程序之间进行数据交换的协议.格式如下:通过<>节点来区别数据结构如:<load-on-startup(这个是标签) test="value&q ...
- ubuntu-12.04下安装postgresql
2013-10-01 20:42:57| moniter参考资料:Ubuntu 12.04下PostgreSQL-9.1安装与配置详解(在线安装)一.安装postgresqlbamboo@bam ...