Skip to content

Instantly share code, notes, and snippets.

@hrsvrdhn
Created July 7, 2017 16:51
Show Gist options
  • Save hrsvrdhn/88225b5b6cf84b1bcf726453d94eefd1 to your computer and use it in GitHub Desktop.
Save hrsvrdhn/88225b5b6cf84b1bcf726453d94eefd1 to your computer and use it in GitHub Desktop.
Fibonacci Finding in NlogN
def multiply(a,b):
result = [[0,0],[0,0]]
for i in range(2):
for j in range(2):
summ = 0
for k in range(2):
summ += a[i][k]*b[k][j]
result[i][j] = summ%(1000000007)
return result
def findingfibo(a,b,n):
ans = [[a+b,b],[b,a]]
matrix = [[1,1],[1,0]]
while n>0:
if (n&1):
ans = multiply(ans,matrix)
matrix = multiply(matrix,matrix)
n /= 2
return ans[0][1]
a,b,n = map(int,raw_input().split())
print findingfibo(a,b,n-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment