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))- A. 24 0 0
- B. 24 1 0
- C. 24 1 1
- 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
- What is printed? python import math n = 1016 - 1 print(math.isqrt(n), int(n 0.5))
- What is printed? python print(sorted([('b', 2), ('a', 2), ('c', 3)], key=lambda t: (-t[1], t[0])))
- What is printed? python def ok(s): def chk(l, r): while l < r: if s[l] != s[r]: return False l += 1 r -= 1 return True l, r = 0, len(s)…
- What is printed? python def removek(num, k): st = [] for d in num: while k and st and st[-1] > d: st.pop() k -= 1 st.append(d) st =…
- What is printed? python def divcount(a, k): cnt = {0: 1} s = 0 ans = 0 for x in a: s = (s + x) % k ans += cnt.get(s, 0) cnt[s] =…
- What is printed? python print(sorted(['b', 'A', 'a', 'B']), sorted(['b', 'A', 'a', 'B'], key=str.lower))