-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.cpp
More file actions
41 lines (34 loc) · 710 Bytes
/
mod.cpp
File metadata and controls
41 lines (34 loc) · 710 Bytes
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
constexpr int P = 998244353;
int norm(int x) {
return x >= P ? (x - P) : x;
}
void inc(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void dec(int &x, int y) {
x -= y;
if (x < 0) x += P;
}
int power(int a, int b) {
int r = 1;
while (b) {
if (b & 1) r = 1ll * r * a % P;
a = 1ll * a * a % P;
b >>= 1;
}
return r;
}
// Barrett
using u64 = unsigned long long;
using u128 = __uint128_t;
struct FastMod {
u64 b, m;
FastMod() {}
FastMod(u64 b) : b(b), m(u64((u128(1) << 64) / b)) {}
u64 operator()(u64 a) const {
u64 q = (u64) ((u128(m) * a) >> 64);
u64 r = a - q * b;
return r >= b ? r - b : r;
}
} mod;