FFT 高精度乘法模板
#define L(x) (1 << (x))
const double PI = acos(-1.0);
const int N = 1e7 + 10;
double ax[N], ay[N], bx[N], by[N];
char sa[N / 2], sb[N / 2];
int sum[N];
int x1[N], x2[N];
int revv(int x, int bits) {
int ret = 0;
for (int i = 0; i < bits; i++) {
ret <<= 1;
ret |= x & 1;
x >>= 1;
}
return ret;
}
void fft(double * a, double * b, int n, bool rev) {
int bits = 0;
while (1 << bits < n) ++bits;
for (int i = 0; i < n; i++) {
int j = revv(i, bits);
if (i < j)
swap(a[i], a[j]), swap(b[i], b[j]);
}
for (int len = 2; len <= n; len <<= 1) {
int half = len >> 1;
double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len);
if (rev) wmy = -wmy;
for (int i = 0; i < n; i += len) {
double wx = 1, wy = 0;
for (int j = 0; j < half; j++) {
double cx = a[i + j], cy = b[i + j];
double dx = a[i + j + half], dy = b[i + j + half];
double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx;
a[i + j] = cx + ex, b[i + j] = cy + ey;
a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;
double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;
wx = wnx, wy = wny;
}
}
}
if (rev) {
for (int i = 0; i < n; i++)
a[i] /= n, b[i] /= n;
}
}
int sol(int a[], int na, int b[], int nb, int ans[]) {
int len = max(na, nb), ln;
for (ln = 0; L(ln) < len; ++ln);
len = L(++ln);
for (int i = 0; i < len ; ++i) {
if (i >= na) ax[i] = 0, ay[i] = 0;
else ax[i] = a[i], ay[i] = 0;
}
fft(ax, ay, len, 0);
for (int i = 0; i < len; ++i) {
if (i >= nb) bx[i] = 0, by[i] = 0;
else bx[i] = b[i], by[i] = 0;
}
fft(bx, by, len, 0);
for (int i = 0; i < len; ++i) {
double cx = ax[i] * bx[i] - ay[i] * by[i];
double cy = ax[i] * by[i] + ay[i] * bx[i];
ax[i] = cx, ay[i] = cy;
}
fft(ax, ay, len, 1);
for (int i = 0; i < len; ++i)
ans[i] = (int)(ax[i] + 0.5);
return len;
}
string mul(string sa, string sb) {
int l1, l2, l;
int i;
string ans;
memset(sum, 0, sizeof(sum));
l1 = sa.size();
l2 = sb.size();
for (i = 0; i < l1; i++)
x1[i] = sa[l1 - i - 1] - '0';
for (i = 0; i < l2; i++)
x2[i] = sb[l2 - i - 1] - '0';
l = sol(x1, l1, x2, l2, sum);
for (i = 0; i < l || sum[i] >= 10; i++) {//进位
sum[i + 1] += sum[i] / 10;
sum[i] %= 10;
}
l = i;
while (sum[l] <= 0 && l > 0) l--; // 检索最高位
for (i = l; i >= 0; i--)
ans += sum[i] + '0'; // 倒序输出
return ans;
}
FFT 高精度乘法模板的更多相关文章
- H. GSS and Simple Math Problem 高精度乘法模板
链接:https://www.nowcoder.com/acm/contest/104/G来源:牛客网 题目描述 Given n positive integers , your task is to ...
- 高精度乘法模板(luogu1303)
洛谷1303 //luogu1303,不压位的高精度乘法 #include <cstdio> #include <iostream> using namespace std; ...
- FFT多项式乘法模板
有时间来补算法原理orz #include <iostream> #include <cstdio> #include <cmath> #include <c ...
- FFT实现高精度乘法
你应该知道$FFT$是用来处理多项式乘法的吧. 那么高精度乘法和多项式乘法有什么关系呢? 观察这样一个$20$位高精度整数$11111111111111111111$ 我们可以把它处理成这样的形式:$ ...
- 高精度乘法(FFT)
学会了FFT之后感觉自己征服了世界! 当然是幻觉... 不过FFT还是很有用的,在优化大规模的动规问题的时候有极大效果. 一般比较凶残的计数动规题都需要FFT(n<=1e9). 下面是高精度乘法 ...
- SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...
- P1919 FFT加速高精度乘法
P1919 FFT加速高精度乘法 传送门:https://www.luogu.org/problemnew/show/P1919 题意: 给出两个n位10进制整数x和y,你需要计算x*y. 题解: 对 ...
- 【高精度】模板 (C++)
//n为长度 1.高精加 复杂度:O(n) #include<iostream> #include<cstring> #include<algorithm> usi ...
- [vijos P1040] 高精度乘法
如果这次noip没考好,完全是因为从7月29日之后就没有再写过程序了.说起来,真是一个泪流满面的事实… 那这样一个弱智题练手恢复代码能力,竟然还花了我两个晚上(当然不是两整个晚上…) 第一天TLE了, ...
- 【PKU1001】Exponentiation(高精度乘法)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 145642 Accepted: 35529 ...
随机推荐
- kylin-3.1.1-bin-hadoop3搭建,构建cube报的错误,Cannot modify dfs.replication at runtime. It is not in list of params that are allowed to be modified at runtime
主要是每次构建cube时会去读取kylin安装目录下的conf/kylin_hive_conf.xml文件, 副本是无法在hive查询时修改的,注释掉这两项 这个其实还有一些参数的控制: 添加这俩个参 ...
- scab2
package com.cmb.cox.utils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;im ...
- Pycharm创建的虚拟环境,使用命令行指定库的版本进行安装
Pycharm创建的项目,使用了虚拟环境,对库的版本进行管理:有些项目的对第三方库的版本 要求不同,可使用虚拟环境进行管理 直接想通过pip命令安装,直接看第3点 操作步骤: 1.找到当前项目的虚拟环 ...
- linux下后台运行程序
文章目录 背景 nohup命令 setsid命令 pm2 背景 后台运行程序的时候,如果退出当前的终端(session),你运行的所有程序(包括后台程序),都将被关闭. 原因是:你运行的程序都是你的终 ...
- Vue3 中的 v-bind 指令:你不知道的那些工作原理
前言 v-bind指令想必大家都不陌生,并且都知道他支持各种写法,比如<div v-bind:title="title">.<div :title="t ...
- Redis查询大key
原文 安装 wget "https://pypi.python.org/packages/68/44/5efe9e98ad83ef5b742ce62a15bea609ed5a0d1caf35 ...
- LaravelLumen 分组求和问题 where groupBy sum
在Laravel中使用分组求和,如果直接使用Laravel各数据库操作方法,应该会得出来如下代码式: DB::table('table_a') ->where('a','=',1) ->g ...
- Oracle 存储过程 捕获异常
1.带参数插入并带返回值,异常信息 CREATE OR REPLACE PROCEDURE test_pro (v_id in int,v_name in varchar2,app_code out ...
- 分布式事务的概念和解决方案Seate
引入分布式事务: 在电商系统中,扣减库存与保存订单是在两个服务中存在的,如果扣减库存后订单保存失败了是不会回滚的,这样就会造成数据不一致的情况,这其实就是我们所说的分布式事务的问题,接下来我们来学习分 ...
- Curve 进入 CNCF Sandbox,完善统一云原生开源存储拼图
2022 年 6 月 15 日,云原生计算基金会 (CNCF) 宣布,分布式存储系统 Curve 被正式接纳为 CNCF 沙箱(Sandbox)项目.Curve 由网易数帆开源,提供块存储和文件存储能 ...