-
Notifications
You must be signed in to change notification settings - Fork 4
/
A^B-Power-Large.cpp
87 lines (67 loc) · 1.66 KB
/
A^B-Power-Large.cpp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Vishi's birthday is on the door.
Anshuli and his friends are planning to give him a birthday party. For that Anshuli's friends want him to buy the cake. He needs to pay 'x' amount of money to buy the cake on the first day. After each day has passed, the cake becomes 'x' times the price that it was on the previous day. For buying the cake Anshuli has to collect money from all the friends and for that, he will need 'y' days and after 'y' days he will go and buy the cake.
Anshuli seeks your help in calculating the price of cake on the yth day.
Take the price as modulo 10^9 + 7 as the price can be very large.
Input Format
The first line contains an integer T, the number of testcases. It's followed by T lines.
Each testcase will contain two integers X & Y separated by a space.
Output Format
Output T lines, each corresponding to the answer of the testcase.
Constraints
1 <= T <= 10
1 <= X,Y <= 10100000
X % (109 + 7) != 0
Note
Both integers will have a maximum of 100000 digits.
Sample Input 1
3
9 3
3 3
7 4
Sample Output 1:
729
27
2401
SAMPLE INPUT
3
9 3
3 3
7 4
SAMPLE OUTPUT
729
27
2401
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int main()
{
long long int j,t,a;
string b;
cin>>t;
for(j=0; j<t; j++)
{
cin>>a;
cin>>b;
ll remainderB = 0;
ll MOD = 1000000007;
for (int i = 0; i < b.length(); i++)
{
remainderB = (remainderB * 10 + b[i] - '0') % (MOD - 1);
}
cout << power(a, remainderB, MOD) << endl;
}
return 0;
}