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 ...
随机推荐
- 查找担保圈-step1-担保圈表函数
USE [test]; GO /****** Object: UserDefinedFunction [dbo].[f_findrecycle] Script Date: 2019/7/8 14:37 ...
- Mybatis(三) 动态SQL
if + where 用法 1. if 元素来实现多条件查询 1.1 UserMapper.xml配置文件 <!--查询用户列表 (if)--> <select id="g ...
- spring配置文件拆分策略及方法
一.拆分策略 如果一个开发人员负责一个模块,我们采用公用配置(包括数据源.事务等)+每个系统模块一个单独配置文件(包括Dao.Service.Web控制器)的形式 如果是按照分层进行的分工,我们采用公 ...
- 如何在LinuxKernel中操作file(set_fs與get_fs)
在Kernel 中,照理說能存取至 0 ~ 4GB.但是實作層面卻是只能讓我們使用到3GB ~ 4GB 這會導致我們無法使用open(),write()這些在user space下的function. ...
- js、jQuery各种高度
height.js ```$(document).height(); //整个网页的高度 $(window).height(); //浏览器可视窗口的高度 $(window).scrollTop(); ...
- Tomcat的安全性
Web应用程序的一些内容是受限的,只有授权的用户在提供了正确的用户名和密码后才能查看他们,servlet技术支持通过配置部署 描述器(web.xml)来对这些内容进行访问控制,那么web容器是怎么样支 ...
- 十大经典排序算法(Python,Java实现)
参照:https://www.cnblogs.com/wuxinyan/p/8615127.html https://www.cnblogs.com/onepixel/articles/7674659 ...
- Volatile可见性 与 Synchronization原子性的优化
Volatile可见性 比如现在我们有这样一段代码:线程等待另一个线程将数据装载完就输出success,可是最后程序一直卡在while循环里没有往下执行. public class VolatileD ...
- MySQL安装过程中遇到的错误代码为1045的解决方法
mysql的安装包,及其图形化破解软件:https://pan.baidu.com/s/1PIzaEGpC9QEPUwZ8OowhCw 二级压缩包下边的 视图化管理软件:Navicat.exe 发 ...
- 分布式的几件小事(四)dubbo负载均衡策略和集群容错策略
1.dubbo负载均衡策略 ①random loadbalance 策略 默认情况下,dubbo是random loadbalance 随机调用实现负载均衡,可以对provider不同实例设置不同的权 ...