-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnum_ring.cpp
More file actions
75 lines (63 loc) · 1.9 KB
/
num_ring.cpp
File metadata and controls
75 lines (63 loc) · 1.9 KB
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
template<int64_t MOD, typename T = int, typename TCAST = int64_t>
struct FieldElement {
T value;
void norm() {
value %= MOD;
if (value < 0) value += MOD;
}
static T fastPow(TCAST val, int64_t pw) {
TCAST res = 1;
for (; pw; pw >>= 1) {
if (pw&1)
res = (res * val) % MOD;
val = (val * val) % MOD;
}
return res;
}
FieldElement() : value(0) { }
FieldElement(const TCAST& val) : value(val) { norm(); }
FieldElement(const FieldElement& other) : value(other.value) { }
FieldElement& operator=(const FieldElement& other) {
value = other.value;
return *this;
}
FieldElement& operator+=(const FieldElement& other) {
value += other.value;
if (value >= MOD) value -= MOD;
return *this;
}
FieldElement operator+(const FieldElement& other) const {
FieldElement res(value);
return res += other;
}
FieldElement& operator-=(const FieldElement& other) {
value = value - other.value;
if (value < 0) value += MOD;
return *this;
}
FieldElement operator-(const FieldElement& other) const {
FieldElement res(value);
res -= other;
return res;
}
FieldElement& operator*=(const FieldElement& other) {
value = TCAST(value) * other.value % MOD;
return *this;
}
FieldElement operator*(const FieldElement& other) const {
FieldElement res(*this);
res *= other;
return res;
}
FieldElement& operator/=(const FieldElement& other) {
assert(other.value > 0);
value = (TCAST(value) * fastPow(other.value, MOD - 2)) % MOD;
return *this;
}
FieldElement operator/(const FieldElement& other) {
FieldElement res(value);
res /= other;
return res;
}
};
typedef FieldElement<int(1e9)+7> m64;