http://acm.hdu.edu.cn/showproblem.php?pid=1402

给出两个高精度正整数,求它们的积,最长的数长度不大于5e4。

FFT裸题,将每个数位看做是多项式的系数即可。

我们最后就是要求出两个多项式相乘的系数。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef double dl;
const dl pi=acos(-1.0);
const int N=2e5+;
struct complex{//定义复数
dl x,y;
complex(dl xx=0.0,dl yy=0.0){
x=xx;y=yy;
}
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 FFT(complex a[],int n,int on){
for(int i=,j=n>>;i<n-;i++){
if(i<j)swap(a[i],a[j]);
int k=n>>;
while(j>=k){j-=k;k>>=;}
if(j<k)j+=k;
}
for(int i=;i<=n;i<<=){
complex res(cos(-on**pi/i),sin(-on**pi/i));
for(int j=;j<n;j+=i){
complex w(,);
for(int k=j;k<j+i/;k++){
complex u=a[k],t=w*a[k+i/];
a[k]=u+t;
a[k+i/]=u-t;
w=w*res;
}
}
}
if(on==-)
for(int i=;i<n;i++)a[i].x/=n;
}
char a[N],b[N];
complex x[N],y[N];
int ans[N];
int main(){
while(cin>>a>>b){
int len1=strlen(a),len2=strlen(b);
int n=;
while(n<len1*||n<len2*)n<<=;
for(int i=;i<len1;i++)x[i]=complex(a[len1--i]-'',);
for(int i=len1;i<n;i++)x[i]=complex(,);
for(int i=;i<len2;i++)y[i]=complex(b[len2--i]-'',);
for(int i=len2;i<n;i++)y[i]=complex(,);
FFT(x,n,);FFT(y,n,);
for(int i=;i<n;i++)x[i]=x[i]*y[i];
FFT(x,n,-);
for(int i=;i<n;i++)ans[i]=(int)(x[i].x+0.5);
for(int i=;i<n;i++){
ans[i+]+=ans[i]/;
ans[i]%=;
}
n=len1+len2-;
while(ans[n]<=&&n>)n--;
for(int i=n;i>=;i--)printf("%d",ans[i]);
puts("");
}
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

HDU1402:A * B Problem Plus——题解的更多相关文章

  1. BZOJ2298:[HAOI2011]problem a——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=2298 https://www.luogu.org/problemnew/show/P2519 一次 ...

  2. FFT/NTT模板 既 HDU1402 A * B Problem Plus

    @(学习笔记)[FFT, NTT] Problem Description Calculate A * B. Input Each line will contain two integers A a ...

  3. 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解

    [比赛链接] 点击打开链接 [题解] Problem A Word Correction[字符串] 不用多说了吧,字符串的基本操作 Problem B  Run for your prize[贪心] ...

  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. 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)

    洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...

  6. Lintcode399 Nuts & Bolts Problem solution 题解

    [题目描述] Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one m ...

  7. Hdoj 1086.You can Solve a Geometry Problem too 题解

    Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare ...

  8. BZOJ2301:[HAOI2011]Problem b——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 https://www.luogu.org/problemnew/show/P2522 对于给 ...

  9. HDU1402 A * B Problem Plus

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

随机推荐

  1. HTTP 两种基本请求方法 GET和 POST的区别

    GET方法 1.GET交互方式是从服务器上获取数据,而并非修改数据,所以GET交互方式是安全的.就像数据库查询一样,从数据库查询数据,并不会影响数据库的数据信息,对数据库来说,也就是安全的.2.GET ...

  2. nginx 日志模块

    ngx_http_log_module.c 数据结构 typedef struct { void **main_conf; void **srv_conf; void **loc_conf;} ngx ...

  3. python错误记录

    在主函数里调用其他函数时形参顺序要一致 错例如下:

  4. Blockchain For Dummies(IBM Limited Edition

    Blockchain For Dummies(IBM Limited Edition)笔记 该系列内容主要介绍用于商业的区块链,有人说区块链之于贸易,犹如因特网之于信息.在商业领域区块链可以用于交易任 ...

  5. 5.airflow问题

    1. Traceback (most recent call last): File "/usr/bin/airflow", line 28, in <module> ...

  6. USACO 2.3.3 Zero Sum 和为零(深搜枚举)

    Description 请考虑一个由1到N(N=3, 4, 5 ... 9)的数字组成的递增数列:1 2 3 ... N. 现在请在数列中插入“+”表示加,或者“-”表示减,抑或是“ ”表示空白,来将 ...

  7. Programming Protocol-Independent Packet Processors

    引言 OpenFlow协议固定的包头域数目,使得南向协议过于死板. P4可以实现自定义包头,增加灵活性. P4是OpenFlow未来发展的方向. We propose P4 as a strawman ...

  8. MySql 8 命令

    1-创建用户 create user 用户名@'%' identified by '密码'; create user 用户名@'localhost' identified by '密码';   2-授 ...

  9. struts2--文件上传类型3

    拦截器栈在<package>标签内 <action>标签外配置 如上我们如果把它定义成默认拦截器的话就不需要在 <action>标签中引入,没有的话需要引入拦截器 ...

  10. Scrum 项目准备4.0

    4.0----------------------------------------------- 1.准备看板. 形式参考图4. 2.任务认领,并把认领人标注在看板上的任务标签上. 先由个人主动领 ...