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,π) and the infinite sequence of odd numbers (1,3,5,7,9,…). We use the notation an to represent the n-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 Fn represents the number of rabbit pairs alive after the n-th month, then we obtain the Fibonacci sequence having terms Fn that are defined by the recurrence relation Fn=Fn−1+Fn−2 (with F1=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 n-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 n. 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≤40 and k≤5.

Return: The total number of rabbit pairs that will be present after n months if we begin with 1 pair and in each generation, every pair of reproduction-age rabbits produces a litter of k rabbit pairs (instead of only 1 pair).

斐波那契数列的一点改变,生育年龄的兔子每次生育不止一个兔子。所以公式修改为Fn=Fn−1+K*Fn−2

使用dp,就是不要按照题面意思用递归了,算过的就不算了,这样时间复杂度大大下降O(n)

本来想用dict,实现一个map,然后判断序列每个数字是否算过了,最后还是建了两个list

搞不懂 input 的值为什么都视为 str ,python 3里 input 和raw_input 是一样的,没有raw_input  了。但是必须把变量转换成int 才能算,不知道为什么。

还有就是对于list 未定义的区域,一定要先初始化值,不然直接使用会冒出奇怪的数字。 先 append(0)

n=input("n is:\n")
k=input("k is:\n")
#dynamic programming
f_seq=[1,1];
bool_list=[1,1,0];
for i in range(2,int(n)):if(bool_list[i]==0):f_seq.append(f_seq[i-1]+int(k)*f_seq[i-2])bool_list[i]=1bool_list.append(0)i+=1;else:bool_list.append(0)i+=1;
print(f_seq[int(n)-1]);

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. 【Rosalind】Rabbits and Recurrence Relations

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

  3. 04 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. Rsync命令参数详解
  2. mysql 变量null_如何检查变量是否为NULL,然后使用MySQL存储过程进行设置?
  3. vue 表格内容跳转页面_Vue项目实战系列文章(一)项目预热
  4. 9.匿名函数:lambda表达式/filter()/map()
  5. Nginx之windows下搭建
  6. ftp服务器如何复制文件路径,ftp服务器上复制文件路径
  7. 矩形波的傅里叶变换_冲激信号、门信号、方波、矩形波的傅里叶变换总结
  8. 在Centos 7中开放80端口
  9. 当你在浏览器输入一个网址,如http://www.taobao.com,按回车之后发生了什么?
  10. Oracle采用自己的公有云作为ZFS阵列的后端存储
  11. Java面试题:程序计数器为什么是私有的?
  12. 在万彩手影大师上怎么制作微课_手势动画制作|万彩手影大师心得与教程
  13. Terminate Instance 操作详解 - 每天5分钟玩转 OpenStack(33)
  14. SwiftyJSON之使用分析
  15. 什么是网络通信协议?(七层功能介绍)
  16. 2020哪款蓝牙耳机降噪效果更好?100-500平价蓝牙耳机测评推荐
  17. 如何获取win10安装驱动
  18. 双态运维联盟工作会议暨2017年度双态运维大会乌镇峰会筹备会在新华三杭州园区召开
  19. Houdini 导出.ass文件
  20. [LSTM]时间序列预测存在的问题--滑动窗口是一把双刃剑【持续更新】

热门文章

  1. YoloV4训练自己的数据集(二)
  2. c语言判断三个数能否组成三角形,随便输入三个数,看能否组成一个三角形
  3. 大数据:正在到来的数据革命,以及它如何改变政府、商业与我们的生活 [2.0升级版] - 电子书下载(高清版PDF格式+EPUB格式)...
  4. 【渗透实战】日常挖洞第二期_旁站注入“花式”漏洞拿下大型服务器
  5. USB 设备热插拔的检测
  6. 基恩士plc编程指令大全_2020东莞洪梅基恩士PLC模块回收高于同行
  7. Grafana Labs线上交流问卷调查
  8. 申宝正规股票下周市场将迎来抛压
  9. CNVD-2021-30167 用友NC命令执行漏洞复现
  10. macOS - networksetup 命令