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

注: F1 = 1,  F2 = 1, F3 = F2+F1*2,
这里k=3, 所以手写答案为 1,1,4,7,
方法一:

def fibonacciRabbits(n, k):
F = [1, 1]
generation = 2
while generation <= n:
F.append(F[generation - 1] + F[generation - 2] * k)
generation += 1
return (F[n-1]) print fibonacciRabbits(5, 3) 方法二: def fibonacciRabbits(n,k):
if n <= 2:
return (1)
else:
return (fibonacciRabbits(n-1,k) + fibonacciRabbits(n-2,k)*k)
print fibonacciRabbits(5,3)

  


04 Rabbits and Recurrence Relations的更多相关文章

  1. 4.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. Maple拥有优秀的符号计算和数值计算能力

    https://www.maplesoft.com/products/maple/ Maple高级应用和经典实例: https://wenku.baidu.com/view/f246962107221 ...

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

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

  5. 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 ...

  6. Master Theorem

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

  7. [LeetCode] Cherry Pickup 捡樱桃

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

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

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

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

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

随机推荐

  1. tomcat 关闭出现 Connection refused 解决方法

    1.找到tomcat占用的端口 ps -eaf|grep tomcat 2.杀掉tomcat进程 kill -p  6453 后记: 杀其他进程的时候,上面的方法不可以,用下面的就ok了 lsof - ...

  2. crontab 定时任务设置

    CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...

  3. 把价钱转化为xx.xx的形式

    把number类型转化为字符串类型let orderPayFeeStr = this.state.orderPayFee.toString();//商品价格转化为字符串if(orderPayFeeSt ...

  4. IDA Pro 权威指南学习笔记(十四) - 操纵函数

    IDA 无法定位一个函数调用,由于没有直接的方法到达函数,IDA 将无法识别它们 IDA 可能无法正确确定函数的结束部分,需要手动干预,以更正反汇编代码中的错误 如果编译器已经将函数分割到几个地址范围 ...

  5. Ubuntu 下 安装 hadoop(转+修改)

    出于需要在电脑上安装hadoop,版本:hadoop 1.2.1 (stable) 按照网上教程安装成功,把一点需要修改的地方说说. 参考博客: ubuntu12.04 hadoop单机模式和伪分布模 ...

  6. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  7. update project maven项目的时候出错

    preference node "org.eclipse.wst.validation"has been remove 上面的错误是因为修改包名无法互相引入导致的,仅仅需要将Ecl ...

  8. [z]单次遍历带权随机选取

    http://www.gocalf.com/blog/weighted-random-selection.html 没事可以看看,这个博客里面很多文章不错

  9. Latex编译后Yap查看报错Not all fonts could be loaded

    在用Latex写中文大论文时,编译后,用Yap查看DVI文件,打开时,Yap报错: "Not all fonts could be loaded. See 'File->Documen ...

  10. leetcode462

    public class Solution { public int MinMoves2(int[] nums) { var list = nums.OrderBy(x => x).ToList ...