先找到递归结构,然后转化为整数次幂,再用快速幂优化。
const int mod = 1337;
typedef long long LL;
class Solution {
public:
int superPow(int a, vector<int>& b) {
a %= mod;
if (b.size() == 1) {
return myPow(a, b.back());
}
int tmp = b.back();
b.pop_back();
return (myPow(a, tmp) * myPow(superPow(a, b), 10)) % mod;
}
int myPow(LL x, LL n){
int res = 1;
while (n) {
if (n & 1) res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res % mod;
}
};