递归实现:
//求a的b次方模mod
public static long quickMul(long a,long b,long mod){
if (b == 0) return 1;
long temp = quickMul(a,b / 2,mod);
return ((b & 1) == 0 ? temp * temp: temp * temp * a) % mod;
}
非递归实现:
//求a的b次方模mod
public static long quickMul(long a,long b,long mod){
long ans = 1;
while (b!=0){
if ((b&1) !=0) {
ans = ans * a % mod;
}
a = a * a % mod;
b >>=1;
}
return ans;
}