hrbust 1048Calculate Fibonacci Recursivel【斐波那契数列】

该博客探讨了斐波那契数列的递归算法,指出在计算F(n)的过程中,F(m)的出现次数遵循斐波那契数列的规律,具体为F(n-m)次。作者提到了可以通过构建递归树来理解这一现象,并建议由于问题规模不大,可以考虑预处理或直接计算解决方案。
摘要由CSDN通过智能技术生成

Calculate Fibonacci Recursively
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 97(18 users)Total Accepted: 28(13 users)Rating: Special Judge: No
Description

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

Input

There are many cases of input. Each case contains two integer n and m (0 <= m < n <= 10000000).

Output

For 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 Input

5 0
5 3

Sample Output

3
2


题意:已知斐波那契数列的递归算法,求F(m)在计算F(n)的递归过程中计算的次数


思路:把递归的过程画成二叉树,计算一下次数就会发现F(m)的出现次数其实也是斐波那契数列,出现次数为F(n-m),F(0)和F(2)的出现次数是一样的,可以加个特判。这题范围不大,可以预处理或者直接计算。


#include<cstdio>
#define mod 9973
int 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);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值