高精度乘法(FFT)
学会了FFT之后感觉自己征服了世界!
当然是幻觉...
不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果.
一般比较凶残的计数动规题都需要FFT(n<=1e9).
下面是高精度乘法的板子.
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<ctime>
- #include<cmath>
- #include<algorithm>
- #include<queue>
- #include<set>
- #include<map>
- #include<iomanip>
- using namespace std;
- #define LL long long
- #define up(i,j,n) for(int i=j;i<=n;i++)
- #define pii pair<int,int>
- #define db double
- #define eps 1e-4
- #define FILE "dealing"
- int read(){
- int x=0,f=1,ch=getchar();
- while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
- while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0',ch=getchar();}
- return x*f;
- }
- const int maxn=401000,inf=1000000000000000LL,limit=20000,mod=9973;
- bool cmin(int& a,int b){return a>b?a=b,true:false;}
- bool cmax(int& a,int b){return a<b?a=b,true:false;}
- namespace FFT{
- int ans[maxn];
- db pi=(acos(-1.0));
- struct cp{
- db x,y;
- cp(db x=0,db y=0):x(x),y(y){}
- inline cp operator+(const cp& b){return cp(x+b.x,y+b.y);}
- inline cp operator-(const cp& b){return cp(x-b.x,y-b.y);}
- inline cp operator*(const cp& b){return cp(x*b.x-y*b.y,x*b.y+y*b.x);}
- }w[maxn],a[maxn],b[maxn];
- int L,H,R[maxn];
- inline void swap(cp& x,cp& y){cp t(x);x=y;y=t;}
- void FFT(cp* a,int f){
- up(i,0,L-1)if(i<R[i])swap(a[i],a[R[i]]);
- for(int len=2;len<=L;len<<=1){
- int l=len>>1;
- cp wn(cos(pi/l),f*sin(pi/l));
- up(i,1,l-1)w[i]=w[i-1]*wn;
- for(int st=0;st<L;st+=len){
- for(int k=0;k<l;k++){
- cp x=a[st+k],y=w[k]*a[st+k+l];
- a[st+k]=x+y,a[st+k+l]=x-y;
- }
- }
- }
- if(f==-1)up(i,0,L-1)a[i].x/=L;
- }
- void prepare(){
- w[0].x=1;
- up(i,0,L)R[i]=(R[i>>1]>>1)|((i&1)<<(H-1));
- }
- void solve(int* c,int *d,int n,int m,int* ch){
- up(i,0,n-1)a[i].x=c[i+1],a[i].y=0;
- up(i,0,m-1)b[i].x=d[i+1],b[i].y=0;
- n++,m++;
- for(H=0,L=1;L<n+m-1;H++)L<<=1;
- prepare();
- FFT(a,1);FFT(b,1);
- up(i,0,L-1)a[i]=a[i]*b[i];
- FFT(a,-1);
- up(i,0,n+m-2)ch[i+1]=(int)(a[i].x+0.5);
- return;
- }
- };
- char s[maxn];
- int a[maxn],b[maxn],n,m,ans[maxn];
- int main(){
- freopen(FILE".in","r",stdin);
- freopen(FILE".out","w",stdout);
- scanf("%s",s+1);
- n=strlen(s+1);
- for(int i=1;i<=n;i++)a[i]=s[n-i+1]-'0';
- scanf("%s",s+1);
- m=strlen(s+1);
- for(int i=1;i<=m;i++)b[i]=s[m-i+1]-'0';
- FFT::solve(a,b,n,m,ans);
- int len=n+m;
- for(int i=1;i<=len;i++)if(ans[i]>=10)ans[i+1]+=ans[i]/10,ans[i]%=10;
- while(ans[len]>=10)ans[len+1]+=ans[len]/10,ans[len]%=10,len++;
- while(!ans[len]&&len>1)len--;
- for(int i=len;i>=1;i--)printf("%d",ans[i]);
- return 0;
- }
高精度乘法(FFT)的更多相关文章
- FFT实现高精度乘法
你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...
- P1919 FFT加速高精度乘法
P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...
- [vijos P1040] 高精度乘法
如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了, ...
- 【PKU1001】Exponentiation(高精度乘法)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 145642 Accepted: 35529 ...
- hdu 1042 N!(高精度乘法 + 缩进)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目大意:求n!, n 的上限是10000. 解题思路:高精度乘法 , 因为数据量比较大, 所以 ...
- hdu 1042 N!(高精度乘法)
Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in ...
- Vijos 1040 高精度乘法
描述 高精度乘法 输入:两行,每行表示一个非负整数(不超过10000位) 输出:两数的乘积. 样例1 样例输入1 99 101 样例输出1 9999 题解 这道题和之前的Vijos 1010 清帝之惑 ...
- 【POJ 1001】Exponentiation (高精度乘法+快速幂)
BUPT2017 wintertraining(15) #6A 题意 求\(R^n\) ( 0.0 < R < 99.999 )(0 < n <= 25) 题解 将R用字符串读 ...
- 【BZOJ5300】[CQOI2018]九连环 (高精度,FFT)
[BZOJ5300][CQOI2018]九连环 (高精度,FFT) 题面 BZOJ 洛谷 题解 去这里看吧,多么好 #include<iostream> #include<cstdi ...
随机推荐
- [转载][FPGA]有限状态机FSM学习笔记(二)
1. Mealy和Moore状态机的互换 对于给定的时序逻辑功能,可以用Mealy机实现,也可以用Moore机实现.根据Moore机比Mealy机输出落后一个周期的特性,可以实现两种状态机之间的转换. ...
- 遇到的cannot find module 'xxx' 问题
昨天配置新项目的环境,明明都npm inatsll都安装了,再次运行的时候一直报错,缺少模块.我以为就一个没安装上,就手动npm install -g 'xxx' :结果一个装好了,又继续蹦出cann ...
- git history 记录(上传到 issu-170 )
一.上传到gitlab 本地issu-170落后git很多,发生冲突的要手动修改. 2000 cd robot_demo_0226_ws/ 2001 ls 2002 cd IGV01-SW-170 2 ...
- Wireshark如何选择多行
Wireshark如何选择多行 在Wireshark中,用户经常需要选择几行,然后进行批量操作,如导出或者分析.但Wireshark没有提供通过鼠标直接选择多行的功能.这个时候,用户需要采用标记分 ...
- The 2016 ACM-ICPC Asia China-Final Contest Promblem D
显然答案具有单调性,可以二分.问题是 我们二分出一个 堆数,该怎么判定能否达到这个堆数呢? 我们可以很简单的用调整法证明,最底下的一层的冰淇淋肯定是最小的那些,往上叠加的话我们再贪心的让较少的放在较小 ...
- 上传图片/文件到server
package yao.camera.util; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; impo ...
- shell(2):正则表达式
一.整理正则表达式博客 (1)正则 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则. 在linux中,通配符是由shel ...
- Hdu-1565 方格取数(1) (状态压缩dp入门题
方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- Qt在线讲座之QML脚本书写规范
时间:2016年3月1日晚7:30 在线讲座:http://qtdream.com主页处就可以收看直播(详见主页提示) 參与对象:对Qt跨平台开发框架感兴趣的朋友们.当然了,假设你是大牛.也可以旁听一 ...
- kubernetes调度之pod优先级和资源抢占
系列目录 Pod可以拥有优先级.优先意味着相对于其它pod某个pod更为重要.如果重要的pod不能被调度,则kubernetes调度器会优先于(驱离)低优先级的pod来让处于pending状态的高优先 ...