首页 > 其他分享 >[Some Tricks] 自动取模类

[Some Tricks] 自动取模类

时间:2024-02-18 15:11:06浏览次数:24  
标签:return Modular res Tricks Some 取模类 lhs rhs norm

const i128 o = 1;
template < i64 mod, i64 invpow = mod - 2 >
struct Modular {
  u64 M = (o << 64) / mod;
  i64 query (i64 x) {
    u64 x_ = 1ull * x;
    u64 q = 1ull * (((i128) (M) * (i128) (x_)) >> 64);
    u64 r = x_ - q * (1ull * mod);
    return r >= mod ? 1ll * r - mod : 1ll * r;
  }
  i64 x;
  i64 norm (i64 v) {v = (v < 0) ? (v + mod) : v, v = (v >= mod) ? (v - mod) : v, v = (v >= mod) ? query (v) : v; return v;}
  i64 pow_mod (i64 a, i64 b) {i64 res = 1; for (i64 i = b; i > 0; i >>= 1, a = norm (a * a)) res = (i & 1) ? norm (res * a) : res; return res;}
  void init (i64 rhs) {x = norm (rhs); return ;}
  i64 val () {return norm (x);}
  void neg () {x = norm (mod - x); return ;}
  Modular inv () {assert (x != 0); auto res = *this; res.x = pow_mod (res.x, invpow); return res;}
  void add () {x = norm (x + 1); return ;}
  void sub () {x = norm (x - 1); return ;}
  Modular &operator += (Modular rhs) & {x = norm (x + rhs.x); return *this;}
  Modular &operator -= (Modular rhs) & {x = norm (x - rhs.x); return *this;}
  Modular &operator *= (Modular rhs) & {x = norm (x * rhs.x); return *this;}
  Modular &operator /= (Modular rhs) & {*this *= rhs.inv (); return *this;}
  Modular &operator += (i64 rhs) & {rhs = norm (rhs), x = norm (x + rhs); return *this;}
  Modular &operator -= (i64 rhs) & {rhs = norm (rhs), x = norm (x - rhs); return *this;}
  Modular &operator *= (i64 rhs) & {rhs = norm (rhs), x = norm (x * rhs); return *this;}
  Modular &operator /= (i64 rhs) & {rhs = norm (rhs), *this *= pow_mod (rhs, invpow); return *this;}  
  Modular &operator ^= (i64 ind) & {x = pow_mod (x, ind), x = (ind < 0) ? pow_mod (x, invpow) : x; return *this;}
  friend Modular operator + (Modular lhs, Modular rhs) {auto res = lhs; res += rhs; return res;}
  friend Modular operator - (Modular lhs, Modular rhs) {auto res = lhs; res -= rhs; return res;}
  friend Modular operator * (Modular lhs, Modular rhs) {auto res = lhs; res *= rhs; return res;}
  friend Modular operator / (Modular lhs, Modular rhs) {auto res = lhs; res /= rhs; return res;}
  friend Modular operator + (Modular lhs, i64 rhs) {auto res = lhs; rhs = norm (rhs); res.x = norm (res.x + rhs); return res;}
  friend Modular operator - (Modular lhs, i64 rhs) {auto res = lhs; rhs = norm (rhs); res.x = norm (res.x - rhs); return res;}
  friend Modular operator * (Modular lhs, i64 rhs) {auto res = lhs; rhs = norm (rhs); res.x = norm (res.x * rhs); return res;}
  friend Modular operator / (Modular lhs, i64 rhs) {auto res = lhs; rhs = norm (rhs); res.x = norm (res.x * pow_mod (rhs, invpow, mod)); return res;}  
  friend Modular operator ^ (Modular lhs, i64 ind) {auto res = lhs; res ^= ind; return res;}
  friend bool operator == (Modular lhs, Modular rhs) {return (lhs.val () == rhs.val ());}
  friend bool operator != (Modular lhs, Modular rhs) {return (lhs.val () != rhs.val ());}
  friend bool operator > (Modular lhs, Modular rhs) {return (lhs.val () > rhs.val ());}
  friend bool operator < (Modular lhs, Modular rhs) {return (lhs.val () < rhs.val ());}
  friend bool operator >= (Modular lhs, Modular rhs) {return (lhs.val () >= rhs.val ());}
  friend bool operator <= (Modular lhs, Modular rhs) {return (lhs.val () <= rhs.val ());} 
};

标签:return,Modular,res,Tricks,Some,取模类,lhs,rhs,norm
From: https://www.cnblogs.com/RB16B/p/18019349

相关文章

  • 二十、基于Bootstrap和FontAwesome制作页面
    调整项目目录结构:在项目statics目录下,创建JS、CSS、Plugins文件,分别用于存放js、css和第三方插件。Bootstrap:提供现成的样式+效果1、下载Bootstrap在boostrap官网下载用于生产版本的bootstrap,放在plugins文件夹。2、Html中引入Bootstrap的css<head>...<linkrel="style......
  • Eigen中变换矩阵Eigen::Isometry3d T的使用方法(左乘和右乘)
    https://zhuanlan.zhihu.com/p/610439768?utm_id=0 一、基本定义Eigen::Isometry3dT_imu_to_lidar=Eigen::Isometry3d::Identity()转换矩阵本质是一个4*4的矩阵二、操作方法.translation():无参数,返回当前变换平移部分的向量表示(可修改),可以索引[]获取各分量.rotation(......
  • tricks I
    1.P2824排序碰见这种只有最后有一个查询的问题我们可以考虑二分最后的答案。具体地,对于当前\(mid\),把所有小于\(mid\)的设为\(0\),其他的设为\(1\)。此时我们只需要维护最后的位置是否大于\(mid\)就好。那么每次升降序的排序就很好办了。我们用线段树维护一个区间和,也......
  • git pull : 提示 can't lock ref 'xxxx' ,some local refs could not be update
    在执行gitpull操作时可能会遇到can'tlockref'xxxx',somelocalrefscouldnotbeupdate;tryrunninggitremotepruneorigintoremoveanyold,conflictingbranches。出现以上的问题的原因是本地所追踪的远程分支,与本地对应分支不同,导致的gitpull失败。如有一......
  • ChatGPT无法登录报错something went wrong. please make sure your device's date and
    这两天Android在登陆ChatGPT的时候,出现错误:somethingwentwrong.pleasemakesureyourdevice'sdateandtimearesetproperly如下图:这个问题就出现的非常蹊跷,于是我在网上搜索了一圈,很多的教程都指向节点网络问题,但是我的Hostease网络确定没有问题,因此这个问题就快无解了,正......
  • Essay - OI tricks
    ......
  • 关于error: failed to push some refs to如何解决
    Smiling&Weeping----在你的门前,我堆起一个雪人,代表笨拙的我,把你久等 常见的错误报错内容基本都是error:failedtopushsomerefsto‘远程仓库地址’。导致产生原因我们想把自己本地的某个项目关联到远程仓库并推送上去操作本地项......
  • fontawesome 测试
    贫穷的人只能用Free版v我$99买Pro版虽然该博客已经白嫖到了Pro了。https://fontawesome.com/search动画测试样式测试......
  • break 或 continue 循环函数,使用some同forEarch一样,但是直到找到就不继续往下循环,节省
    停止循环是循环中一个常见的需求。使用for循环我们可以用break提前结束循环。consta=[0,1,2,3,4];for(vari=0;i<a.length;i++){if(a[i]===2){break;//stoptheloop}console.log(a[i]);}//>0,1另一个常见的需求使我们需要直接取......
  • fortawesome在vue3中的使用
    fortawesome的使用,2023-12-25https://fontawesome.com/search?o=r&m=free&c=coding&s=solid6.5.1,3.0.5,6.5.1yarnadd@fortawesome/fontawesome-svg-coreyarnadd@fortawesome/vue-fontawesomeyarnadd@fortawesome/free-solid-svg-icons/*SetupusingVu......