Mint<p> k : k = k % p;
作者:
lovyamyppm
,
2021-08-22 11:06:26
,
所有人可见
,
阅读 283
typedef long long LL;
template< typename T > inline LL fp(LL a, T n, int mod) {
if(n < 0) a = fp(a, mod - 2, mod), n = -n;
LL res = 1;
for(; n; n >>= 1, a = a * a % mod) if(n & 1) res = res * a % mod;
return res;
}
template< int mod > class Mint {
public:
int x;
Mint():x(0) {}
Mint(int _x): x(_x) {if(x < 0 || x >= mod) x %= mod; if(x < 0) x += mod;}
Mint(LL _x) {if(_x < 0 || _x >= mod) _x %= mod; if(_x < 0) _x += mod; x = _x;}
Mint operator - () const {return Mint(x == 0 ? x : mod - x);}
Mint operator + (const Mint &rhs) const {return Mint(x + rhs.x >= mod ? x + rhs.x - mod : x + rhs.x);}
Mint operator - (const Mint &rhs) const {return Mint(x - rhs.x < 0 ? x - rhs.x + mod : x - rhs.x);}
Mint operator * (const Mint &rhs) const {return Mint((LL)x * rhs.x % mod);}
Mint operator / (const Mint &rhs) const {return Mint((LL)x * fp(rhs.x, -1, mod) % mod);}
bool operator == (const Mint &rhs) const {return x == rhs.x;}
bool operator != (const Mint &rhs) const {return x != rhs.x;}
Mint &operator += (const Mint &rhs) {x += rhs.x; if(x >= mod) x -= mod; return *this;}
Mint &operator -= (const Mint &rhs) {x -= rhs.x; if(x < 0) x += mod; return *this;}
Mint &operator *= (const Mint &rhs) {x = (LL)x * rhs.x % mod; return *this;}
Mint &operator /= (const Mint &rhs) {x = (LL)x * fp(rhs.x, -1, mod) % mod; return *this;}
friend ostream &operator << (ostream &out, const Mint &rhs) {return out <<to_string(rhs.x);}
Mint inv() const {return Mint(fp(x, -1, mod));}
template< typename T > Mint pow(T k) const {return Mint(fp(x, k, mod));}
};