Problem

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

A 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, 1for i in range (2,int(n)):a, b = b, int(k)*a + b #只需要保存最近两个月的数量即可print  bif __name__ == "__main__":fh = open ("C:\\Users\\hyl\\Desktop\\rosalind_fib.txt")l = fh.readline().split(' ')n, k = l[0], l[1]   fib(n,k)

  

转载于:https://www.cnblogs.com/ylHe/p/6178312.html

4.Rabbits and Recurrence Relations相关推荐

  1. 【Rosalind】Rabbits and Recurrence Relations

    题目描述 Problem A sequence is an ordered collection of objects (usually numbers), which are allowed to ...

  2. 04 Rabbits and Recurrence Relations

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

  3. Rabbits and Recurrence Relations

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

  4. 数据警务_如何停止网上种族歧视警务

    数据警务 重点 (Top highlight) "You don't need to get all worked up over this." "You're too ...

  5. python生信编程1-5

    文章目录 Counting DNA Nucleotides/统计ATCG数 Problem Sample Dataset Sample Output Transcribing DNA into RNA ...

  6. 你在Java中用过动态规划吗?

    点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 1. 介绍 动态规划典型的被用于优化递归算法,因为它们倾向于以指数的方式进行扩展.动态规划主 ...

  7. 【学术相关】2020 图灵奖发布,两位编程语言先驱、《龙书》作者获奖

    刚刚,最新一届图灵奖颁出,Alfred Aho.Jeffrey Ullman两位计算机科学家因基础算法和理论而获得2020年ACM 图灵奖. 根据ACM的介绍,两人获奖的成就是:"提出了编程 ...

  8. 递归函数非递归化_递归神秘化

    递归函数非递归化 by Sachin Malhotra 由Sachin Malhotra 递归神秘化 (Recursion Demystified) In order to understand re ...

  9. commons-math3-3.6.1-org.apache.commons.math3.analysis.function-包下的类(三)-中英对照文档及源码赏析

    commons-math3-3.6.1-org.apache.commons.math3.analysis.function-包下的类(三)-中英对照文档及源码赏析 摘要:中英对照文档.源码赏析.or ...

最新文章

  1. 2022-2028年中国塑料制品行业投资分析及前景预测报告(全卷)
  2. Mybatis入门:1(Mybatis框架的环境搭建)
  3. 注入游戏没有焦点_不戴眼镜看3D电影、玩3D游戏,这项技术能焕发端游市场第二春吗?...
  4. java list翻转_浅谈Java数据结构中的常见问题
  5. linux mysql tmp_linux下mysql的tmp_table_size改变大小方法
  6. Android 屏幕尺寸知识
  7. 【渝粤题库】陕西师范大学300008 历史教学论
  8. mysql 5.6.34 二进制下载_linux下安装mysql二进制版本(5.6.34)
  9. java中decrement,Java Math decrementExact()用法及代码示例
  10. 学习人工智能的头四个月
  11. linux parted命令,Linux分区之parted命令详解
  12. Spring框架零基础学习(一):IOC|DI、AOP
  13. 参数调整 新部署rac_CDH5部署三部曲之三:问题总结
  14. scala面试问题_Scala高级面试问答
  15. POJ 2409 Let it Bead(Polya简单应用)
  16. 《软件测试》[(美)Ron Patton]第三章阅读总结
  17. 没有计算机的一天英语作文带翻译,美好的一天糟糕的一天英语作文带翻译
  18. 拟物化设计与扁平化设计
  19. RedisTemplate that could not be found如何解决?
  20. DataFrame写入mysql时报错Duplicate entry ‘...‘ for key ‘PRIMARY

热门文章

  1. 力扣300、最长上升子序列动态规划刷题
  2. 李彦宏一句“车水马龙”,中国AI的一幕天地宽广
  3. threeJs画正方体
  4. 基恩士KV8000系列PLC,控制松下总线伺服和山社总线步进电机,全套图纸,DWG格式,一整套图纸发给你
  5. wireshark抓的包中文显示点点....
  6. SeSe 2004年9月18日, 0:59:51
  7. 网页直接访问链接不让安全中心拦截
  8. Redis主从、哨兵、 Cluster集群一锅端!
  9. 记录DS-5上工程AC5移植到AC6的奇葩问题
  10. 华为云 Tomcat配置https证书