-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmint.cpp
100 lines (79 loc) · 2.06 KB
/
mint.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
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
template <long long MOD> class MINT {
private:
long long value;
public:
MINT() : value(0) {}
MINT(long long v) {
value = v % MOD;
if (value < 0)
value += MOD;
}
MINT pow(long long exponent) const {
MINT result = 1, base = *this;
while (exponent > 0) {
if (exponent & 1)
result *= base;
base *= base;
exponent >>= 1;
}
return result;
}
MINT inv() const { return pow(MOD - 2); }
operator long long() const { return value; }
MINT &operator+=(const MINT &other) {
value += other.value;
if (value >= MOD)
value -= MOD;
return *this;
}
MINT &operator-=(const MINT &other) {
value -= other.value;
if (value < 0)
value += MOD;
return *this;
}
MINT &operator*=(const MINT &other) {
value = (value * other.value) % MOD;
return *this;
}
MINT &operator/=(const MINT &other) {
*this *= other.inv();
return *this;
}
friend MINT operator+(MINT a, const long long &b) {
return MINT((a.value + (b % MOD) + MOD) % MOD);
}
friend MINT operator-(MINT a, const long long &b) {
return MINT((a.value - (b % MOD) + MOD) % MOD);
}
friend MINT operator*(MINT a, const long long &b) {
return MINT((a.value * (b % MOD)) % MOD);
}
friend MINT operator/(MINT a, const long long &b) {
return MINT((a.value * MINT(b).inv().value) % MOD);
}
friend MINT operator+(MINT a, const MINT &b) {
return MINT((a.value + b.value) % MOD);
}
friend MINT operator-(MINT a, const MINT &b) {
return MINT((a.value - b.value + MOD) % MOD);
}
friend MINT operator*(MINT a, const MINT &b) {
return MINT((a.value * b.value) % MOD);
}
friend MINT operator/(MINT a, const MINT &b) {
return MINT((a.value * b.inv().value) % MOD);
}
friend std::ostream &operator<<(std::ostream &os, const MINT &mi) {
return os << mi.value;
}
friend std::istream &operator>>(std::istream &is, MINT &mi) {
long long x;
is >> x;
mi = MINT(x);
return is;
}
};
const long long MOD = 1e9 + 7;
using mint = MINT<MOD>;