Problem

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

Recall the definition of the Fibonacci numbers from “Rabbits and Recurrence Relations”, which followed the recurrence relation Fn=Fn−1+Fn−2Fn=Fn−1+Fn−2 and assumed that each pair of rabbits reaches maturity in one month and produces a single pair of offspring (one male, one female) each subsequent month.

Our aim is to somehow modify this recurrence relation to achieve a dynamic programming solution in the case that all rabbits die out after a fixed number of months. See Figure 4 for a depiction of a rabbit tree in which rabbits live for three months (meaning that they reproduce only twice before dying).

Given: Positive integers n≤100n≤100 and m≤20m≤20.

Return: The total number of pairs of rabbits that will remain after the nn-th month if all rabbits live for mm months.

Sample Dataset

6 3

Sample Output

4
# coding=utf-8
### 11. Mortal Fibonacci Rabbits ### # 0 1 1 2 2 3 4 5 7 9 12 # method1
def fibonacciRabbits(n, m):
F = [0, 1, 1]
for i in range(3, n + 1):
if i <= m:
total = F[i - 1] + F[i - 2]
elif i == m + 1:
total = F[i - 1] + F[i - 2] - 1
else:
total = F[i - 1] + F[i - 2] - F[i - m - 1]
F.append(total)
return (F[n]) # print fibonacciRabbits(6,3) # method2
def f(n, k):
s = [0] * (k + 1) # list *4 [0, 0, 0, 0] s[0]代表当月出生的兔子,s[k]代表当月死亡的兔子
s[0] = 1 # [1, 0, 0, 0]
for x in range(1, n):
s[1:k + 1] = s[0:k]
print s
s[0] = sum(s[2:])
return sum(s[:-1]) print f(10, 3)

  

11 Mortal Fibonacci Rabbits的更多相关文章

  1. 指数级计算复杂度 调用Fibonacci函数次数

    指数级计算复杂度 计算调用次数 #include <stdio.h> long fibonacciCallTimes(long n); int main(void) { long resu ...

  2. 赶时髦过了一遍Swift 语言....

    Swift 语言 2014年6月3日发布,替代OBJECT-C Swift is a new programming language for creating iOS and OS X apps. ...

  3. 基础调试命令 - wt (watch and trace)

    本文介绍windbg动态调试过程中一个非常有用的命令,wt的用法. wt命令 wt命令之所以称为wt是因为它是watch and trace的简称,即用来观察和跟踪的命令.这个命令一般用在动态调试而不 ...

  4. [IOS]《A Swift Tour》翻译(一)

    以下翻译内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3768936.html 碎碎念... Swift是苹果在WWDC刚发 ...

  5. Apple Swift编程语言入门教程

    Apple Swift编程语言入门教程 作者: 日期: 布衣君子 2015.09.22 目录 1   简介 2   Swift入门 3   简单值 4   控制流 5   函数与闭包 6   对象与类 ...

  6. swift 2.x学习笔记(二)

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #008400 } p.p2 { margin: 0.0px 0. ...

  7. 来自苹果的编程语言——Swift简介转载】

    关于 这篇文章简要介绍了苹果于WWDC 2014发布的编程语言——Swift. 原文作者: Lucida Blog 新浪微博 豆瓣 转载前请保留出处链接,谢谢. 前言 在这里我认为有必要提一下Brec ...

  8. 《C与指针》第七章练习

    本章问题 1.具有空函数体的函数可以作为存根使用,你如何对这类函数进行修改,使其更有用? answer:Have the stub(存根) print out a message when it is ...

  9. 转 苹果的新编程语言 Swift 简介

    苹果官方文档地址 https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Pro ...

随机推荐

  1. $.each()的用法

    var obj = { one:1, two:2, three:3}; $.each(obj, function(key, val) { console.log(key);//one two thre ...

  2. 让多个Fragment 切换时不重新实例化

    转自:http://www.yrom.net/blog/2013/03/10/fragment-switch-not-restart/ 让多个Fragment 切换时不重新实例化 在项目中需要进行Fr ...

  3. 使用anaconda安装tensorflow (windows10环境)

    版权声明:勤学 修德 明辨 笃实 - CSDN周雄伟 https://blog.csdn.net/ebzxw/article/details/80701613 已有环境:python3.7.1 ana ...

  4. 如何判断两个IP地址是不是在同一个网段

     要判断两个IP地址是不是在同一个网段,就将它们的IP地址分别与子网掩码做与运算,得到的结果一网络号,如果网络号相同,就在同一子网,否则,不在同一子网. 例:假定选择了子网掩码255.255.254. ...

  5. java批量下载,将多文件打包成zip格式下载

    现在的需求的: 根据产品族.产品类型,下载该产品族.产品类型下面的pic包: pic包是zip压缩文件: t_product表: 这些包以blob形式存在另一张表中: t_imagefile表: 现在 ...

  6. CFGym 101490J 题解

    一.题目链接 http://codeforces.com/gym/101490 二.题面 三.题意 给你n个点,代表学生所在位置,n个点,代表老师所在位置.每个学生分配一个老师.让你找出一个最小的学生 ...

  7. fdisk用法(转载)

    Linux下的fdisk功能是极其强大的,用它可以划分出最复杂的分区,下面简要介绍一下它的用法: 对于IDE硬盘,每块盘有一个设备名:对应于主板的四个IDE接口,设备名依次为:/dev/hda,/de ...

  8. linux下python3离线加载nltk_data,不用nltk.download()

    在不能上网的服务器上把nltk_data关联到python3,已经安装anaconda3所以不需要安装nltk,环境是linux 首先没有nltk_data在使用nltk会报错 LookupError ...

  9. 36. Valid Sudoku + 37. Sudoku Solver

    ▶ 有关数独的两个问题. ▶ 36. 检测当前盘面是否有矛盾(同一行.同一列或 3 × 3 的小框内数字重复),而不关心该盘面是否有解. ● 初版代码,24 ms,没有将格子检测函数独立出去,行检测. ...

  10. PHP 成长规划

    PHP程序员的技术成长规划 作者:黑夜路人(2014/10/15) 转:http://blog.csdn.net/heiyeshuwu/article/details/40098043 按照了解的很多 ...