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?
- A. (uint32_t)(a * b) % m
- B. (a * b % m) with int a and b
- C. ((uint64_t)a * b) % m
- 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
- Which expression tests whether bit k of an unsigned x is set?
- int x = 0x1F; What does printf("%d", 07 + 0x1F) print?
- uint32t x = 0xFFFFFFFFu; How many times does while (x > 0) x >>= 1; execute its body, counting from the start?
- int cnt[3] = {0}; int p; for (p = cnt; p < cnt + 3; p++) p = p - cnt; What is the sum of cnt[0] + cnt[1] + cnt[2]?
- A loop while (!(x & 1)) { x >>= 1; c++; } counts the trailing zero bits of a nonzero uint32t x = 0b101000. What is c?
- uint64t mask = (1ULL << 40) - 1; What is mask?