04 Rabbits and Recurrence Relations

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,19  
方法一:

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)

  

				
时间: 2024-08-27 04:44:55

04 Rabbits and Recurrence Relations的相关文章

URBS:Non-Uniform Rational B-Splines

Maxim Shemanarev wrote: > Well, that's too big topic I suppose. Jens somehow right, we need to stop at > some point. I think that NURBS will be just the right case to stop. I didn't have  intention to go wild either. However, in order to understand 

master theorem

The master theorem concerns recurrence relations of the form: {\displaystyle T(n)=a\;T\!\left({\frac {n}{b}}\right)+f(n)} where {\displaystyle a\in \mathbb {N} {\mbox{, }}1<b\in \mathbb {R} } In the application to the analysis of a recursive algorith

Python高级特性(1):Iterators、Generators和itertools(转)

译文:Python高级特性(1):Iterators.Generators和itertools [译注]:作为一门动态脚本语言,Python 对编程初学者而言很友好,丰富的第三方库能够给使用者带来很大的便利.而Python同时也能够提供一些高级的特性方便用户使用更为复杂的数据结构.本系 列文章共有三篇,本文是系列的第一篇,将会介绍迭代器.生成器以及itertools模块的相关用法.由于作者 Sahand Saba 列举的示例中有诸多专业的数学相关内容,因此翻译中有诸多不妥之处请大家指出,非常感谢

POJ #1080 - Human Gene Functions

A classic 2D DP problem. A disguise of LCS - actually not very hard to decode: it is about 2 sequences' matching, though with a weight value of each match. The point of this problem: how to decode problem statement and how to distill the actuall mode

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

对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists以及sets等容器的支持,使用与传统数学类 似的符号标记方式,还有列表推导式这样与数学中集合推导式和集的结构式(set-builder notation)很相似的语法结构. 另外一些很吸引数学爱好者的特性是Python中的iterator(迭代器).generator(生成器)以及相关的itertools包.这 些工具帮助人们能够很轻松的写出处理诸如无穷序列(infinite sequence).随机

Causal Corpus 事件因果关系语料统计

Causal Corpus 事件因果关系语料统计 本文是对因果关系抽取领域数据库标注及开源情况的统计.除了对因果关系的标注,一些类似的语料也包含在内,从而为语料的使用提供灵活性,可以根据不同的目标选取不同的语料库. 领域简介 因果关系通常标注为 ( cause , effect , signal ) 三元组,cause 和 effect 分别代表原因事件和结果事件,signal 是语言学从因果结构的触发词,例如 because, so, thus 等等. 需要知道的是不同的因果语料对于因果关系的

算分-PRIORITIZING

Heaps and Heapsort: 堆是一种快速访问最大优先级元素的数据结构,这是快速实现选择排序的基础,但是总体来说平均速度比快排要慢一点,不过其最坏情况和平均情况是差不多的. 首先是堆的定义,每个结点比它的孩子结点都小(所以父节点最小),或者每个结点都比它的孩子结点大(父节点最大),把A[1,n]看做一个堆,i的两个孩子结点分别是2i和2i+1,这个非常方便实用.接着是删除堆的最小值(最大值同理),想法就是先删除最小值,然后把最后一个元素放到根节点并且不断sift-down,这个是一个结

精体展矿计运界布属点世据真起验青kPswaoY3w

社保划到税务征收,将大大提升社保费的征管效率.税务的征管能力是目前而言最强的,以后税务征收社保不是代收,属于本职了. 之前税局要把社保信息和交个税的工资比对起来有困难!现在好了,个税是自己的,社保也是自己的,比对困难?不存在的! 这一变革,会给那些不给员工上社保.不全额上社保的企业致命一击! 最新案例 前段时间的发改委关于限制特定严重失信人乘坐民航的一则意见--发改财金[2018]385号,其中还有税务总局的联合署名. http://weibo.com/20180408PP/2309279811

而广除还状么林验以适调半去gbQwsadnbQjN

为了从不同环节,尤其与广大使用人群直接关系的环节反映质量状况,对共享自行车投放点.运营仓库.生产企业等不同环节的产品抽查,覆盖了共享自行车从成品出厂到待投放的关键环节. 该负责人称,根据新车投放情况,结合共享自行车行业市场占有分布特点,本次重点抽查了摩拜.ofo.Hellobike三个品牌的产品,占本次抽查批次总数的83.3%.其中,在天津.无锡.武汉.广州.深圳.东莞6个城市抽查了9批次摩拜产品,占产品抽查批次总数的37.5%,抽查批次合格率88.9%,抽查不合格的1批次产品为待投放于广州市的