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. one2many &&many2many

    只记录双向的情况(双向是单向的一种)  @OneToMany 和 @ManyToOne :一个Group 包含多个 User; Group.class package com.XX.model; im ...

  2. JDK 9 & JDK 10 新特性

    JDK 9 新增了不少特性,官方文档:https://docs.oracle.com/javase/9/whatsnew/toc.htm#JSNEW-GUID-527735CF-44E1-4144-9 ...

  3. [Java][Web]Request 实现转发和 MVC 设计模式

    String data = "aaaaa"; request.setAttribute("data",data); // 将数据存在 request reque ...

  4. Python GUI编程(Tkinter) windows界面开发

    Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py 1 #!C:\Python27\python.e ...

  5. zookeeper的四种类型的节点

    znode创建类型(CreateMode),有以下四种: PERSISTENT 持久化节点 PERSISTENT_SEQUENTIAL 顺序自动编号持久化节点,这种节点会根据当前已存在的节点数自动加 ...

  6. 第三章 Istio基本介绍

    3.1 Istio的核心组件及其功能 Istio总体分两部分:控制面和数据面. 数据面(sidecar):sidecar通过注入的方式和业务容器共存于一个pod,会劫持业务容器的流量,并接受控制面组件 ...

  7. 十六.jQuery源码解析之Sizzle设计思路.htm

    为了便于后面的叙述,需要了解一些相关术语和约定. 并列选择器表达式:"div,p,a"====>div,p,a是并列的. 块表达式:"div>p"中 ...

  8. node 中的定时器, nextTick()和setImmediate()的使用

    1.node中使用定时器的问题在于,它并非精确的.譬如setTimeout()设定一个任务在10ms后执行,但是在9ms后,有一个任务占用了5ms,再次轮到定时器时,已经耽误了4ms. 好了node中 ...

  9. C++中结构体与类的区别 2

    这里有两种情况下的区别.(1)C的struct与C++的class的区别.(2)C++中的struct和class的区别.在第一种情况下,struct与class有着非常明显的区别.C是一种过程化的语 ...

  10. da分布式算法

    参考学习<数字信号处理的FPGA实现> 思想如图: 在下半部分可以看到:是将N阶的数B bit,一位一位的移入LUT然后经过累加器.其中N个数需要2.^N次方长度的LUT,B bit表示需 ...