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. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  2. 04-JVM内存模型:直接内存

    1.1.什么是直接内存(Derect Memory) 在内存模型最开始的章节中,我们画出了JVM的内存模型,里面并不包含直接内存,也就是说这块内存区域并不是JVM运行时数据区的一部分,但它却会被频繁的 ...

  3. java nio通过ByteBuffer输出文件信息

    1.通过ByteBuffer的get()方法每次读取一个字节转换成char类型输出. fc = new FileInputStream("src/demo20/data.txt") ...

  4. JVM知识(上)

    目录 什么是JVM? JVM的生命周期 JVM的体系结构 JVM的数据类型 java虚拟机被称为"虚拟",因为它是一个抽象的计算机定义的规范.要运行一个Java程序,需要一个抽象的 ...

  5. JAVA基础学习之路(十二)链表

    定义链表的基本结构: class Link {//外部类 //内部类,只为链表类服务 private class Node {//定义节点类 private String data;//保存的数据 p ...

  6. Debian 9 + Windows 10 双系统安装体验

    很久之前就想在自己的电脑上也装个 Debian 玩玩了,最近正好有时间折腾,就踩了踩坑在笔记本上装了玩玩~ UEFI + GPT 解决启动相关的麻烦配置 如果在支持 UEFI 的电脑上安装 Debia ...

  7. 福利,一张图看懂IT售前工程师修炼之道

    职场中的新人如何自我定位? 如何深刻理解IT售前这个职位? 如何从IT售前菜鸟成长为IT售前专家? 推荐这本书<IT售前工程师修炼之道> 本书精华内容 售前的重要性 售前要有逻辑能力 售前 ...

  8. Hyperledger Fabric中的Identity

    Hyperledger Fabric中的Identity 什么是Identity 区块链网络中存在如下的角色:peers, orderers, client application, administ ...

  9. Blockchain For Dummies(IBM Limited Edition

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

  10. 关于Amazon.com Seller 网络以及IP地址更换 官方回答

    Greetings from Amazon Seller Support, I understand your concern that there will be a change of IP ad ...