Your task is to calculate a^b mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.
Example1:
a = 2
b = [3]
Result: 8
Example2:
a = 2
b = [1,0]
Result: 1024
利用mod的性质,a^123 % k = (a ^ 120) % k × (a ^ 3) % k = (((a ^ 12) % k) ^ 10 % k) × (a ^ 3) % k。这题可以递归着做。不看答案真没想法。
int base =1337;publicintsuperPow(int a,int[] b) {if (b ==null||b.length==0) {return1; }returnsuperPowHelper(a, b,b.length-1);// 注意下标}privateintsuperPowHelper(int a,int[] b,int i) {if (i <0) {// 注意下标return1; }returnmodPow(superPowHelper(a, b, i -1),10)% base *modPow(a,Integer.valueOf(b[i]))% base;}privateintmodPow(int a,int k) { a = a % base;int result =1;while (k >0) { result = (result * a) % base; k--; }return result;}