ProgrammingVery hard

Which expression is correct for (a * b) % MOD with MOD = 1000000007 and 0 <= a, b < MOD, to avoid overflow in C?

  1. A. (a * b) % MOD where a and b are int
  2. B. (a % MOD) * (b % MOD) with int variables
  3. C. (a * b) % MOD where a and b are unsigned char
  4. D. (a * b) % MOD where a and b are long long

Correct answer: D. (a * b) % MOD where a and b are long long

Each factor is below about 10^9, so the product is below 10^18, which fits in a 64-bit long long. With int operands the product overflows, which is undefined.

More C Programming questions

Exams that ask this topic