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))- A. 55 120
- B. 55 121
- C. 54 120
- 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
- What is printed? python class A: x = 1 def get(self): return self.x a = A() a.x = 5 A.x = 9 b = A() print(a.get(), b.get(), A.x)
- What is printed? python class A: def f(self): return ['A'] class B(A): def f(self): return ['B'] + super().f() class C(A): def f(self)…
- What is printed? python from itertools import product print(len(list(product('01', repeat=3))), list(product('ab', 'x'))[-1], sum(1 for in…
- What is printed? python import heapq, itertools c = itertools.count() h = [] for p, name in [(2, 'x'), (1, 'y'), (2, 'z'), (1, 'w')]…
- What is printed? python d = dict.fromkeys(['a', 'b'], []) d['a'].append(1) print(d, d['b'] is d['a'])
- What is printed? python def pc(x): c = 0 while x: x &= x - 1 c += 1 return c print(pc(0b1011011), pc(255), pc(0))