-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModInt.cpp
87 lines (82 loc) · 1.75 KB
/
ModInt.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
79
80
81
82
83
84
85
86
87
#include<bits/stdc++.h>
using namespace std;
const long long P = 1e9 + 7;
// assume -P <= x < 2 * P
long long norm(long long x) {
if(x < 0) x += P;
if(x >= P) x -= P;
return x;
}
template<typename T>
T binpow(T a, long long b) {
T res = 1;
while(b > 0) {
if(b & 1) res = (res * a);
a = (a * a);
b >>= 1;
}
return res;
}
struct Z {
long long x;
Z(long long x = 0) : x(norm(x % P)) {}
long long val() const {
return x;
}
Z operator-() const {
return Z(norm(P - x));
}
Z inv() const {
assert(x != 0);
return binpow(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = (x * rhs.x) % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = norm(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
friend istream &operator>>(istream &is, Z &a) {
long long v;
is >> v;
a = Z(v);
return is;
}
friend ostream &operator<<(ostream &os, const Z &a) {
return os << a.val();
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}