HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算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实现高精度乘法)的更多相关文章
- 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 ...
- hdu 1402 A * B Problem Plus FFT
/* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...
- HDU - 1402 A * B Problem Plus FFT裸题
http://acm.hdu.edu.cn/showproblem.php?pid=1402 题意: 求$a*b$ 但是$a$和$b$的范围可以达到 $1e50000$ 题解: 显然...用字符串模拟 ...
- HDU 1402 A * B Problem Plus (FFT模板题)
FFT模板题,求A*B. 用次FFT模板需要注意的是,N应为2的幂次,不然二进制平摊反转置换会出现死循环. 取出结果值时注意精度,要加上eps才能A. #include <cstdio> ...
- P1919 FFT加速高精度乘法
P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...
- HDU 1402 A * B Problem Plus 快速傅里叶变换 FFT 多项式
http://acm.hdu.edu.cn/showproblem.php?pid=1402 快速傅里叶变换优化的高精度乘法. https://blog.csdn.net/ggn_2015/artic ...
- SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...
- FFT实现高精度乘法
你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...
- BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法
Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...
随机推荐
- 【Head First Servlets and JSP】笔记10:请求分派(RequestDispatcher)
1.让其它组件接管全部请求. package com.example.web; import com.example.model.BeerExpert; import javax.servlet.*; ...
- Squid 正向代理配置
Squid 正向代理配置 1.删除主配置文件重写写入配置 rm -f /etc/squid/squid.conf 2.重新写入配置正向代理 vim /etc/squid/squid.conf # 监听 ...
- PHP类的变量与成员,及其继承、访问与重写要注意的问题
PHP的类及其实例: <?php ?> 后期静态绑定:为了避免子类重写静态属性后,使用继承来的方法仍然方法父类的静态属性,PHP5.3增加了一个新的语法,后期静态绑定,使用static关 ...
- INSPIRED启示录 读书笔记 - 第41章 产品经理的反省清单
十大问题 1.产品能吸引目标消费者的关注吗? 2.产品的设计是否人性化,是否易于操作? 3.产品能在竞争中取胜吗?即使是面对未来风云变化的市场,依旧有取胜的把握吗? 4.我了解目标用户吗?产品(不是理 ...
- Golang html encoding解析
自动解析html页面的编码格式: 需要依赖 golang.org/x/text 和 golang.org/x/net 这两个外部库 package main import ( "net/ht ...
- Gitblit搭建及Git协作开发流程
1. 概述 目前主流的是git作为自己代码管理,但是采用github需要付费才能够使用,如果不付费,代码需要公开.创业团队及小型开发团队都有必要搭建自己的代码服务器,自己摸索需要一定的时间,会赶不及项 ...
- Linux系统LVM分区减容扩容
Linux系统LVM分区减容扩容 目标:将VolGroup-lv_home缩小到20G,并将剩余的空间添加给VolGroup-lv_root 1.首先查看磁盘使用情况 [root@localhost ...
- SQL中的5种常用的聚集函数
首先你要知道 where->group by->having->order by/limit ,这个就是写sql语句时的顺序 常用的5个聚集函数: Max ...
- windows编译hadoop 2.x Hadoop-eclipse-plugin插件
本文转载至:http://blog.csdn.net/congcong68/article/details/42098391 一.简介 Hadoop2.x之后没有Eclipse插件工具,我们就不能在E ...
- 循环插入一条数据的sql写法
DECLARE @i INTSET @i = 1WHILE @i > 0 BEGIN DECLARE @TransportFormMstID BIGINT; DECLARE @TradeOrde ...