Created
July 7, 2017 16:51
-
-
Save hrsvrdhn/88225b5b6cf84b1bcf726453d94eefd1 to your computer and use it in GitHub Desktop.
Fibonacci Finding in NlogN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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