Medium
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
1. a^b % 1337 = (a%1337)^b % 1337
2. xy % 1337 = ((x%1337) * (y%1337)) % 1337
class Solution {
public int superPow(int a, int[] b) {
if(b==null || b.length==0) return a;
return superPow(a,b,b.length,1337);
}
public int superPow(int a,int[] b,int len,int k){
if(len==1)
return powMod(a,b[0],k);
return powMod(superPow(a,b,len-1,k),10,k)*powMod(a,b[len-1],k)%k;
}
public int powMod(int a,int b,int k){
a = a % k;//防止a过大后面与pow乘溢出
int pow = 1;
for(int i=0;i<b;i++){
pow = pow * a % k;
}
return pow;
}
}