- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModular Arithmetic.cpp
More file actions
Latest commit
45 lines (33 loc) · 680 Bytes
/
Copy pathModular Arithmetic.cpp
File metadata and controls
45 lines (33 loc) · 680 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
#include<bits/stdc++.h>
usingnamespacestd;
#definelllonglong
ll mulMod (ll a, ll b, ll M) {
a %= M, b %= M;
if(M <= 1000000009) return a * b % M;
ll res = 0;
while(a > 0) {
a %= M, b %= M;
if (a & 1) {
res += b;
if (res >= M) res -= M;
}
a >>= 1; b <<= 1; b %= M;
if (b >= M) b -= M;
}
return res;
}
ll bigMod(ll p, ll q, ll M) {
ll ans = 1;
while(q) {
if(q & 1) ans = mulMod(ans, p, M);
p = mulMod(p, p, M);
q>>=1;
}
return ans % M;
}
ll inverseMod(ll p, ll M) {
returnbigMod(p, M - 2, M);
}
int32_tmain() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
}