-
Notifications
You must be signed in to change notification settings - Fork 1
/
fib.py
24 lines (20 loc) · 880 Bytes
/
fib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import sys
def modified_fibonacci(n, k):
''' Modified Fibonacci Sequence
:param n: number of generations (integer)
:param k: number of pairs a reproduction age pair of rabbits produces (integer)
:return: the nth element in the sequence (integer)
'''
result = {1: 1, 2: 1}
if n in result:
return result[n]
result[n] = modified_fibonacci(n - 1, k) + k * modified_fibonacci(n - 2, k)
return result[n]
if __name__ == "__main__":
'''
Given: Positive integers n≤40 and k≤5.
Return: The total number of rabbit pairs that will be present after n months, if we begin with 1 pair and in each
generation, every pair of reproduction-age rabbits produces a litter of k rabbit pairs (instead of only 1 pair).
'''
n, k = [int(x) for x in sys.stdin.read().splitlines()[0].split(" ")]
print(modified_fibonacci(n, k))