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与大数乘法)的更多相关文章

  1. HDU-1402 A * B Problem Plus FFT(快速傅立叶变化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 一般的的大数乘法都是直接模拟乘法演算过程,复杂度O(n^2),对于这题来说会超时.乘法的过程基本 ...

  2. HDU1402 A * B Problem Plus FFT

    分析:网上别家的代码都分析的很好,我只是给我自己贴个代码,我是kuangbin的搬运工 一点想法:其实FFT就是快速求卷积罢了,当小数据的时候我们完全可以用母函数来做,比如那种硬币问题 FFT只是用来 ...

  3. [hdu1402]A * B Problem Plus(FFT模板题)

    解题关键:快速傅里叶变换fft练习. 关于结果多项式长度的确定,首先将短多项式扩展为长多项式,然后扩展为两倍. #include<cstdio> #include<cstring&g ...

  4. 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 ...

  5. HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)

    题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...

  6. FFT之大数乘法

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...

  7. [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 ...

  8. A * B Problem Plus(fft)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1402 hdu_1402:A * B Problem Plus Time Limit: 2000/100 ...

  9. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

随机推荐

  1. IBM MQ 创建以及常见问题集锦

    消息队列+发送队列+消息通道 接收通道名称与发送端的发送通道名称要一致,修改通道信息后要执行 start channle(chlname) 重启通道.常用的MQ命令 66.0.42.240 用户 mq ...

  2. rsync 同步文件重复拷贝问题

    rsync 是同步文件的利器,一般用于多个机器之间的文件同步与备份,同时也支持在本地的不同目录之间互相同步文件.在这种场景下,rsync 远比 cp 命令更加合适,它只会同步需要更新的文件,默认情况下 ...

  3. jQuery的切换函数(hover,toggle)

    1.hover:(鼠标悬停与离开事件) 一个模仿悬停事件(鼠标移动到一个对象上面及移出这个对象)的方法.这是一个自定义的方法,它为频繁使用的任务提供了一种“保持在其中”的状态. 当鼠标移动到一个匹配的 ...

  4. 前端学习之--CSS

    CSS 常用帮助文档 CSS:被用来同时控制多重网页的样式和布局.HTML页面中CSS样式的写法有3种: 1:标签中写入 <body style='margin o auto;'> 2:h ...

  5. DTrace C++ Mysteries Solved 转

      I’ve been using DTrace on Leopard in my recent work, and while it’s a great tool, the C++ support ...

  6. Java反射常用示例

    package xmq.study.reflection; import java.lang.annotation.Annotation; import java.lang.reflect.Const ...

  7. how to read openstack code: Core plugin and resource extension

    本章我们将写一个自己的core plugin 和一个resource extension来加深理解.(阅读本文的前提是你已经理解了restful以及stevedore等内容) 什么是 core plu ...

  8. 学习swift从青铜到王者之swift属性09

    1.结构体常量和类常量的存储属性 let p1 = Person1() //p1.age = 88 不允许修改 //p11.name = "yhx1" 不允许修改 var p11 ...

  9. 【网络】TCP协议

    一.概述 主要特点: 1)面向连接的运输层协议 2)每一条TCP连接只能有两个端点,每一条TCP连接只能是点对点的(一对一) 3)TCP提供可靠交付的服务 4)TCP提供全双工通信 5)面向字节流:T ...

  10. 【APUE】线程与信号

    每个线程都有自己的信号屏蔽字,但是信号的处理是进程中所有线程共享的.进程中的信号是递送到单个线程的. 线程中pthread_sigmask函数类似与进程的sigprocmask函数,可以用来阻塞信号. ...