-
Notifications
You must be signed in to change notification settings - Fork 9
/
finite_field.cpp
52 lines (39 loc) · 1.77 KB
/
finite_field.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
// finite_field.cpp
// Eric K. Zhang; Nov. 3, 2018
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
template<int p> struct FF {
LL val;
FF(const LL x=0) { val = (x % p + p) % p; }
bool operator==(const FF<p>& other) const { return val == other.val; }
bool operator!=(const FF<p>& other) const { return val != other.val; }
FF operator+() const { return val; }
FF operator-() const { return -val; }
FF& operator+=(const FF<p>& other) { val = (val + other.val) % p; return *this; }
FF& operator-=(const FF<p>& other) { *this += -other; return *this; }
FF& operator*=(const FF<p>& other) { val = (val * other.val) % p; return *this; }
FF& operator/=(const FF<p>& other) { *this *= other.inv(); return *this; }
FF operator+(const FF<p>& other) const { return FF(*this) += other; }
FF operator-(const FF<p>& other) const { return FF(*this) -= other; }
FF operator*(const FF<p>& other) const { return FF(*this) *= other; }
FF operator/(const FF<p>& other) const { return FF(*this) /= other; }
static FF<p> pow(const FF<p>& a, LL b) {
if (!b) return 1;
return pow(a * a, b >> 1) * (b & 1 ? a : 1);
}
FF<p> pow(const LL b) const { return pow(*this, b); }
FF<p> inv() const { return pow(p - 2); }
};
template<int p> FF<p> operator+(const LL lhs, const FF<p>& rhs) { return FF<p>(lhs) += rhs; }
template<int p> FF<p> operator-(const LL lhs, const FF<p>& rhs) { return FF<p>(lhs) -= rhs; }
template<int p> FF<p> operator*(const LL lhs, const FF<p>& rhs) { return FF<p>(lhs) *= rhs; }
template<int p> FF<p> operator/(const LL lhs, const FF<p>& rhs) { return FF<p>(lhs) /= rhs; }
typedef FF<1000000007> num;
int main() {
num a = 23;
num b = 1 / a;
assert(a * b == 1);
cout << a.pow(100).val << endl; // => 398079999
cout << (a - 30).val << endl; // => 1000000000
}