你应该知道$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实现高精度乘法的更多相关文章

  1. P1919 FFT加速高精度乘法

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

  2. BZOJ2179: FFT快速傅立叶 FFT实现高精度乘法

    Code: #include <cstdio> #include <algorithm> #include <cmath> #include <cstring ...

  3. SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法

    SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...

  4. 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 ...

  5. HDU - 1402 A * B Problem Plus (FFT实现高精度乘法)

    题意:计算A*B,A,B均为长度小于50000的整数. 这是FFT在大整数相乘中的一个应用,我本来想用NTT做的,但NTT由于取模很可能取炸,所以base必须设得很小,而且效率也比不上FFT. A和B ...

  6. 高精度乘法(FFT)

    学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...

  7. [vijos P1040] 高精度乘法

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

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

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

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

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

随机推荐

  1. 数据结构&字符串:可持久化字典树

    利用可持久化Trie树实现范围内取值异或最大值 如果标题没有表达清楚意思,可以看这里的题干: 然后根据异或的性质,异或一个数两次相当于没有异或,那么我们可以维护一个异或前缀和 有了异或前缀和之后我们就 ...

  2. POJ 3279 Fliptile ( 开关问题)

    题目链接 Description Farmer John knows that an intellectually satisfied cow is a happy cow who will give ...

  3. POJ 3061 Subsequence ( 尺取法)

    题目链接 Description A sequence of N positive integers (10 < N < 100 000), each of them less than ...

  4. mac系统用docker安装oracle数据库

    oracle没有mac可用的版本,最好的办法是通过docker安装 一.下载docker 1.通过brew下载 brew cask install docker 2.手动下载(需要vpn) https ...

  5. React组件生命周期小结

    React组件生命周期小结 下面所写的,只适合前端的React.(React也支持后端渲染,而且和前端有点小区别,不过我没用过.) 相关函数 简单地说,React Component通过其定义的几个函 ...

  6. 八大疯狂的HTML5 Canvas及WebGL动画效果——8 CRAZY ANIMATIONS WITH WEBGL AND HTML5 CANVAS【收藏】

    HTML5, WebGL and Javascript have changed the way animation used to be. Past few years, we can only a ...

  7. spring cloud config 详解

    Spring Cloud 为开发人员提供了一系列的工具来快速构建分布式系统的通用模型 .例如:配置管理.服务发现.断路由.智能路由.微代理.控制总线.一次性Token.全局锁.决策竞选.分布式sess ...

  8. ES6 新增的一些东西

    一.常量 不允许重复定义 const a='HELLO' const a='world'//报错Uncaught SyntaxError: Identifier 'a' has already bee ...

  9. caffe Python API 之 数据输入层(Data,ImageData,HDF5Data)

    import sys sys.path.append('/projects/caffe-ssd/python') import caffe4 net = caffe.NetSpec() 一.Image ...

  10. JS函数和变量名称冲突

    在JS中如果函数名与变量名冲突,JS是怎么执行的? <script> console.log(sum);//function sum(){} function sum(){} var su ...