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

Calculate Fibonacci Recursively
Time Limit: 1000 MS Memory 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);
    }
}


猜你喜欢

转载自blog.csdn.net/qq_38000095/article/details/72898632
今日推荐