你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。
示例 1:
输入:a = 2, b = [3]
输出:8
示例 2:
输入:a = 2, b = [1,0]
输出:1024
示例 3:
输入:a = 1, b = [4,3,3,8,5,2]
输出:1
示例 4:
输入:a = 2147483647, b = [2,0,0]
输出:1198
提示:
1 <= a <= 231 - 1
1 <= b.length <= 2000
0 <= b[i] <= 9
b 不含前导 0
class Solution {
int a;
int mod = 1337;
int[] b;
public int superPow(int _a, int[] _b) {
a = _a;
b = _b;
return dfs(b.length-1);
}
public int dfs(int u){
if(u == -1) return 1;
return qmi(dfs(u-1),10)*qmi(a,b[u]) % mod;
}
public int qmi(int a, int k){
int res = 1;
a %= mod;
while(k > 0){
if((k & 1) == 1) res = res * a % mod;
k >>= 1;
a = a * a % mod;
}
return res;
}
}