-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_theory.cpp
68 lines (63 loc) · 1 KB
/
number_theory.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
int bpow(int A, int B)
{
int product = 1;
while (B > 0) {
if (B & 1)
product = product * A;
A = A * A;
B = B >> 1;
}
return product;
}
int bpow(int A, int B, int mod)
{
A %= mod;
int product = 1;
while (B > 0) {
if (B & 1)
product = (product * A) % mod;
A = (A * A) % mod;
B = B >> 1;
}
return product;
}
int extendedEuclid(int A, int B, int &X, int &Y)
{
if (B == 0)
{
X = 1;
Y = 0;
return A;
}
int X1, Y1;
int GCD = extendedEuclid(B, A % B, X1, Y1);
X = Y1;
Y = X1 - Y1 * (A / B);
return GCD;
}
int modInverse(int A, int M)
{
int X, Y;
int GCD = extendedEuclid(A, M, X, Y);
if (GCD != 1) {
cout << "A and M are not coprime...Check again" << nl;
return -1;
}
else {
return (X % M + M) % M;
}
}
int modInverseFermat(int A, int M)
{
/* assert M is prime */
return bpow(A, M - 2, M);
}
void getModInverse(int *inv, int M)
{
/* assert M is a prime and int inv[M] */
inv[0] = -1;
inv[1] = 1;
for (int i = 2; i < M; i++) {
inv[i] = M - (M / i) * inv[M % i] % M;
}
}