Super Pow
Thoughts
Code
class Solution {
public int powMod(int a, int b, int k) {
a %= k; // 防止res * a溢出
int res = 1;
for (int i = 0; i < b; i++) {
res = (res * a) % k; // 防止res * a溢出
}
return res;
}
public int superPow(int a, int[] b, int bLen, int k) {
if (bLen == 0) {
return powMod(a, b[0], k);
}
return powMod(superPow(a, b, bLen - 1, k), 10, k) * powMod(a, b[bLen], k) % k;
}
public int superPow(int a, int[] b) {
return superPow(a, b, b.length - 1, 1337);
}
}Analysis
Last updated