我已經看到了SRM 573's solution here:這個函數爲什麼有效計算模塊化pow值?
long modPow(long x, long y)
{
//Caculates x raised to the y-power modulo MOD
//in O(log(y)) time by using repeated squaring
long r = 1;
while(y > 0){
if((y&1) != 0) {
r = (r * x) % MOD;
}
x = (x * x)%MOD;
y >>= 1;
}
return r;
}
這個功能,但我對這個功能感到困惑於計算x^y%MOD
模塊化值;
爲什麼x = (x * x)%MOD;
需要在函數中?
這對我沒有意義。