-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpMod.cpp
More file actions
42 lines (39 loc) · 792 Bytes
/
ExpMod.cpp
File metadata and controls
42 lines (39 loc) · 792 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
/*
Problem: EXPMOD
Description
Given two positive integers a and b. Compute a^b mod (10^9 + 7)
Input
One line contains two integers a and b (1 <= a,b <= 18446744073709551614)
Output
The value a^b mod (10^9+7)
Example
Input
2 3
Output
8
*/
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const ull P = 1e9+7;
ull Exp(ull x, ull N) {
if (N == 1) {
return x;
} else {
x = x % P; // x^N mod P = (x mod P) * (x mod P) * .. *(x mod P)
ull t = Exp(x, N/2) % P;
ull res = (t*t) % P;
if (N % 2 == 1)
{
res = (res*x) % P;
}
return res;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
ull x, N;
cin >> x >> N;
cout << Exp(x, N) << endl;
return 0;
}