题意:计算A*B,A,B均为长度小于50000的整数。

这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT。

A和B的存储均用long long,在计算乘积的时候转化成double,计算完成后再转回来即可。

测得base在精度允许范围内最多能开到10000。

把平方和快速幂的函数也写上了,可以当模板用~

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int N=1e5+,inf=0x3f3f3f3f;
const db pi=acos(-);
const ll P[]= {,,,,};
struct cpl {
db x,y;
cpl operator-(cpl& b) {return {x-b.x,y-b.y};}
cpl operator+(cpl& b) {return {x+b.x,y+b.y};}
cpl operator*(cpl& b) {return {x*b.x-y*b.y,x*b.y+y*b.x};}
} A[N],B[N];
void change(cpl* a,int n) {
for(int i=,j=n>>,k; i<n-; ++i) {
if(i<j)swap(a[i],a[j]);
k=n>>;
while(j>=k)j-=k,k>>=;
j+=k;
}
}
void FFT(cpl* a,int n,int f) {
change(a,n);
for(int k=; k<n; k<<=) {
cpl wn= {cos(pi*f/k),sin(pi*f/k)};
for(int i=; i<n; i+=k<<) {
cpl w{,};
for(int j=i; j<i+k; ++j) {
cpl x=a[j],y=w*a[j+k];
a[j]=x+y,a[j+k]=x-y;
w=w*wn;
}
}
}
if(!~f)for(int i=; i<n; ++i)a[i].x/=n,a[i].y/=n;
}
struct Bigint {
static const int N=1e5+;
static const int bit=;
static const ll base=pow(,bit);
int n;
ll a[N];
ll& operator[](int x) {return a[x];}
void init(ll x) {
memset(a,,sizeof a);
a[]=x,n=;
}
void init(char* s) {
memset(a,,sizeof a);
int m=strlen(s);
for(int i=; i<m; ++i)a[(m--i)/bit]+=(s[i]-'')*P[(m--i)%bit];
n=(m-)/bit;
}
void Sqr() {
int m;
for(m=; m<=n*; m<<=);
for(int i=; i<m; ++i)A[i]= {a[i],};
FFT(A,m,);
for(int i=; i<m; ++i)A[i]=A[i]*A[i];
FFT(A,m,-);
for(int i=; i<m; ++i)a[i]=A[i].x+0.5;
for(int i=; i<m; ++i) {
if(a[i]>=base) {
a[i+]+=a[i]/base;
a[i]%=base;
if(i==m-)++m;
}
}
for(n=m-; n>&&!a[n]; --n);
}
void Mul(Bigint& b) {
int m;
for(m=; m<=n*||m<=b.n*; m<<=);
for(int i=; i<m; ++i)A[i]= {a[i],},B[i]= {b[i],};
FFT(A,m,),FFT(B,m,);
for(int i=; i<m; ++i)A[i]=A[i]*B[i];
FFT(A,m,-);
for(int i=; i<m; ++i)a[i]=A[i].x+0.5;
for(int i=; i<m; ++i) {
if(a[i]>=base) {
a[i+]+=a[i]/base;
a[i]%=base;
if(i==m-)++m;
}
}
for(n=m-; n>&&!a[n]; --n);
}
void Pow(int p) {
Bigint b=*this;
this->init();
for(; p; p>>=,b.Sqr())if(p&)this->Mul(b);
}
void pr() {
for(int i=n; i>=; --i) {
if(i==n)printf("%lld",a[i]);
else printf("%04lld",a[i]);
}
puts("");
}
} a,b;
char s1[N],s2[N]; int main() {
while(scanf("%s%s",s1,s2)==) {
a.init(s1),b.init(s2);
a.Mul(b);
a.pr();
}
return ;
}

HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)的更多相关文章

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

  2. hdu 1402 A * B Problem Plus FFT

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

  3. HDU - 1402 A * B Problem Plus FFT裸题

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...

  4. HDU 1402 A * B Problem Plus (FFT模板题)

    FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...

  5. P1919 FFT加速高精度乘法

    P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...

  6. HDU 1402 A * B Problem Plus 快速傅里叶变换 FFT 多项式

    http://acm.hdu.edu.cn/showproblem.php?pid=1402 快速傅里叶变换优化的高精度乘法. https://blog.csdn.net/ggn_2015/artic ...

  7. SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法

    SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...

  8. FFT实现高精度乘法

    你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...

  9. BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法

    Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...

随机推荐

  1. Java进阶学习:将文件上传到七牛云中

    Java进阶学习:将文件上传到七牛云中 通过本文,我们将讲述如何利用七牛云官方SDK,将我们的本地文件传输到其存储空间中去. JavaSDK:https://developer.qiniu.com/k ...

  2. 【Flask】Sqlalchemy join

    ### join:1. join分为left join(左外连接)和right join(右外连接)以及内连接(等值连接).2. 参考的网页:http://www.jb51.net/article/1 ...

  3. MongoDB 使用Limit和Skip完成分页 和游标(二)

    //$slice操作符返回文档中指定数组的内部值 //查询出Jim书架中第2~4本书 db.persons.find({name:"jim"},{books:{"$sli ...

  4. 20145230《Java程序设计》第3周学习总结

    20145230 <Java程序设计> 第3周学习总结 教材学习内容总结 String s=new String();第四章我首先了解了CPU与内存的关系,栈与堆的关系.要产生对象必须先定 ...

  5. 20165101刘天野 2017-2018-2 《Java程序设计》第3周学习总结

    20165101刘天野 2017-2018-2 <Java程序设计>第3周学习总结 编程语言的几个发展阶段 类 构造方法与对象的创建 类与程序的基本结构 参数传值 对象的组合 实例成员与类 ...

  6. Unity 简易监听框架

    全局维护一个字典,字典中的key为字符串或者自定义类型,value为委托, using System.Collections; using System.Collections.Generic; us ...

  7. RHEL 7 安装 ngnix

    安装ngnix yum install -y make apr* autoconf automake curl curl-devel gcc gcc-c++ gtk+-devel zlib-devel ...

  8. c# 判断字符串中是否含有汉字,数字

    正则表达式使用时需要引用 using System.Text.RegularExpressions; private void buttonX1_Click(object sender, EventA ...

  9. MATLAB中feval与eval的区别

    feval函数有两种调用形式1.[y1, y2, ...] = feval(fhandle, x1, ..., xn)2.[y1, y2, ...] = feval(fname, x1, ..., x ...

  10. java:RandomAccessFile随机读取文件内容

    RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了.这些记录的大小不必相同:但是其大小和位置必须是可知的.但是该类仅限于操作文件. ...