学会了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. Codeforces Gym101606 D.Deranging Hat (2017 United Kingdom and Ireland Programming Contest (UKIEPC 2017))

    D Deranging Hat 这个题简直了,本来想的是冒泡排序然后逆着输出来的,后来发现不对,因为题目上求的是最优解,而且冒泡的话,输出结果有的超出10000行了,所以就是把一开始的,排好序的字母标 ...

  2. WEB接口测试之Jmeter接口测试自动化 (三)(数据驱动测试)

     接口测试与数据驱动 1简介 数据驱动测试,即是分离测试逻辑与测试数据,通过如excel表格的形式来保存测试数据,用测试脚本读取并执行测试的过程. 2 数据驱动与jmeter接口测试 我们已经简单介绍 ...

  3. BZOJ——2101: [Usaco2010 Dec]Treasure Chest 藏宝箱

    http://www.lydsy.com/JudgeOnline/problem.php?id=2101 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit:  ...

  4. logging模块详解以及常见代码

    1.在django中获取客户端IP地址: if 'HTTP_X_FORWARDED_FOR' in request.META: ip = request.META['HTTP_X_FORWARDED_ ...

  5. 使用和不使用navigationbar分别处理显示和返回页面

    不使用navigationbar的情况下 AnnounceViewController *pushView = [[AnnounceViewController alloc]init];pushVie ...

  6. 您也使用托管C++吗? 【转】

    http://blog.csdn.net/Muf/article/details/656920 转向.NET后,手头上往往仍有旧的模块要重用.也许这些模块是Delphi写的,也许是C/C++写的,或者 ...

  7. 赵雅智_android_frame动画

    在開始实例解说之前,先引用官方文档中的一段话: Frame动画是一系列图片依照一定的顺序展示的过程,和放电影的机制非常相似.我们称为逐帧动画.Frame动画能够被定义在XML文件里,也能够全然编码实现 ...

  8. Java自定义注解和运行时靠反射获取注解

    转载:http://blog.csdn.net/bao19901210/article/details/17201173/ java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编 ...

  9. Balanced Binary Tree——数是否是平衡,即任意节点左右字数高度差不超过1

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  10. Laravel建站01--开发环境部署

    内容导航 安装git 安装composer 安装Laravel 既然是开发环境,就需要源代码管理.这里使用git来管理. 一:部署开发环境之前安装git 在 Linux 上安装git 如果你想在 Li ...