题目描述

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,π)(\pi, -\sqrt{2}, 0, \pi)(π,−2​,0,π) and the infinite sequence of odd numbers (1,3,5,7,9,…)(1, 3, 5, 7, 9, \ldots)(1,3,5,7,9,…). We use the notation ana_nan​ to represent the -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 FnF_nFn​ represents the number of rabbit pairs alive after the n-th month, then we obtain the Fibonacci sequence having terms FnF_nFn​ that are defined by the recurrence relation Fn=Fn−1+Fn−2F_{n} = F_{n-1} + F_{n-2}Fn​=Fn−1​+Fn−2​ (with F1=F2=1F_1 = F_2 = 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 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≤40n \leq 40n≤40 and k≤5k \leq 5k≤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).

Sample Dataset

5 3

Sample Output

19

题解

类似斐波那契数列,递推公式如下:
Fn={1(n=1,2)Fn−1+k×Fn−2(n≥3)F_n=\left\{ \begin{array}{lcl} 1 & (n=1,2)\\ F_{n-1}+k\times F_{n-2} & (n\ge 3) \end{array} \right. Fn​={1Fn−1​+k×Fn−2​​(n=1,2)(n≥3)​
可利用矩阵进行优化:
An−1=[FnFn−1]A1=[11]K=[1k10]An−1=Kn−2×A1A_{n-1}=\left[ \begin{matrix} F_n\\F_{n-1} \end{matrix} \right]\\ A_1=\left[ \begin{matrix} 1\\1 \end{matrix} \right]\\ K=\left[\begin{matrix} 1 & k\\ 1 & 0 \end{matrix} \right]\\ A_{n-1}=K^{n-2}\times A_1 An−1​=[Fn​Fn−1​​]A1​=[11​]K=[11​k0​]An−1​=Kn−2×A1​
利用快速幂算法可缩短其时间复杂度至O(log2n)O(log_2n)O(log2​n)

参考代码

from numpy import *
import numpy as np
import rewith open("rosalind_fib.txt", "r") as f:s = f.readline()s = re.split(r" ", s)n = int(s[0])k = int(s[1])f.close()K = mat(array([[1, k], [1, 0]]))
A1 = mat(array([[1], [1]]))
Ans = (K ** (n-2)) * A1print(Ans[0, 0])

【Rosalind】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. 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. 【Rosalind】Computing GC Content

    问题描述 Problem The GC-content of a DNA string is given by the percentage of symbols in the string that ...

  5. 【Rosalind】Finding a Protein Motif – 正则表达式的使用

    Rosalind习题Finding a Protein Motif 先祝大家中秋和国庆双节快乐! 在这个日历我还在努力地学习代码(指只有晚上写了2h白天都在睡觉)zzzz 最近在做Rosalind上的 ...

  6. python计算相对分子质量_【ROSALIND】【练Python,学生信】20 计算多肽链的质量

    题目: 计算多肽链的质量 Given: A protein string P of length at most 1000 aa. 所给:一条不超过1000 aa长的氨基酸序列P. Return: T ...

  7. 【Rosalind】Counting Point Mutations

    问题描述 Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are c ...

  8. python输出一棵松树_【ROSALIND】【练Python,学生信】32 构建一棵树

    题目: 构建一棵树(Completing a Tree) Given: A positive integer n (n≤1000) and an adjacency list correspondin ...

  9. python 进化树_【ROSALIND】【练Python,学生信】48 Newick格式与进化树

    题目: Newick格式与进化树结点间的距离(Distances in Trees) Given: A collection of n trees (n≤40) in Newick format, w ...

最新文章

  1. Hadoop集群搭建(二:集群主机间免密登录配置)
  2. “AI赋能,驱动未来”—— 2018中国人工智能峰会(南京)圆满落幕
  3. 【源码】Word转PDF V1.0.1 小软件,供新手参考
  4. IE6,IE7,FF等浏览器不兼容原因及解决办法(转)
  5. HashMap 怎么 hash?又如何 map?
  6. Linux系统的压缩技术
  7. ITK:计算矢量图像中每个像素的大小以生成大小图像
  8. TensorFlow 中三种启动图用法
  9. FTP和TCP、UDP
  10. linux系统中删除一次性任务流程,Linux系统中的计划任务
  11. 互联网反欺诈体系中的常用技术和数据类型
  12. 【笔记】LR录制方式和常用函数
  13. Create a restful application with AngularJS and CakePHP (I)
  14. SQL 2012企业版安装教程
  15. CSS模拟微信对话框
  16. 《鸟哥的Linux私房菜-基础篇》学习笔记
  17. 游戏原画之组合画法-张聪-专题视频课程
  18. 表弟准备买房,让我帮他分析分析哪个小区最合适,我直接用python下载了本地所有房源信息做成了可视化图
  19. java整合阿里pc网站支付
  20. 简易云台制作记录(内含MPU6050角度的求法)

热门文章

  1. Linux 中 Netcat 工具的使用
  2. 临界Hashgard:读懂智能合约与虚拟机,看这一篇就够了!
  3. vuex报错Computed property “xxx“ was assigned to but it has no setter.
  4. Arduino 使用 旋转编码器
  5. python对象模型_python 实现对象模型
  6. Spring Cloud OAuth2中访问/oauth/token报Unsupported grant type: password问题的解决
  7. SpringBoot application.properties和application.yml配置详解
  8. 本地下载配置dubbo.xsd文件
  9. 定时器:setTimeout()
  10. 南宁漏水检测:热烈祝贺广西中水荣获广西首届著名品牌