Problem:
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.
Example 1:
Input: a = 2, b = [3]
Output: 8
Example 2:
Input: a = 2, b = [1,0]
Output: 1024
思路:
Solution (C++):
const int base = 1337;
int superPow(int a, vector<int>& b) {
if (b.empty()) return 1;
int last = b.back();
b.pop_back();
return mod(superPow(a,b), 10) * mod(a, last) % base;
}
int mod(int a, int k) {
a %= base;
int res = 1;
for (int i = 0; i < k; ++i)
res = (res * a) % base;
return res;
}
性能:
Runtime: 12 ms Memory Usage: 6.9 MB
思路:
Solution (C++):
性能:
Runtime: ms Memory Usage: MB