ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def mat_mul(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]]
M = [[1, 1], [1, 0]]
while n:
if n & 1:
R = mat_mul(R, M, m)
M = mat_mul(M, M, m)
n >>= 1
return R[0][1]
print(fib(10, 10**9), fib(90, 10**9 + 7))- A. 34 210345902
- B. 55 210345902
- C. 55 2880067194370816120
- D. 55 210345903
Correct answer: B. 55 210345902
Powers of the matrix [[1, 1], [1, 0]] give Fibonacci numbers, and reducing modulo m at each multiplication keeps the numbers small. F(10) = 55 is below the modulus, while F(90) = 2880067194370816120 reduces to 210345902 modulo 1000000007.
More Python Programming questions
- What is printed? python n = 12 print(n & 1 == 0, (n & 1) == 0, n & n - 1 == 0, 8 & 7 == 0)
- What is printed on the first line? python print(pow(2, -1, 5), pow(3, -1, 7), pow(2, -3, 9)) try: print(pow(2, -1, 4)) except ValueError…
- What is printed? python import math print(math.comb(100, 50) % 1000, len(str(math.comb(100, 50))), math.perm(5, 2), math.comb(5, 7))
- What is printed? python import math print(math.floor(-0.5), math.ceil(-0.5), math.trunc(-0.5), math.floor(2.999999999999999999))
- What is printed? python print(f'{1234567.891:.2f}', f'{255:#x}', f'{5:03d}', f'{-5:03d}', f'{-5:+04d}')
- What is printed? python print(str(-0), int('-0'), -0.0 == 0, str(-0.0), 0 -1.0)