-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpotentiation
55 lines (43 loc) · 874 Bytes
/
Expotentiation
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
//Expotentiation
//https://cses.fi/problemset/task/1095/
//Input:
//3
//3 4
//2 8
//123 123
//
//Output:
//81
//256
//921450052
//Solve: used binary expotentiaion (https://cp-algorithms.com/algebra/binary-exp.html)so that we can get the power value as early as possible and moded in every step so that it does not overflow
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll m=1e9+7;
//Binary expotentiation
ll pow(ll a , ll b ){
if(b==0){
return 1;
}
ll y= pow(a,b/2);
y=y*y%m;
if(b%2==1){
y=y*a%m;
}
return y;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
//loop for the number of times, it will take input
for(int i=0; i<n; i++){
ll x ,y;
cin>>x>>y;
cout<<pow(x,y)<<"\n";
}
return 0;
}