-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModular Multiplicative Inverse.cpp
More file actions
58 lines (43 loc) · 1012 Bytes
/
Modular Multiplicative Inverse.cpp
File metadata and controls
58 lines (43 loc) · 1012 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Modular Multiplicative Inverse - Iterative method.
// Time complexity: O(log m).
#include <cstdio>
using namespace std;
void extEuclid(int a, int b, int &x, int &y, int &gcd)
{
x = 0; y = 1; gcd = b;
int m, n, q, r;
for (int u = 1, v = 0; a != 0; gcd = a, a = r) {
q = gcd / a; r = gcd % a;
m = x-u*q; n = y-v*q;
x = u; y = v; u = m; v = n;
}
}
// The result could be negative, if it's required to be positive, then add 'm'.
int modInv(int n, int m)
{
int x, y, gcd;
extEuclid(n, m, x, y, gcd);
if (gcd == 1) return x % m;
return 0;
}
int main()
{
int a = 14;
int b = 11;
int m = 26;
int res = ((a%m) * modInv(b, m)) % m; // (a/b) % m.
if(res < 0) res += m;
printf("res : %d\n", res);
return 0;
}
// Extended Euclidean Algorithm - Recursive method.
/*
int x, y, gcd;
void extEuclid(int a, int b)
{
if (b == 0) { x = 1, y = 0, gcd = a; return; }
extEuclid(b, a % b);
x = x - (a / b) * y;
swap(x, y);
}
*/