FFT实现高精度乘法
你应该知道$FFT$是用来处理多项式乘法的吧。
那么高精度乘法和多项式乘法有什么关系呢?
观察这样一个$20$位高精度整数$11111111111111111111$
我们可以把它处理成这样的形式:$\sum_{i=0}^{19}1\times10^i$
这样就变成了一个多项式了!
直接上代码吧(以$Luogu\ P1919$为例):
#include <cmath>
#include <cstdio>
#include <algorithm>
using std::swap;
const int N = 1.4e5 + 10;
const double Pi = acos(-1);
int n, m, r[N], P, ans[N];
char s[N];
struct C { double x, y; } a[N], b[N];
C operator + (C a, C b) { return (C){ a.x + b.x, a.y + b.y }; }
C operator - (C a, C b) { return (C){ a.x - b.x, a.y - b.y }; }
C operator * (C a, C b) { return (C){ a.x * b.x - a.y * b.y, a.x * b.y + b.x * a.y }; }
void FFT(C f[], int opt) {
for(int i = 0; i < n; ++i) if(i < r[i]) swap(f[i], f[r[i]]);
for(int len = 1, nl = 2; len < n; len = nl, nl <<= 1) {
C rot = (C){cos(Pi / len), opt * sin(Pi / len)};
for(int l = 0; l < n; l += nl) {
C w = (C){1, 0}; int r = l + len;
for(int k = l; k < r; ++k, w = w * rot) {
C x = f[k], y = w * f[k + len];
f[k] = x + y, f[k + len] = x - y;
}
}
}
}
int main() {
scanf("%d%s", &n, s + 1);
for(int i = 1; i <= n; ++i) a[i - 1].x = s[n - i + 1] - '0';
scanf("%s", s + 1);
for(int i = 1; i <= n; ++i) b[i - 1].x = s[n - i + 1] - '0';
//将字符串转化为多项式的系数
--n;
for(m = n + n, n = 1; n <= m; n <<= 1, ++P);
for(int i = 0; i < n; ++i) r[i] = (r[i >> 1] >> 1) | ((i & 1) << (P - 1));
//蝴蝶变换FFT
FFT(a, 1), FFT(b, 1);
for(int i = 0; i < n; ++i) a[i] = a[i] * b[i];
FFT(a, -1);
for(int i = 0; i <= m; ++i) ans[i] = (int)(a[i].x / n + .5);
for(int i = 0, tmp1, tmp2; i < m; ++i)
ans[i + 1] += (ans[i] / 10), ans[i] %= 10;
//处理进位(每个系数最多为两位数)
for(int i = m, flag = 0; i >= 0; --i) {
if(ans[i] != 0) flag = 1;
else if(!flag) continue;
printf("%d", ans[i]);
}//flag为前导零标记
return puts("") & 0;
}
$PS:$代码中没有处理$0\times0$的情况,请读者自行处理。
FFT实现高精度乘法的更多相关文章
- P1919 FFT加速高精度乘法
P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...
- BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法
Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...
- SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...
- HDU 1402 A * B Problem Plus (FFT求高精度乘法)
A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)
题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...
- 高精度乘法(FFT)
学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...
- [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. 解题思路:高精度乘法 , 因为数据量比较大, 所以 ...
随机推荐
- 图论&数学:矩阵树定理
运用矩阵树定理进行生成树计数 给定一个n个点m条边的无向图,问生成树有多少种可能 直接套用矩阵树定理计算即可 矩阵树定理的描述如下: 首先读入无向图的邻接矩阵,u-v G[u][v]++ G[v][u ...
- Python ctypes 在 Python 2 和 Python 3 中的不同 // 使用ctypes过程中问题汇总
In Python 2.7, strings are byte-strings by default. In Python 3.x, they are unicode by default. Try ...
- lnmp、lamp、lnmpa一键安装包(Updated: 2016-4-12)
lnmp.lamp.lnmpa一键安装包(Updated: 2016-4-12) 文章目录 脚本特性 安装步骤 如何添加虚拟主机? 如何删除虚拟主机? 如何管理ftp账号? 数据备份 如何管理服务 ...
- monkey测试===Android测试工具Monkey用法简介(转载)
Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序进行压力测试.Monkey ...
- 64_l5
libsmartcols-2.29.1-2.fc26.x86_64.rpm 13-Feb-2017 07:04 150590 libsmartcols-devel-2.29.1-2.fc26.i686 ...
- vue知识点(1)
处理用户输入 v-on指令添加一个事件监听器 div id="app-5"> <p>{{ message }}</p> <button v-on ...
- The content of element type "resultMap" must match "(constructor?,id*,result*,association*,collectio
The content of element type "resultMap" must match "(constructor?,id*,result*,associa ...
- python 使用国内源安装软件
python linux 等 使用国内源安装软件 速度更快 你值得拥有 ! 豆瓣源:pip install -i https://pypi.douban.com/simple/ 阿里源:pip ins ...
- C# 笔记——数据类型
一张图读懂C#数据类型:
- C#ActiveX控件开发
1.新建项目,选择C#,选择.NET Framework2.0,新建一个Windows窗体控件库项目,命名为ActiveXDemo; 2.右击ActiveXDem项目,选择属性——应用程序——程序集信 ...