Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2 b = [3] Result: 8
Example2:
a = 2 b = [1,0] Result: 1024
class Solution {
public:
const int base =1337;
int superPow(int a, vector<int>& b) {
int res = 1;
int n = b.size();
a = a % base;
for (int i = 0; i < n; ++i) {
res = (helper(res, 10) * helper(a, b[i])) % base;
}
return res;
}
private:
int helper(int a, int k) { //a^k mod 1337 where 0 <= k <= 10
int res = 1;
for (int i = 0; i < k; ++i) {
res = (res * a) % base;
}
return res;
}
};