hrbust 1048Calculate Fibonacci Recursivel【斐波那契数列】
来源:互联网 发布:linux重启网络 编辑:程序博客网 时间:2024/06/05 16:06
This is recursion of the well-known Fibonacci Numbers:
F(0) = 0
F(1) = 1
F(n) = F(n-1) + F(n-2) for n >= 2
Auguste wants to calculate F(n) recursively, but he realizes some nnumbers might be calculated repeatedly, so he wants to know how many times the No.m Fibonacci Number will be calculated in the whole process.
For example, to calculate F(5),
F(5) = F(4) + F(3)
F(4) = F(3) + F(2)
F(3) = F(2) + F(1)
F(2) = F(1) + F(0)
F(2) = F(1) + F(0)
F(3) = F(2) + F(1)
F(2) = F(1) + F(0)
So the F(0) was calculated 3 times and F(3) was calculated 2 times. And you should think F(0) and F(1) could calculate directly, not recursively.
Now, your task is to determine how many times will the F(m) be calculated during recursive calculation of F(n).
There are many cases of input. Each case contains two integer n and m (0 <= m < n <= 10000000).
OutputFor each case, if k is the times of F(m) calculated during recursive calculation of F(n), you should print k mod 9973 (means the remainder of k divide 9973).
Sample Input5 0
5 3
3
2
题意:已知斐波那契数列的递归算法,求F(m)在计算F(n)的递归过程中计算的次数
思路:把递归的过程画成二叉树,计算一下次数就会发现F(m)的出现次数其实也是斐波那契数列,出现次数为F(n-m),F(0)和F(2)的出现次数是一样的,可以加个特判。这题范围不大,可以预处理或者直接计算。
#include<cstdio>#define mod 9973int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { int f1=0,f2=1,sum=0; for (int i=1;i<=n-(m==0?2:m);++i) { sum=(f1+f2)%mod; f1=f2; f2=sum; } if (n==2&&!m) sum=1; printf("%d\n",sum%mod); }}
- hrbust 1048Calculate Fibonacci Recursivel【斐波那契数列】
- 斐波那契Fibonacci数列
- 斐波那契 (Fibonacci)数列
- Fibonacci(斐波那契)数列实现
- 斐波那契(Fibonacci)数列
- 斐波那契(Fibonacci)数列
- 斐波那契数列(Fibonacci)
- 斐波那契(Fibonacci)数列
- 斐波那契数列(Fibonacci)
- 斐波那契(Fibonacci)数列
- 斐波那契(Fibonacci)数列
- 斐波那契数列 Fibonacci sequence
- 斐波那契数列-Fibonacci Sequence
- Fibonacci(斐波那契数列)
- 斐波那契数列(fibonacci)
- 斐波那契数列(Fibonacci)
- Fibonacci斐波那契数列序列
- 斐波那契数列Fibonacci
- 大型网站架构的发展
- Python字符编码整理
- 如何理解jsp页面中的“<base href="<%=basePath%>">”
- libcurl库的使用支持http、https、ftp上传下载
- POJ_2485 Highways
- hrbust 1048Calculate Fibonacci Recursivel【斐波那契数列】
- 获取枚举的对应值
- MFC动态创建控件并响应事件代码实现过程
- CentOS7.0基于hadoop集群安装配置Hive
- Spring JdbcTemplate详解
- dom4j 操作xml
- [RK3288][Android6.0] Audio播放时的pcm数据dump思路
- springmvc视图解析器.
- 下载服务端数据到本地保存为Excel