Skip to content

Instantly share code, notes, and snippets.

@avamsi
Created November 28, 2014 13:12
Show Gist options
  • Save avamsi/93a4277cc4fed6970096 to your computer and use it in GitHub Desktop.
Save avamsi/93a4277cc4fed6970096 to your computer and use it in GitHub Desktop.
mod = 10**9 + 7
cache = {0: 1, 1: 1}
def fibo(x):
try:
return cache[x]
except KeyError:
pass
k = x / 2
if x % 2:
cache[x] = (fibo(k) * (2*fibo(k-1) + fibo(k))) % mod
return cache[x]
else:
cache[x] = (fibo(k - 1)**2 + fibo(k)**2) % mod
return cache[x]
for _ in xrange(int(raw_input())):
n, m = map(int, raw_input().split())
print (fibo(m + 1) - fibo(n)) % mod
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment