题意:计算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. 爬虫基本库之beautifulsoup

    一.beautifulsoup的简单使用 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据.官方解释如下: Beautiful Soup提供一些简单的.pyt ...

  2. iOS iPhone X 适配启动图片

    刚出了Xcode9正式版 迫不及待地下载 使用了 保密了这么久的iPhone X 模拟器 运行起来这个样子 上下都留白不正常 这必须匹配新的启动图才行,马上查苹果开发文档 get it!!!! 添加了 ...

  3. linux常用技巧(资料)

    Linux中查看程序安装位置 如果是rpm的安装,用rpm -ql如果是一般安装 用 whereis 或者 find find /usr -name catalina.out======== 如何查看 ...

  4. Ubuntu 12.04下boost库的交叉编译

    oost Ver: 1.55.0Compiler : GNU gcc 4.6 for ARM 1. 确保ARM编译成功安装,并配置好环境变量.2. 解压boost压缩包 3. 进入目录执行./boot ...

  5. jQuery带缩略图焦点图插件

    在线演示 本地下载

  6. Kubernetes 部署Mysql 主从复制集群

    Mysql主从参考文章: https://www.jianshu.com/p/509b65e9a4f5 http://blog.51cto.com/ylw6006/2071864 Statefulse ...

  7. freemarker内建函数介绍

    Sequence的内置函数1.sequence?first 返回sequence的第一个值.2.sequence?last 返回sequence的最后一个值.3.sequence?reverse 将s ...

  8. 常用 GDB 命令中文速览

    转自:https://linux.cn/article-8900-1.html?utm_source=index&utm_medium=moremore 目录 break -- 在指定的行或函 ...

  9. qq在线客服代码

    http://wpa.qq.com/msgrd?v=3&uin=1456262869&site=www.cactussoft.cn&menu=yes

  10. HDFS相关概念

    数据块 每个磁盘都有默认的数据块大小,这是磁盘进行数据读写的最小单位.构建与单个磁盘之上的文件系统通过磁盘块来管理该文件系统中的快.该文件系统块的大小可以使磁盘块的整数倍.文件系统块一般为几千字节,而 ...