-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMNT.cpp
78 lines (78 loc) · 1.27 KB
/
MNT.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
ll inv(ll a, ll m) {
return (a%=m)>1?(1-m*inv(m, a))/a+m:1;
}
struct Mnt {
int x;
Mnt() {
x=0;
}
Mnt(ll b) {
if(b<-M||b>=M)
b%=M;
if(b<0)
b+=M;
x=b;
}
friend bool operator==(const Mnt& a, const Mnt& b) {
return a.x==b.x;
}
friend bool operator!=(const Mnt& a, const Mnt& b) {
return a.x^b.x;
}
friend bool operator<(const Mnt& a, const Mnt& b) {
return a.x<b.x;
}
friend void read(Mnt& a) {
ll x;
read(x);
a=Mnt(x);
}
friend string to_string(Mnt a) {
return to_string(a.x);
}
Mnt& operator+=(const Mnt& a) {
if((x+=a.x)>=M)
x-=M;
return *this;
}
Mnt& operator-=(const Mnt& a) {
if((x-=a.x)<0)
x+=M;
return *this;
}
Mnt& operator*=(const Mnt& a) {
x=(ll)x*a.x%M;
return *this;
}
Mnt& operator/=(const Mnt& a) {
return (*this)*=inv(a.x, M);
}
friend Mnt pm(Mnt a, ll p) {
Mnt r=1;
for (; p; p/=2, a*=a)
if(p&1)
r*=a;
return r;
}
Mnt operator-() const {
return Mnt(-x);
}
Mnt& operator++() {
return *this+=1;
}
Mnt& operator--() {
return *this-=1;
}
friend Mnt operator+(Mnt a, const Mnt& b) {
return a+=b;
}
friend Mnt operator-(Mnt a, const Mnt& b) {
return a-=b;
}
friend Mnt operator*(Mnt a, const Mnt& b) {
return a*=b;
}
friend Mnt operator/(Mnt a, const Mnt& b) {
return a/=b;
}
};