Super Pow

Desicription

Your task is to calculate a^b mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

Example 1:

1
2
Input: a = 2, b = [3]
Output: 8

Example 2:

1
2
Input: a = 2, b = [1,0]
Output: 1024

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
int superPow(int a, std::vector<int>& b) {
auto pow = [](long long a, long long b) {
long long res = 1;
while(b) {
if(b & 1) {
res = (res * a) % 1337;
}
b >>= 1;
a = (a * a) % 1337;
}
return res;
};

long long res = 1;
for(int i = 0; i < b.size(); i++) {
res = pow(res, 10) * pow(a, b[i]) % 1337;
}
return res;
}
};