Modulus Function to Avoid Integer Overflow in C++
问题 If I have 2 int or long long variables, call them a and b , and I want to compute the sum (a + b) mod p , where p is a large prime integer, how can I utilize the modulo operator in C++ to achieve the desired result? I have tried (a + b) % p , but this gives overflow sometimes, since a + b will overflow before the mod is applied. Other similar approaches I have tried seem to avoid overflow, but give an incorrect result. How can I use the modulo operator in this case to correctly compute the