-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodInverse (2).cpp
64 lines (45 loc) · 1.32 KB
/
modInverse (2).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
//modular multiplicative inverse
#include<bits/stdc++.h>
using namespace std;
int modularExponentiation(int x,int n,int m){
if(n==0)
return 1;
else if( n%2 == 0)
return modularExponentiation((x*x)%m,n/2,m);
else
return (x*modularExponentiation((x*x)%m,(n-1)/2,m))%m;
}
int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
int modMultInv(int a,int m){
// a*b=1 ... so range of b is [1,m-1]
// inverse exists if and only if a m are coprime
// => gcd(a,m) == 1
a = a%m;
for(int b=1;b<m;b++){
if((a*b)%m==1)
return b;
}
}
int main(){
cout<<modMultInv(7,10)<<endl;
cout<<modMultInv(5,11)<<endl;
cout<<modInverse(5,11)<<endl; //
}
/* Approach 3 (used only when M is prime)
This approach uses Fermat's Little Theorem.
The theorem specifies the following: A^M−1≡1(modM)
By multiplying with A^−1
both sides,the equation can be rewritten as follows:
A^-1≡A^M−2(modM)
The formula for A^−1
is in the form of exponents. Therefore, modular exponentiation can be used to determine the result.
For example, if A=5 and M=11 then AM−2(modM)=59(mod11)=9
is the inverse of 5 under modulo 11.
int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
*/