Kyopro Library
 
読み取り中…
検索中…
一致する文字列を見つけられません
modint.hpp
[詳解]
1#pragma once
2#include"../../kyopro_library/template.hpp"
3
4/// @brief ModInt
5template<ll MOD>
6struct ModInt {
7 ModInt(ll x=0){ value=(x>=0?x%MOD:MOD-(-x)%MOD); }
8
9 ModInt operator-() const { return ModInt(-value); }
10 ModInt operator+() const { return ModInt(*this); }
11 ModInt& operator+=(const ModInt& other) {
12 value+=other.value;
13 if(value>=MOD) value-=MOD;
14 return*this;
15 }
16 ModInt& operator-=(const ModInt& other) {
17 value+=MOD-other.value;
18 if(value>=MOD) value-=MOD;
19 return*this;
20 }
21 ModInt& operator*=(const ModInt other) {
22 value=value*other.value%MOD;
23 return*this;
24 }
26 (*this)*=other.inv();
27 return*this;
28 }
29 ModInt operator+(const ModInt& other) const { return ModInt(*this)+=other; }
30 ModInt operator-(const ModInt& other) const { return ModInt(*this)-=other; }
31 ModInt operator*(const ModInt& other) const { return ModInt(*this)*=other; }
32 ModInt operator/(const ModInt& other) const { return ModInt(*this)/=other; }
33 bool operator==(const ModInt& other) const { return value==other.value; }
34 bool operator!=(const ModInt& other) const { return value!=other.value; }
35 friend ostream& operator<<(ostream& os, const ModInt& x) { return os<<x.value; }
36 friend istream& operator>>(istream& is, ModInt& x) {
37 ll v;
38 is>>v;
39 x=ModInt<MOD>(v);
40 return is;
41 }
42
43 ModInt pow(ll x) const {
44 ModInt ret(1),mul(value);
45 while(x) {
46 if(x&1) ret*=mul;
47 mul*=mul;
48 x>>=1;
49 }
50 return ret;
51 }
52 ModInt inv() const { return pow(MOD-2); }
53 ll val() {return value; }
54 static constexpr ll get_mod() { return MOD; }
55
56private:
57 ll value;
58};
59
60using Mod998=ModInt<998244353>;
61using Mod107=ModInt<1000000007>;
62
63#define VM vector<mint>
64#define VVM vector<vector<mint>>
ModInt
Definition modint.hpp:6
friend ostream & operator<<(ostream &os, const ModInt &x)
Definition modint.hpp:35
ModInt(ll x=0)
Definition modint.hpp:7
ModInt & operator*=(const ModInt other)
Definition modint.hpp:21
ModInt operator-() const
Definition modint.hpp:9
ModInt & operator+=(const ModInt &other)
Definition modint.hpp:11
ModInt operator*(const ModInt &other) const
Definition modint.hpp:31
ModInt & operator-=(const ModInt &other)
Definition modint.hpp:16
ModInt & operator/=(ModInt other)
Definition modint.hpp:25
bool operator!=(const ModInt &other) const
Definition modint.hpp:34
ModInt operator+() const
Definition modint.hpp:10
friend istream & operator>>(istream &is, ModInt &x)
Definition modint.hpp:36
static constexpr ll get_mod()
Definition modint.hpp:54
ModInt inv() const
Definition modint.hpp:52
ll val()
Definition modint.hpp:53
ModInt operator/(const ModInt &other) const
Definition modint.hpp:32
ModInt pow(ll x) const
Definition modint.hpp:43
bool operator==(const ModInt &other) const
Definition modint.hpp:33
ModInt operator+(const ModInt &other) const
Definition modint.hpp:29
ModInt operator-(const ModInt &other) const
Definition modint.hpp:30