ProgrammingVery hardGATE CS · Campus placements

To compute a * b mod m with 0 <= a, b < m <= 1000000007 in a competitive program, which expression is correct?

  1. A. (uint32_t)(a * b) % m
  2. B. (a * b % m) with int a and b
  3. C. ((uint64_t)a * b) % m
  4. D. a * (b % m) % m with int a and b

Correct answer: C. ((uint64_t)a * b) % m

The product of two values below about 10^9 can reach 10^18, which overflows 32-bit types. Casting one operand to uint64_t before multiplying keeps the full product, and the reduction then fits.

More C Programming questions

Exams that ask this topic