ProgrammingVery hard
Which expression is correct for (a * b) % MOD with MOD = 1000000007 and 0 <= a, b < MOD, to avoid overflow in C?
- A. (a * b) % MOD where a and b are int
- B. (a % MOD) * (b % MOD) with int variables
- C. (a * b) % MOD where a and b are unsigned char
- 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
- How many times does the loop x &= x - 1 run for x = 0b101100 before x becomes 0?
- For unsigned x = 40 (binary 101000), what is x & -x?
- Reversing the 8 bits of 0b00010110 gives which value in decimal?
- The Gray code of 5 (binary 101), computed as n ^ (n >> 1), is which value?
- What is the parity of the number of set bits in 0b1011, so that the result is 1 when odd?
- What is the value of 2 + 3 << 1 & 7 in C?