学会了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)的更多相关文章

  1. FFT实现高精度乘法

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

  2. P1919 FFT加速高精度乘法

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

  3. [vijos P1040] 高精度乘法

    如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了, ...

  4. 【PKU1001】Exponentiation(高精度乘法)

    Exponentiation Time Limit: 500MS   Memory Limit: 10000K Total Submissions: 145642   Accepted: 35529 ...

  5. hdu 1042 N!(高精度乘法 + 缩进)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 题目大意:求n!, n 的上限是10000. 解题思路:高精度乘法 , 因为数据量比较大, 所以 ...

  6. hdu 1042 N!(高精度乘法)

    Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!   Input One N in ...

  7. Vijos 1040 高精度乘法

    描述 高精度乘法 输入:两行,每行表示一个非负整数(不超过10000位) 输出:两数的乘积. 样例1 样例输入1 99 101 样例输出1 9999 题解 这道题和之前的Vijos 1010 清帝之惑 ...

  8. 【POJ 1001】Exponentiation (高精度乘法+快速幂)

    BUPT2017 wintertraining(15) #6A 题意 求\(R^n\) ( 0.0 < R < 99.999 )(0 < n <= 25) 题解 将R用字符串读 ...

  9. 【BZOJ5300】[CQOI2018]九连环 (高精度,FFT)

    [BZOJ5300][CQOI2018]九连环 (高精度,FFT) 题面 BZOJ 洛谷 题解 去这里看吧,多么好 #include<iostream> #include<cstdi ...

随机推荐

  1. js 克隆数据 (数组的深浅拷贝)

    var a1 = [1,2,3]; var a2 = a1; a2[0] = 90; console.log(a1[0]) //90 解析:数组是复合的数据类型,直接复制的话,只是复制了指向底层数据结 ...

  2. 唤醒你的大脑 --- javascript冒泡排序

    var a; a = [1, 2, 3, 11, 55, 5, 0, 44]; (function bubbleSort() { for (var i = 0; i <= a.length - ...

  3. js中call、apply、bind那些事2

    前言 回想起之前的一些面试,几乎每次都会问到一个js中关于call.apply.bind的问题,比如… 怎么利用call.apply来求一个数组中最大或者最小值 如何利用call.apply来做继承 ...

  4. Java基础:抽象类和接口

    转载请注明出处:jiq•钦's technical Blog 一.引言 基于面向对象五大原则中的以下两个原则,我们应该多考虑使用接口和抽象类: 里氏替换原则:子类能够通过实现父类接口来替换父类,所以父 ...

  5. who命令

    who1.c #include <stdio.h>#include <utmp.h>#include <fcntl.h>#include <unistd.h& ...

  6. shell脚本编写-自动部署及监控

    1.编写脚本自动部署反向代理.web.nfs: I.部署nginx反向代理两个web服务,调度算法使用加权轮询 II.所有web服务使用共享存储nfs,保证所有web都对其有读写权限,保证数据一致性: ...

  7. Linux安装Java/Maven

    所需文件:jdk 下载 安装Java INSTALL_PATH=/opt/soft TAR_FILE=/mnt/d/resources/soft/jdk-8u152-linux-x64.tar.gz ...

  8. mysql 查看当前连接数

    http://www.cnblogs.com/pcdelphi/archive/2009/10/31/2017990.html   实战经验: >登录到mysql数据库的终端 >show ...

  9. spl_autoload_register的使用

    class Loader{ static function loadClass($class) { $class = $class.'.php'; if(file_exists($class)) { ...

  10. 显示和隐藏Mac隐藏文件的命令

    显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏Mac隐藏文件的命令:defaults writ ...