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 ...
随机推荐
- Zuul网关跨域问题
1.跨域就指着协议,域名,端口不一致,出于安全考虑,跨域的资源之间是无法交互的.简单说就是协议不通,域名不通,端口不同都会产生跨域问题 Access-Control-Allow-Origin是HTML ...
- [转帖]Intro Guide to Dockerfile Best Practices
Intro Guide to Dockerfile Best Practices By Tibor Vass July 02 2019 https://blog.docker.com/2019/ ...
- 使用 vscode将本地项目上传到github、从github克隆项目以及删除github上的某个文件夹
安装Git后,可以看到windows环境下有两个命令输入窗口Git CMD 和Git Bash Git GUI是可视化图形界面 Git中的Bash是基于CMD的,在CMD的基础上增添一些新的命令与功能 ...
- Zabbix-自带监控项与Kye
开启主机自动发现 ⦁选择配置 ->自动发现 ->创建自动发现->ip范围必须连续,不连续的话逗号分开 ⦁更新间隔 -> 2s ->更新 ⦁检查 -> 选择新的 -& ...
- python网络爬虫(6)爬取数据静态
爬取静态数据并存储json import requests import chardet from bs4 import BeautifulSoup import json user_agent='M ...
- 配置Mysql远程连接
一.赋予某个用户权限 1.赋予权限格式:grant 权限 on 数据库对象 to 用户@IP(或者相应正则) 注:可以赋予select,delete,update,insert,index等权限精确到 ...
- 使用@ResponseBody输出JSON
添加jackson依赖 添加@ResponseBody 测试: 原理: 当一个处理请求的方法标记为@ResponseBody时,就说明该方法需要输出其他视图(json.xml),SpringMVC通过 ...
- O019、通过例子学习 Keystone
参考https://www.cnblogs.com/CloudMan6/p/5373311.html 上节介绍了 Keystone 的核心概念.本节我们通过“查询可用 image”这个操作让大家对 ...
- 简单了解journalctl
journalctl 命令 journalctl是什么以及作用? journalctl 用来查询 systemd-journald 服务收集到的日志.systemd-journald 服务是 syst ...
- 上传模板到SAP资源库
事物代码:SMW0 -WEB资源库 如果创建后上载本地模板报错,说明没有维护文件类型,需要在导航栏的设置里维护MIME类型添加新的文件后缀名 维护好后再上载模板 ABAP下载模板:以下FORM可以参考 ...