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

取模类

取模类

集成了常见的取模四则运算,运算速度与手动取模相差无几,效率极高。

using i64 = long long;template<class T> constexpr T mypow(T n, i64 k) {T r = 1;for (; k; k /= 2, n *= n) {if (k % 2) {r *= n;}}return r;
}template<class T> constexpr T power(int n) {return mypow(T(2), n);
}template<const int &MOD> struct Zmod {int x;Zmod(signed x = 0) : x(norm(x % MOD)) {}Zmod(i64 x) : x(norm(x % MOD)) {}constexpr int norm(int x) const noexcept {if (x < 0) [[unlikely]] {x += MOD;}if (x >= MOD) [[unlikely]] {x -= MOD;}return x;}explicit operator int() const {return x;}constexpr int val() const {return x;}constexpr Zmod operator-() const {Zmod val = norm(MOD - x);return val;}constexpr Zmod inv() const {assert(x != 0);return mypow(*this, MOD - 2);}friend constexpr auto &operator>>(istream &in, Zmod &j) {int v;in >> v;j = Zmod(v);return in;}friend constexpr auto &operator<<(ostream &o, const Zmod &j) {return o << j.val();}constexpr Zmod &operator++() {x = norm(x + 1);return *this;}constexpr Zmod &operator--() {x = norm(x - 1);return *this;}constexpr Zmod operator++(signed) {Zmod res = *this;++*this;return res;}constexpr Zmod operator--(signed) {Zmod res = *this;--*this;return res;}constexpr Zmod &operator+=(const Zmod &i) {x = norm(x + i.x);return *this;}constexpr Zmod &operator-=(const Zmod &i) {x = norm(x - i.x);return *this;}constexpr Zmod &operator*=(const Zmod &i) {x = i64(x) * i.x % MOD;return *this;}constexpr Zmod &operator/=(const Zmod &i) {return *this *= i.inv();}constexpr Zmod &operator%=(const int &i) {return x %= i, *this;}friend constexpr Zmod operator+(const Zmod i, const Zmod j) {return Zmod(i) += j;}friend constexpr Zmod operator-(const Zmod i, const Zmod j) {return Zmod(i) -= j;}friend constexpr Zmod operator*(const Zmod i, const Zmod j) {return Zmod(i) *= j;}friend constexpr Zmod operator/(const Zmod i, const Zmod j) {return Zmod(i) /= j;}friend constexpr Zmod operator%(const Zmod i, const int j) {return Zmod(i) %= j;}friend constexpr bool operator==(const Zmod i, const Zmod j) {return i.val() == j.val();}friend constexpr bool operator!=(const Zmod i, const Zmod j) {return i.val() != j.val();}friend constexpr bool operator<(const Zmod i, const Zmod j) {return i.val() < j.val();}friend constexpr bool operator>(const Zmod i, const Zmod j) {return i.val() > j.val();}
};int MOD[] = {998244353, 1000000007};
using Z = Zmod<MOD[1]>;
http://www.hskmm.com/?act=detail&tid=38052

相关文章:

  • 2025年环评公司权威推荐排行榜,环评手续,环评报告,环评验收,专业高效服务助力企业合规发展
  • 2025年棒球帽厂家推荐排行榜,运动棒球帽,时尚棒球帽,定制棒球帽,防晒棒球帽公司精选榜单
  • 于状压的线性 RMQ 算法
  • Flink编程模型 - 详解
  • 服务器关机用halt、poweroff还是shutdown -h now?一文帮你说明
  • KD Tree
  • ST 表
  • 小波矩阵树:高效静态区间第 K 大查询
  • Seata用法
  • 分数运算类
  • 撸一个功能强大的基于语义的图像检索系统
  • 提交一张 PPT,参与 RTE2025 全球语音智能体云展示
  • 解释 EIP-4337
  • 数论常见结论及例题
  • 求解连续数字的正约数集合——倍数法
  • git回滚代码
  • 组合数
  • q
  • 裴蜀定理
  • 逆元
  • 扩展欧几里得 exgcd
  • 离散对数 bsgs 与 exbsgs
  • 常见数列
  • 20232314 2025-2026-1 《网络与系统攻防技术》实验三实验报告
  • 【LTDC】LTDC 简介
  • 分类器案例 - -一叶知秋
  • Markdown数学公式 - -一叶知秋
  • 最大流
  • 最小割树 Gomory-Hu Tree
  • 最小割