forked from swaaz/basicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.cpp
More file actions
42 lines (31 loc) · 713 Bytes
/
Copy pathprogram.cpp
File metadata and controls
42 lines (31 loc) · 713 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
/*
* Code for modular exponentiation
* Time complexity : O(n*log(n))
*/
#include<bits/stdc++.h>
using namespace std;
// This function returns value of (x^y)%mod.
int power(int x, int y, int mod)
{
int answer = 1; // Initialise the answer
x = x % mod;
if(x == 0) return 0; // If x is divisible by mod
while(y > 0)
{
if(y % 2 == 1) // If y is odd multiply x with answer
{
answer = (answer*x) % mod;
}
y /= 2; // reduce the value of y
x = (x*x) % mod; // increment the value of x
}
return answer;
}
// Main function
int main()
{
int x = 2, y = 10, mod = 1000000007;
int result = power(x, y, mod);
cout << result << '\n';
return 0;
}