Skip to content

Commit 6fddfba

Browse files
Merge pull request matthewsamuel95#274 from ThiagoWhispher/patch-1
Created Fast Exponentiation with Mod
2 parents 7a5fa6b + 5d244f6 commit 6fddfba

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long long ll;
6+
7+
ll mult(ll a, ll b, ll m){
8+
return ((a%m)*(b%m))%m;
9+
}
10+
11+
ll fast_exp_mod(ll b, ll e, ll m){
12+
if(e == 0) return 1;
13+
if(e == 1) return b%m;
14+
if(e % 2 == 0){
15+
ll x = fast_exp_mod(b, e>>1, m);
16+
return mult(x,x,m);
17+
}
18+
return mult(fast_exp_mod(b, e-1, m), b, m);
19+
}
20+
21+
int main(){
22+
ll b, e, m;
23+
scanf("%lld%lld%lld", &b, &e, &m);
24+
printf("%lld\n", fast_exp_mod(b, e, m));
25+
return 0;
26+
}

0 commit comments

Comments
 (0)