当前位置: 首页 > news >正文

高精度乘法模板(fft)

 正常高精度复杂度是o(n^2),fft复杂度o(nlogn)

#define int long long//__int128 2^127-1(GCC)
#define PII pair<int,int>
#define f first
#define s second
using namespace std;
const int inf = 0x3f3f3f3f3f3f3f3f, N = 3e5 + 5, mod = 1e9 + 7;
const double PI = acos(-1);
int n, m;
struct Complex
{double x, y;Complex operator+ (const Complex& t) const{return { x + t.x, y + t.y };}Complex operator- (const Complex& t) const{return { x - t.x, y - t.y };}Complex operator* (const Complex& t) const{return { x * t.x - y * t.y, x * t.y + y * t.x };}
}a[N], b[N];int rev[N], bit, tot;
void fft(Complex a[], int inv)
{for (int i = 0; i < tot; i++)if (i < rev[i])swap(a[i], a[rev[i]]);for (int mid = 1; mid < tot; mid <<= 1){auto w1 = Complex({ cos(PI / mid), inv * sin(PI / mid) });for (int i = 0; i < tot; i += mid * 2){auto wk = Complex({ 1, 0 });for (int j = 0; j < mid; j++, wk = wk * w1){auto x = a[i + j], y = wk * a[i + j + mid];a[i + j] = x + y, a[i + j + mid] = x - y;}}}
}
signed main() {ios_base::sync_with_stdio(0);cin.tie(0), cout.tie(0);string aa, bb;cin >> aa >> bb;n = aa.size()-1, m = bb.size()-1;for (int i = 0; i <= n; i++) { a[i].x = aa[i] - '0'; }for (int i = 0; i <= m; i++) { b[i].x = bb[i] - '0'; }while ((1 << bit) < n + m + 1) bit++;tot = 1 << bit;for (int i = 0; i < tot; i++) {rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (bit - 1));}fft(a, 1), fft(b, 1);for (int i = 0; i < tot; i++) a[i] = a[i] * b[i];fft(a, -1);string s;int t=0;for (int i = n+m; i >= 0; i--) {t+=(int)(a[i].x / tot + 0.5);s+=t%10+'0';t/=10;}if(t) s+=t+'0';reverse(s.begin(),s.end());cout<<s;
}

http://www.lryc.cn/news/156226.html

相关文章:

  • C# 现状简单说明
  • el-table滚动加载、懒加载(自定义指令)
  • 不关闭Tamper Protection(篡改保护)下强制卸载Windows Defender和安全中心所有组件
  • 从一到无穷大 #13 How does Lindorm TSDB solve the high cardinality problem?
  • 三维模型OBJ格式轻量化的纹理压缩和质量关系分析
  • 【每日一题】54. 螺旋矩阵
  • git:一些撤销操作
  • leetcode 209. 长度最小的子数组
  • 《rk3399:各显示接口的dts配置》
  • Python数据分析-Pandas
  • golang 多线程管理 -- chatGpt
  • 【Math】导数、梯度、雅可比矩阵、黑塞矩阵
  • 【C语言】——调试技巧
  • 【Python】pytorch,CUDA是否可用,查看显卡显存剩余容量
  • React16入门到入土
  • 【GPT引领前沿】GPT4技术与AI绘图
  • 【LeetCode】19. 删除链表的倒数第 N 个结点
  • spring boot3.x集成swagger出现Type javax.servlet.http.HttpServletRequest not present
  • 《低代码指南》——智能化低代码开发实践案例
  • 268_C++_字节计算(((bits) + 7) / 8)、字节对齐(((number) + 3) / 4 * 4)
  • JavaWeb知识梳理(后端部分)
  • AI:07-基于卷积神经网络的海洋生物的识别
  • centos7下docker设置新的下载镜像源并调整存放docker下载镜像的仓库位置
  • Gitea--私有git服务器搭建详细教程
  • SOLIDWORKS放样是什么意思?
  • Xcode打包ipa文件,查看app包内文件
  • AJAX学习笔记6 JQuery对AJAX进行封装
  • 阿里云服务器退款规则_退款政策全解析
  • ExpressLRS开源之基本调试数据含义
  • DOM 简介 | 深入了解DOM