首页 > 其他分享 >模板-mod类

模板-mod类

时间:2023-01-15 17:44:06浏览次数:38  
标签:const int operator ModInt x0 mod 模板 MOD

模板

int MOD; 
// const int MOD = ;
struct ModInt {
    int x;

    ModInt(int x = 0) : x(x % MOD) {}
    ModInt(long long x) : x (x % MOD) {}

    int val() { return x; }
    ModInt operator + (const ModInt &a) const { int x0 = x + a.x; return ModInt(x0 < MOD ? x0 : x0 - MOD); }
    ModInt operator - (const ModInt &a) const { int x0 = x - a.x; return ModInt(x0 < MOD ? x0 + MOD : x0); }
    ModInt operator * (const ModInt &a) const { return ModInt(1LL * x * a.x % MOD); }
    ModInt operator / (const ModInt &a) const { return *this * a.inv(); }
    void operator += (const ModInt &a) { x += a.x; if (x >= MOD) x -= MOD; }
    void operator -= (const ModInt &a) { x -= a.x; if (x < 0) x += MOD; }
    void operator *= (const ModInt &a) { x = 1LL * x * a.x % MOD; }
    void operator /= (const ModInt &a) { *this = *this / a; }
    ModInt qpow(long long n) const { ModInt res(1), mul(x);for (; n; n>>=1, mul *= mul)if (n & 1) res *= mul;return res; }
    ModInt inv() const { return qpow(MOD-2); }
    friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x;}
};

使用

变量做模数时,使用 int MOD,常数做模数时,使用 const int MOD

可以像 int 一样使用。

支持的运算 :

  • +
  • -
  • *
  • /
  • +=
  • -=
  • *=
  • /=
  • qpow(n) 快速幂
  • inv() 逆元

可以使用 std::cout 直接输出

标签:const,int,operator,ModInt,x0,mod,模板,MOD
From: https://www.cnblogs.com/mingzi47/p/17053802.html

相关文章

  • 常用算法模板
    BFS单向BFS不记录层数whilequeue不空:cur=queue.pop()for节点incur的所有相邻节点:if该节点有效且未访问过:queue.push(该节点)......
  • P1886 滑动窗口 /【模板】单调队列
    滑动窗口/【模板】单调队列题目描述有一个长为\(n\)的序列\(a\),以及一个大小为\(k\)的窗口。现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的......
  • 区间dp模板
    该死的csdn登陆不上去了,为了防止区间dp模板丢失,在这里再存一份然后是左右取数字的问题,我记得20年的时候我应该看过这题,是有一个数列,前后取若干个数字,问先手能取最大值那......
  • Earthdata批量下载MODIS遥感影像的方法
      本文介绍在Earthdata网站中,批量下载MODIS遥感影像的方法。  首先,打开网页:https://search.earthdata.nasa.gov/search,如果没有Earthdata账号的话可以注册一下。 ......
  • 网络流模板及易错点总结
    一、最大流#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintNN=300,MM=5e3+8,INF=0x7f7f7f7f;intn,m,s,t;intdis[NN];queue......
  • idea修改注释模板
    类头注释:打开file->setting->Editor->FilrandCodeTemplates->Includes->FileHeader 然后编辑你需要注释的内容,保存后,新建类时就会生效。......
  • [VueJsDev] 基础知识 - CommonJs VS ES Module
    CommonJsVSESModule:::details目录目录​CommonJsVSESModule​​​Part.1:CommonJs​​​​Part.2:ESModule​​​​Part.3:CJS对比ESM表​​​​Code.4......
  • Modbus常用功能码协议
    01H-读线圈状态CoilStatus1)描述:读从机线圈寄存器,位操作,可读单个或者多个;2)发送指令:假设从机地址位0x01,寄存器开始地址0x0023,寄存器结束抵制0x0038,总共读取21个线圈。协......
  • TypeError: 'module' object is not callable
    TypeError:'module'objectisnotcallable(“模块”对象不可调用)因为可能你的函数和模块名起的一样在你导包的时候少导用一层 ......
  • In aggregated query without GROUP BY...this is incompatible with sql_mode=only_f
    数据库查询时,出现如下错误:Causedby:com.mysql.jdbc.exceptions.jdbc4MySQLSyntaxErrorException:InaggregatedquerywithoutGROUPBY,expression#1ofSELECTlist......