更新时间:2023-11-30 19:13:40
在 GeeksForGeeks 是以下功能:
// Returns (a * b) % mod
long long moduloMultiplication(long long a,
long long b,
long long mod)
{
long long res = 0; // Initialize result
// Update a if it is more than
// or equal to mod
a %= mod;
while (b)
{
// If b is odd, add a with result
if (b & 1)
res = (res + a) % mod;
// Here we assume that doing 2*a
// doesn't cause overflow
a = (2 * a) % mod;
b >>= 1; // b = b / 2
}
return res;
}
使用它代替
x = (x*x) % p;
即,
x = moduloMultiplication(x, x, p);
,而不是
res = (res*x) % p
即,
res = moduloMultiplication(res, x, p);