分数运算类

news/2025/10/24 13:10:57/文章来源:https://www.cnblogs.com/xihegudi/p/19162998

分数运算类

定义了分数的四则运算,如果需要处理浮点数,那么需要将函数中的 gcd 运算替换为 fgcd

template<class T> struct Frac {T x, y;Frac() : Frac(0, 1) {}Frac(T x_) : Frac(x_, 1) {}Frac(T x_, T y_) : x(x_), y(y_) {if (y < 0) {y = -y;x = -x;}}constexpr double val() const {return 1. * x / y;}constexpr Frac norm() const { // 调整符号、转化为最简形式T p = gcd(x, y);return {x / p, y / p};}friend constexpr auto &operator<<(ostream &o, const Frac &j) {T p = gcd(j.x, j.y);if (j.y == p) {return o << j.x / p;} else {return o << j.x / p << "/" << j.y / p;}}constexpr Frac &operator/=(const Frac &i) {x *= i.y;y *= i.x;if (y < 0) {x = -x;y = -y;}return *this;}constexpr Frac &operator+=(const Frac &i) { return x = x * i.y + y * i.x, y *= i.y, *this; }constexpr Frac &operator-=(const Frac &i) { return x = x * i.y - y * i.x, y *= i.y, *this; }constexpr Frac &operator*=(const Frac &i) { return x *= i.x, y *= i.y, *this; }friend constexpr Frac operator+(const Frac i, const Frac j) { return i += j; }friend constexpr Frac operator-(const Frac i, const Frac j) { return i -= j; }friend constexpr Frac operator*(const Frac i, const Frac j) { return i *= j; }friend constexpr Frac operator/(const Frac i, const Frac j) { return i /= j; }friend constexpr Frac operator-(const Frac i) { return Frac(-i.x, i.y); }friend constexpr bool operator<(const Frac i, const Frac j) { return i.x * j.y < i.y * j.x; }friend constexpr bool operator>(const Frac i, const Frac j) { return i.x * j.y > i.y * j.x; }friend constexpr bool operator==(const Frac i, const Frac j) { return i.x * j.y == i.y * j.x; }friend constexpr bool operator!=(const Frac i, const Frac j) { return i.x * j.y != i.y * j.x; }
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/945199.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

坐标压缩与离散化

坐标压缩与离散化 简单版本 sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(), alls.end()), alls.end()); auto get = [&](int x) {return lower_bound(alls.begin(), alls.end(), x) - alls.beg…

撸一个功能强大的基于语义的图像检索系统

构建了一个支持中英文和图像检索的自定义系统。通过PyQt5开发GUI界面,集成KimiAI实现中文翻译,解决了原生框架中文支持差、界面固定等问题。系统核心功能包括:1)中文文本检索(自动翻译为英文);2)以图搜图(基于…

提交一张 PPT,参与 RTE2025 全球语音智能体云展示

无法亲临 RTE2025 大会?😢 没关系!🎉 我们特别为你的项目提供了一个云展示机会。只需提交一张 PPT,即可参与我们的「全球语音智能体云展示」,与众多领先的语音智能体一同在大会展区屏幕上轮播展示! 🤩项目提…

完整教程:深入解析AppCrawler:开源自动遍历测试工具配置指南

完整教程:深入解析AppCrawler:开源自动遍历测试工具配置指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Con…

解释 EIP-4337

简单来说,EIP-4337 旨在实现“账户抽象”,让智能合约钱包成为用户的默认和主流钱包,从而极大地改善用户体验和安全性。 下面我将从几个方面详细解释 EIP-4337: 1. 核心问题:以太坊的两种账户 在理解 EIP-4337 之前…

数论常见结论及例题

数论常见结论及例题 常见结论 球盒模型(八种) 参考链接。给定 \(n\) 个小球 \(m\) 个盒子。球同,盒不同、不能空隔板法: \(N\) 个小球即一共 \(N-1\) 个空,分成 \(M\) 堆即 \(M-1\) 个隔板,答案为 \(\dbinom{n-1…

材料包含与下载漏洞

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

N8N Workflow Collection - 专业级自动化工作流库 - 详解

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

完整教程:Elasticsearch面试精讲 Day 23:安全认证与权限控制

完整教程:Elasticsearch面试精讲 Day 23:安全认证与权限控制pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Con…

Min25 筛

Min25 筛 求解 \(1-N\) 的质数和,其中 \(N \le 10^{10}\) 。 namespace min25{const int N = 1000000 + 10;int prime[N], id1[N], id2[N], flag[N], ncnt, m;LL g[N], sum[N], a[N], T;LL n;LL mod;inline LL ps(LL …

莫比乌斯函数/反演

莫比乌斯函数/反演 莫比乌斯函数定义:\(\displaystyle {\mu(n) = \begin{cases} 1 &n = 1 \\ (-1)^k &n = \prod_{i = 1}^k p_i \text{ 且 } p_i \text{ 互质 } \\ 0 &else \end{cases}}\) 。莫比乌斯函数…

同余方程组、拓展中国剩余定理 excrt

同余方程组、拓展中国剩余定理 excrt 公式:\(x \equiv b_i(\bmod\ a_i)\) ,即 \((x - b_i) \mid a_i\) 。 int n; LL ai[maxn], bi[maxn]; inline int mypow(int n, int k, int p) {int r = 1;for (; k; k >>=…

完整教程:微软2025教育AI报告:教育群体采用AI的比例显著提升

完整教程:微软2025教育AI报告:教育群体采用AI的比例显著提升pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Con…

康拓展开

康拓展开 正向展开普通解法 将一个字典序排列转换成序号。例如:12345->1,12354->2。 int f[20]; void jie_cheng(int n) { // 打出1-n的阶乘表f[0] = f[1] = 1; // 0的阶乘为1for (int i = 2; i <= n; i++)…

求解连续数字的正约数集合——倍数法

求解连续数字的正约数集合——倍数法 使用规律递推优化,时间复杂度为 \(\mathcal{O}(N\log N)\) ,如果不需要详细的输出集合,则直接将 vector 换为普通数组即可(时间更快) 。 #include <bits/stdc++.h> usi…

git回滚代码

回滚上一次提交是指撤销最近一次的git提交操作。在实际使用中,有两种常见的方法可以实现这个操作: 方法一:使用git revert命令回滚 1. 首先,通过命令`git log`查看提交记录,找到要回滚的提交的hash值。 2. 使用命…

Apache POI 在 Linux 无图形界面环境下因字体配置问题导致Excel导出失败的解决方案 - 详解

Apache POI 在 Linux 无图形界面环境下因字体配置问题导致Excel导出失败的解决方案 - 详解2025-10-24 12:52 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !importan…

扩展欧几里得 exgcd

扩展欧几里得 exgcd 求解形如 \(a\cdot x + b\cdot y = \gcd(a,b)\) 的不定方程的任意一组解。 int exgcd(int a, int b, int &x, int &y) {if (!b) {x = 1, y = 0;return a;}int d = exgcd(b, a % b, y, x);y…

离散对数 bsgs 与 exbsgs

离散对数 bsgs 与 exbsgs 以 \(\mathcal O(\sqrt {P})\) 的复杂度求解 \(a^x \equiv b(\bmod P)\) 。其中标准 \(\tt BSGS\) 算法不能计算 \(a\) 与 \(MOD\) 互质的情况,而 exbsgs 则可以。 namespace BSGS { LL a, …

防爆模乘

防爆模乘 借助浮点数实现 以 \(\mathcal O(1)\) 计算 \(a\cdot b\bmod p\) ,由于不取模,常数比 int128 法小很多。其中 \(1 \le n, k, p \le 10^{18}\) 。 int mul(int a, int b, int m) {int r = a * b - m * (int)…