forked from somiljain7/data-structure
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodinv.cpp
More file actions
Latest commit
29 lines (27 loc) · 563 Bytes
/
Copy pathmodinv.cpp
File metadata and controls
29 lines (27 loc) · 563 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
#include<iostream>
voidextendedEuclid(int A, int B) {
if(B == 0) {
d = A;
x = 1;
y = 0;
}
else {
extendedEuclid(B, A%B);
int temp = x;
x = y;
y = temp - (A/B)*y;
}
}
int d,x,y;
intmodInverse(int A, int M)
{
extendedEuclid(A,M); // Calculating modulo inverse using extended Euclidean algorithm
return (x%M + M)%M; //x may be negative
}
intmain()
{
int a,m; //we want to calculate modulo inverse of 'a' w.r.t. 'm'
cin >> a >> m;
cout << modInverse(a,m);
return0;
}