学会了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. jq 全选、反选、判断选中的条数

    1.全选或全不选.当勾选全选按钮#selectAll旁边的复选框#all时,列表中的选项全部选中,反之取消勾选则列表中的选项全部为未选中状态. $("#all").click(fu ...

  2. 牛客网 牛客小白月赛1 C.分元宵-快速幂

    C.分元宵   链接:https://www.nowcoder.com/acm/contest/85/C来源:牛客网 这个题就是快速幂,注意特判,一开始忘了特判,wa了一发. 代码: 1 #inclu ...

  3. latex beamer 插入代码

    有网友在beamer中使用mcode也就是 listings 输出源代码时遇到如下错误: Runaway argument?! Paragraph ended before \lst@next was ...

  4. python 当pip不能用的时候可以去找python安装包

    初学python,一直pip安装各种包,突然间有一天pip莫名其妙不能用了,除了pip help全部都是没反应 百度好像没人出现过pip挂掉的情况 花了一小时修复pip,卸载啊,路径啊,全部无效 百度 ...

  5. TreeSet, LinkedHashSet and HashSet 的区别

    1. 介绍 TreeSet, LinkedHashSet and HashSet 在java中都是实现Set的数据结构 # TreeSet的主要功能用于排序 # LinkedHashSet的主要功能用 ...

  6. JAVA算法总结_时间复杂度_Demo

    JAVA面试中经常问到排序算法问题,本人结合网络上一些资源整理了编写一下常用的Demo,并附带运行结果,希望能帮助到大家. /** * @Title: 冒泡排序 * @Description: 将数组 ...

  7. 左偏树 / 非旋转treap学习笔记

    背景 非旋转treap真的好久没有用过了... 左偏树由于之前学的时候没有写学习笔记, 学得也并不牢固. 所以打算写这么一篇学习笔记, 讲讲左偏树和非旋转treap. 左偏树 定义 左偏树(Lefti ...

  8. Raid分类说明 (from mongodb权威指南)

    RAID(Redundant Array of Independent Disk,独立磁盘冗余阵列,旧称Redundant Array of InexpensiveDisk,廉价磁盘冗余阵列) 是一种 ...

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

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

  10. hdu1034 简单模拟

    这里开一个二维数组.num[105][2];   我也不知道N有多少,随便开的,  那么这里num[i][0] 表示当前 第 i 个人拥有的糖果数,num[i][1]表示他上面一个人分给他的糖果数.详 ...