-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigMod.cpp
More file actions
40 lines (31 loc) · 775 Bytes
/
BigMod.cpp
File metadata and controls
40 lines (31 loc) · 775 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
/// UVa OJ - 374
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
ULL bigMod(ULL B, ULL P, ULL M); /// returns B^P % M
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
ULL B, P, M, R;
while (scanf("%llu%llu%llu", &B, &P, &M) != EOF) {
R = bigMod(B, P, M);
printf("%llu\n", R);
}
return 0;
}
ULL bigMod(ULL B, ULL P, ULL M)
{
if (P == 0) {
return 1 % M;
}
else if (P % 2 == 0) {
/// ULL result = bigMod(B, P / 2, M);
/// return ((result % M) * (result % M)) % M;
ULL result = bigMod(B, P / 2, M) % M;
return (result * result) % M;
}
else {
return ((B % M) * (bigMod(B, P - 1, M) % M)) % M;
}
}