You are viewing a single comment's thread. Return to all comments →
Python 3 solution
from fractions import gcd import sys sys.setrecursionlimit(10 ** 9) p = 10 ** 9 + 7 def matrix_mul(A, B, p = 10 ** 9 + 7): ''' multiply two 2x2 matrices mod p ''' return [[(A[0][0] * B[0][0] + A[0][1] * B[1][0]) % p , (A[0][0] * B[0][1] + A[0][1] * B[1][1]) % p], [(A[1][0] * B[0][0] + A[1][1] * B[1][0]) % p, (A[1][0] * B[0][1] + A[1][1] * B[1][1]) % p]] def matrix_pow(A, n, p = 10 ** 9 + 7): ''' calculate A^n mod p for a 2x2 matrix ''' if n == 0: return [[1, 0], [0, 1]] B = matrix_pow(A, n // 2, p) B = matrix_mul(B, B, p) if n % 2 == 1: B = matrix_mul(B, A, p) return B def fibonacci(A, B, n, p = 10 ** 9 + 7): ''' Given that F[0]=A, F[1]=B and F[i]=F[i-1]+F[i-2], find F[n]%p ''' M = matrix_pow([[1, 1], [1, 0]], n, p) return (M[1][0] * B + M[1][1] * A) % p def fib_lcm(A): if len(A) == 1: return fibonacci(0, 1, A[0]) B = list(set([gcd(A[0], a) for a in A[1:]])) return (fib_lcm(A[1:]) * fibonacci(0, 1, A[0]) * pow(fib_lcm(B), p - 2, p)) % p N = int(input()) A = [int(input()) for _ in range(N)] print(fib_lcm(A))
Seems like cookies are disabled on this browser, please enable them to open this website
Fibonacci LCM
You are viewing a single comment's thread. Return to all comments →
Python 3 solution