学会了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. [Machine Learning with Python] Data Visualization by Matplotlib Library

    Before you can plot anything, you need to specify which backend Matplotlib should use. The simplest ...

  2. SpringMVC中 Controller的 @ResponseBody注解分析

    需求分析:需要 利用    out 对象返回给财付通是否接收成功 .那么将需要如下代码: /** * 返回处理结果给财付通服务器. * @param msg: Success or fail. * @ ...

  3. 常见编码bug

    1.result.replace("abc","bcd");错误 改成result= result.relace("abc","b ...

  4. iOS -- SKTransition类

      SKTransition类 继承自 NSObject 符合 NSObject(NSObject) 框架  /System/Library/Frameworks/SpriteKit.framewor ...

  5. 老毛桃winpe优盘启动系统个性修改全攻略

    PE优盘系统也有很多:大白菜.老毛桃.深度.通用PE工具箱.U大师.电脑店……这些PE优盘系统大多都会捆绑软件安装.更改主页等,一不小心,你就中招.虽然有些是可以自己去取消,但是启动画面还是带有各种L ...

  6. nopCommerce从无到有01-初探nopCommerce

    nopCommerce框架的基本结构: 该结构可以参考DDD(领域驱动设计)模式. (注:上图源自他人文章,具体出处不祥,在此引用,感谢原创) nopcommerce官方地址:http://www.n ...

  7. django 实现下载功能

    from django.http import FileResponse def download(request): if..... file=open([path],'rb') response= ...

  8. servlet基础梳理(一)

    将近一个月没看servlet了,再加上第一次学习也没有深入.仅仅是笼统的看了一遍,编了一点基础案例就过去了,如今再去看感觉跟没学过一样.这里再用一点时间把这些基础都梳理一下,加深印象并为以后高速复习做 ...

  9. MySQL 查询某个列中同样值的数量统计

    数据如今是这种,我想确定出type列中的news和image....甚至以后有其它值,他们分别有多少个. SELECT type, count(1) AS counts FROM material G ...

  10. 请问如何突破”所选文件超出了文件的最大值设定:25.00 Mb“限制

        警告消息             这个限制 并没有 设置项, 必须 修改 源码才可以.     打开 web/static/src/js/views/form_widgets.js       ...