I know that (a*b)%m
is equal to ((a%m)*(b%m))%m
?
But how to calculate a%(m*m)
?
I know that (a*b)%m
is equal to ((a%m)*(b%m))%m
?
But how to calculate a%(m*m)
?
We need to calculate a%(m*m)
without causing integer overflow.
long long int a, m, sqrt_a, ans;
sqrt_a = sqrt(a);
if (sqrt_a < m) {
ans = a; // since "m > sqrt_a" then a%(m*m) will be "a"
}
else {
ans = a % (m*m); // since "m <= sqrt_a" then m*m won't cause overflow
}