考察点:递归,幂运算;
思路: superPow看成一个模块,它输入一个基数和数组,求出结果;但是它是递归地去求结果,每次去掉最后一位,然后利用powmod这个功能模块函数,返回最后结果。这个递归的思想想到的话可以极大地减少时间复杂度。
C++代码:
class Solution {
const int base = 1337;
int powmod(int a, int k) //a^k mod 1337 where 0 <= k <= 10
{
a %= base;
int result = 1;
for (int i = 0; i < k; ++i)
result = (result * a) % base;
return result;
}
public:
int superPow(int a, vector<int>& b) {
a %= base;
if (b.empty())
return 1;
int last_num = b.back();
b.pop_back();
return powmod(superPow(a, b), 10) * powmod(a, last_num) % base;
}
};