We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
MOD=10**9+7deffib_fast_doubling(n,f0=0,f1=1):"""Computes F(n) given F(0)=f0, F(1)=f1 using fast doubling (O(log n))."""def_fib(k):ifk==0:return(0,1)else:a,b=_fib(k>>1)c=(a*((b*2-a)%MOD))%MODd=(a*a+b*b)%MODifk&1:return(d,(c+d)%MOD)else:return(c,d)fn,fn1=_fib(n)# Combine results to support custom f0, f1return(fn*f1+(fn1-fn)*f0)%MODdefsolve(a,b,n):returnfib_fast_doubling(n,f0=a,f1=b)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Fibonacci Finding (easy)
You are viewing a single comment's thread. Return to all comments →
for python, is not easy...