ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
def mat(a, b, m):
    return [[sum(a[i][k] * b[k][j] for k in range(2)) % m for j in range(2)] for i in range(2)]
def fib(n, m):
    r = [[1, 0], [0, 1]]
    b = [[1, 1], [1, 0]]
    while n:
        if n & 1:
            r = mat(r, b, m)
        b = mat(b, b, m)
        n >>= 1
    return r[0][1]
print(fib(10, 1000), fib(90, 1000))
  1. A. 55 120
  2. B. 55 121
  3. C. 54 120
  4. D. 55 20

Correct answer: A. 55 120

Fibonacci numbers come out of powers of the matrix [[1, 1], [1, 0]], and each product is reduced modulo m. F(10) is 55, and F(90) = 2880067194370816120, which ends in 120.

More Python Programming questions

Exams that ask this topic