forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modular_exponentiation.cpp
70 lines (60 loc) · 1.34 KB
/
modular_exponentiation.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
69
/*
Normal power algorithm would take O(power) time to calculate the answer.
But here we use the fact that every number can be represented in power of 2!
Consider 4^7
Normal calculation = (4*4*... 7 Times)
Modular exponentiation = (4^4)*(4^2)*(4^1)
We use this power of 2s to our advantage.
(4^2)=(4^1)^2
(4^4)=(4^2)^2
So, this reduces the number of multiplication, as we only need to sqaure the number (squaring is just single multiplication operation).
*/
#include<bits/stdc++.h>
using namespace std;
long long modular_exponentiation(long long base,long long power,long long mod)
{
base=base%mod;
long long answer=1;
while(power>0)
{
if(power%2==1)
{
answer=(answer*base)%mod;
}
base=(base*base)%mod;
power=power/2;
}
return answer;
}
int main()
{
long long base,power,mod;
cout<<"Enter base: ";
cin>>base;
cout<<"Enter power: ";
cin>>power;
cout<<"Enter mod: ";
cin>>mod;
cout<<"("<<base<<"^"<<power<<")%"<<mod<<" = "<<modular_exponentiation(base,power,mod)<<"\n";
}
/*
Sample I/0
1.
INPUT
Enter base: 2
Enter power: 10
Enter mod: 8000
OUTPUT
(2^10)%8000 = 1024
2.
INPUT
Enter base: 3
Enter power: 6
Enter mod: 1
OUTPUT
(3^6)%1 = 0
*/
/*
Time Complexity: O(log(power))
Space Complexity: O(1)
*/