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的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
随机推荐
- COJ 1208 矩阵快速幂DP
题目大意: f(i) 是一个斐波那契数列 , 求sum(f(i)^k)的总和 由于n极大,所以考虑矩阵快速幂加速 我们要求解最后的sum[n] 首先我们需要思考 sum[n] = sum[n-1] + ...
- bzoj3514 Codechef MARCH14 GERALD07加强版 lct预处理+主席树
Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1951 Solved: 746[Submi ...
- react.js 组件之间的数据传递props
/* *属性 * 1.如何传递属性 * 2.属性和状态区别和联系 * * 3.子组件都有一个props属性对象 * * 4.单线数据流(只能从父组件流向子组件,就是在父组件定义一个属性,子组件可以通过 ...
- 6572平台上关于wifi热点切换跳的坑
最近在做一个无屏的项目,需要开启设备的wifi热点,通过连接热点设置设备wifi,本来看起来很容易完成的一件事情,遇到了一下的坑 在wifi切换状态时,大概率出现不能切换的问题,比如从wifi状态切换 ...
- 【搜索引擎】Solr最新安装以及通过关系型数据库(MySQL,Oracle,PostgreSQL)导入数据
版本号 最新的solr版本 : Solr 8.1.1下载地址:https://lucene.apache.org/solr/downloads.html solr-8.1.0.tgz for Linu ...
- Spring AOP Capability and Goal
AOP Capability: 1.Spring声明式事务管理配置. 2.Controller层的参数校验. 3.使用Spring AOP实现MySQL数据库读写分离案例分析 4.在执行方法前,判断是 ...
- Caused by: java.lang.IncompatibleClassChangeError: class org.springframework.scheduling.quartz.CronTriggerBean has interface org.quartz.CronTrigger as super class
这是版本的问题: 解决办法有两种: 1.降低Quartz版本,降到1.X去. 2.升级Spring版本到3.1+,根据Spring的建议,将原来的**TriggerBean替换成**TriggerFa ...
- spring boot + redis 实现session共享
这次带来的是spring boot + redis 实现session共享的教程. 在spring boot的文档中,告诉我们添加@EnableRedisHttpSession来开启spring se ...
- 禁用Bootstrap点击空白,modal自动关闭
手动触发modal: $('#myModal').modal(): 禁用点击空白,modal自动关闭:$('#myModal').modal({backdrop: 'static', ke ...
- javax/servlet/ServletContext : Unsupported major.minor version 51.0
原文:http://blog.csdn.net/mlin_123/article/details/50738532 解决:将版本从 3.1.0 改为 3.0.1 <!-- 添加servlet A ...