Calculate Fibonacci Recursively | ||||||
|
||||||
Description | ||||||
This is recursion of the well-known Fibonacci Numbers: |
||||||
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 |
||||||
Sample Output | ||||||
3 |
题意:已知斐波那契数列的递归算法,求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);
}
}