ProgrammingHardCodeforces-style · placement coding round

What is printed?

What is printed?
def mpow(b, e, m):
    r = 1
    b %= m
    while e:
        if e & 1:
            r = r * b % m
        b = b * b % m
        e >>= 1
    return r
print(mpow(2, 10, 1000), mpow(7, 0, 13), mpow(5, 3, 1))
  1. A. 24 0 0
  2. B. 24 1 0
  3. C. 24 1 1
  4. D. 0 1 0

Correct answer: B. 24 1 0

Square-and-multiply handles one bit of the exponent per step. 2^10 mod 1000 is 24, any power with exponent 0 is 1, and everything is 0 modulo 1.

More Python Programming questions

Exams that ask this topic