Problem

sequence is an ordered collection of objects (usually numbers), which are allowed to repeat. Sequences can be finite or infinite. Two examples are the finite sequence (π,−2–√,0,π)(π,−2,0,π) and the infinite sequence of odd numbers (1,3,5,7,9,…)(1,3,5,7,9,…). We use the notation anan to represent the nn-th term of a sequence.

recurrence relation is a way of defining the terms of a sequence with respect to the values of previous terms. In the case of Fibonacci's rabbits from the introduction, any given month will contain the rabbits that were alive the previous month, plus any new offspring. A key observation is that the number of offspring in any month is equal to the number of rabbits that were alive two months prior. As a result, if FnFn represents the number of rabbit pairs alive after the nn-th month, then we obtain the Fibonacci sequence having terms FnFn that are defined by the recurrence relation Fn=Fn−1+Fn−2Fn=Fn−1+Fn−2 (with F1=F2=1F1=F2=1 to initiate the sequence). Although the sequence bears Fibonacci's name, it was known to Indian mathematicians over two millennia ago.

When finding the nn-th term of a sequence defined by a recurrence relation, we can simply use the recurrence relation to generate terms for progressively larger values of nn. This problem introduces us to the computational technique of dynamic programming, which successively builds up solutions by using the answers to smaller cases.

Given: Positive integers n≤40n≤40 and k≤5k≤5.

Return: The total number of rabbit pairs that will be present after nn months, if we begin with 1 pair and in each generation, every pair of reproduction-age rabbits produces a litter of kk rabbit pairs (instead of only 1 pair).

Sample Dataset

5 3

Sample Output

19
###Rabbits and Recurrence Relations ###
def fib(n,k):
a, b = 1, 1
for i in range (2,int(n)):
a, b = b, int(k)*a + b #只需要保存最近两个月的数量即可
print b if __name__ == "__main__":
fh = open ("C:\\Users\\hyl\\Desktop\\rosalind_fib.txt")
l = fh.readline().split(' ')
n, k = l[0], l[1]
fib(n,k)

  

4.Rabbits and Recurrence Relations的更多相关文章

  1. 04 Rabbits and Recurrence Relations

    Problem A sequence is an ordered collection of objects (usually numbers), which are allowed to repea ...

  2. 11 Mortal Fibonacci Rabbits

    Problem Figure 4. A figure illustrating the propagation of Fibonacci's rabbits if they die after thr ...

  3. Python高级特性(1):Iterators、Generators和itertools(参考)

    对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists以及sets等容器的支持,使用与传统数学类 似的符号标记方式,还有列表推导式这样与数学中集合推导式和集的 ...

  4. POJ #2479 - Maximum sum

    Hi, I'm back. This is a realy classic DP problem to code. 1. You have to be crystal clear about what ...

  5. Master Theorem

    Master theorem provides a solution in asymptotic terms to solve time complexity problem of most divi ...

  6. [LeetCode] Cherry Pickup 捡樱桃

    In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...

  7. 004_Python高级特性(1):Iterators、Generators和itertools(参考)

    对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists以及sets等容器的支持,使用与传统数学类 似的符号标记方式,还有列表推导式这样与数学中集合推导式和集的 ...

  8. IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!

    Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...

  9. 动态规划-Stock Problem

    2018-04-19 19:28:21 股票问题是leetcode里一条非常经典的题目,因为其具有一定的现实意义,所以还是在数学建模方面还是有很多用武之地的.这里会对stock的给出一个比较通用的解法 ...

随机推荐

  1. html5移动web开发笔记(一)Web 存储

    localStorage - 没有时间限制的数据存储 localStorage 方法 localStorage 方法存储的数据没有时间限制.第二天.第二周或下一年之后,数据依然可用. 用户访问页面的次 ...

  2. JSP内置对象---用户登录页面(get和post)

    Login.jsp 页面: <%@ page language="java" import="java.util.*" contentType=" ...

  3. TPCH Benchmark with Impala

    1. 生成测试数据在TPC-H的官网http://www.tpc.org/tpch/上下载dbgen工具,生成数据http://www.tpc.org/tpch/spec/tpch_2_17_0.zi ...

  4. cheap gucci bags for women finish fashion jewellery has to move

    Is certainly his dresser seem or dress creation process into video clip. Bus dropped???? Especially ...

  5. 解决 主界面mainactivity 中fragment弹框把下面tab选项卡 顶上去的方案

     android:windowSoftInputMode="adjustPan"            android:configChanges="screenSize ...

  6. int->string-------------c

    void intToString(int n,char a[]){ int i=1,m=n; while((n/=10)!=0)  i++; int j=0; for(;j<i;j++){  a ...

  7. 例子:Backup Isolated Storage To SkyDrive

    本例演示了如何从隔离存储中备份文件到SkyDrive. 1. 登陆微软网站,申请“Create application”,获取一个“Client ID” 2. XAML中添加对Live相关控件库的命令 ...

  8. 应用jacob组件造成的内存溢出解决方案(java.lang.OutOfMemoryError: Java heap space)

    http://www.educity.cn/wenda/351088.html 使用jacob组件造成的内存溢出解决方案(java.lang.OutOfMemoryError: Java heap s ...

  9. 软件测试第四周--关于int.parse()的类型转换问题

    先来归纳一下我们用过的所有类型转换方法: 1. 隐式类型转换,即使用(int) 直接进行强制类型转换.这种方法的优点是简单粗暴,直接指定转换类型,没有任何保护措施,所以也很容易抛出异常导致程序崩溃.当 ...

  10. 《SharePoint 2013 应用开发实战》目录

    博客地址:http://blog.csdn.net/FoxDave 第 1 章  1 ◄SharePoint概述►        1 1.1  SharePoint的发展历程 1 1.1.1  Sha ...