FFT用于高效大数乘法(当模板用)
转载来源:https://blog.csdn.net/zj_whu/article/details/72954766
#include <cstdio>
#include <cmath>
#include <complex>
#include <cstring>
using namespace std;
const double PI(acos(-1.0));
typedef complex<double> C;
const int N = (1 << 20);
int ans[N];
C a[N], b[N];
char s[N], t[N];
void bit_reverse_swap(C* a, int n) {
for (int i = 1, j = n >> 1, k; i < n - 1; ++i) {
if (i < j) swap(a[i], a[j]);
// tricky
for (k = n >> 1; j >= k; j -= k, k >>= 1) // inspect the highest "1"
;
j += k;
}
}
void FFT(C* a, int n, int t) {
bit_reverse_swap(a, n);
for (int i = 2; i <= n; i <<= 1) {
C wi(cos(2.0 * t * PI / i), sin(2.0 * t * PI / i));
for (int j = 0; j < n; j += i) {
C w(1);
for (int k = j, h = i >> 1; k < j + h; ++k) {
C t = w * a[k + h], u = a[k];
a[k] = u + t;
a[k + h] = u - t;
w *= wi;
}
}
}
if (t == -1) {
for (int i = 0; i < n; ++i) {
a[i] /= n;
}
}
}
int trans(int x) {
return 1 << int(ceil(log(x) / log(2) - 1e-9)); // math.h/log() 以e为底
}
int main() {
// freopen("test0.in","r",stdin);
// freopen("test0b.out","w",stdout);
int n, m, l;
for (; ~scanf("%s%s", s, t);) {
n = strlen(s);
m = strlen(t);
l = trans(n + m - 1); // n次*m次不超过n+m-1次
for (int i = 0; i < n; ++i) a[i] = C(s[n - 1 - i] - '0');
for (int i = n; i < l; ++i) a[i] = C(0);
for (int i = 0; i < m; ++i) b[i] = C(t[m - 1 - i] - '0');
for (int i = m; i < l; ++i) b[i] = C(0);
FFT(a, l, 1); //把A和B换成点值表达
FFT(b, l, 1);
for (int i = 0; i < l; ++i) //点值做乘法
a[i] *= b[i];
FFT(a, l, -1); //逆DFT
for (int i = 0; i < l; ++i) ans[i] = (int)(a[i].real() + 0.5);
ans[l] = 0; // error-prone :'l' -> '1'
for (int i = 0; i < l; ++i) {
ans[i + 1] += ans[i] / 10;
ans[i] %= 10;
}
int p = l;
for (; p && !ans[p]; --p)
;
for (; ~p; putchar(ans[p--] + '0'))
;
puts("");
}
return 0;
}
FFT用于高效大数乘法(当模板用)的更多相关文章
- 51nod 1027大数乘法
题目链接:51nod 1027大数乘法 直接模板了. #include<cstdio> #include<cstring> using namespace std; ; ; ; ...
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- ACM学习历程—51NOD1028 大数乘法V2(FFT)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1028 题目大意就是求两个大数的乘法. 但是用普通的大数乘法,这 ...
- 1028 大数乘法 V2(FFT or py)
1028 大数乘法 V2 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B ...
- Java 大数、高精度模板
介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...
- FFT教你做乘法(FFT傅里叶变换)
题目来源:https://biancheng.love/contest/41/problem/C/index FFT教你做乘法 题目描述 给定两个8进制正整数A和B(A和B均小于10000位),请利用 ...
- [POJ] #1001# Exponentiation : 大数乘法
一. 题目 Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 156373 Accepted: ...
- hdu_1042(模拟大数乘法)
计算n! #include<cstring> #include<cstdio> using namespace std; ]; int main() { int n; whil ...
- hdu1313 Round and Round We Go (大数乘法)
Problem Description A cyclic number is an integer n digits in length which, when multiplied by any i ...
随机推荐
- Replace Words
In English, we have a concept called root, which can be followed by some other words to form another ...
- Java基础(十)
复习 静态方法与成员方法 //另一个类里的静态和成员方法 public class MyClass { //静态方法 public static void method2() { System.out ...
- ES6简单初识
ES常用命令介绍 函数的Rest参数和扩展 Promise使用 Module.exports和ES6 import/export的使用 promise使用 promise 为了解决callback嵌套 ...
- ajax怎么打开新窗口具体如何实现
var newwindow=window.open('about:blank'); jQuery.ajax({ type: 'POST', url: 'clickRate.action', dataT ...
- cmd_操作MySQL数据库
建议用 TXT 文档来写代码 然后粘贴至cmd命令直接运行创建students库,表名为student,表包含以下字段: id(主键) name(姓名) age(年龄) sex(性别) sc(综合积分 ...
- Python简单主机批量管理工具
一.程序介绍 需求: 简单主机批量管理工具 需求: 1.主机分组 2.主机信息使用配置文件 3.可批量执行命令.发送文件,结果实时返回 4.主机用户名密码.端口可以不同 5.执行远程命令使用param ...
- Linux-2.1vim简单使用
1.用vim打开文件,文件内容显示在终端,命令模式无法编辑 vim 1.txt vi 1.txt 2.使用a,i,o,shift+o编辑文件,按Esc退出编辑模式 i 插入在光标前 a 插入在光标后 ...
- idea自定义注释
类配置位置: 方法配置位置 配置内容 * * @Author *** * @Date $date$ $time$ $param$ * @return $return$ * @Description * ...
- IOS手机伪类a:active失效
IOS手机伪类a:active失效:点击更改颜色,松开恢复 解决方案:OS系统的移动设备中,需要在按钮元素或body/html上绑定一个touchstart事件才能激活:active状态 docume ...
- 安装MySQL数据库并开启远程访问
一.安装MySQL数据库 MySQL安装在系统盘下(C:\Program Files),方便系统备份. 1.双击安装程序,勾选“I accept the license terms”,点击“Next” ...