兔子

Problem Description

The rabbits have powerful reproduction ability. One pair of adult rabbits can give birth to one pair of kid rabbits every month. And after m months, the kid rabbits can become adult rabbits.
As we all know, when m=2, the sequence of the number of pairs of rabbits in each month is called Fibonacci sequence. But when m<>2, the problem seems not so simple. You job is to calculate after d months, how many pairs of the rabbits are there if there is exactly one pair of adult rabbits initially. You may assume that none of the rabbits dies in this period.

Input

The input may have multiple test cases. In each test case, there is one line having two integers m(1<=m<=10), d(1<=d<=100), m is the number of months after which kid rabbits can become adult rabbits, and d is the number of months after which you should calculate the number of pairs of rabbits. The input will be terminated by m=d=0.

Output

You must print the number of pairs of rabbits after d months, one integer per line.

Sample Input

2 3
3 5
1 100
0 0

Sample Output

5
9
1267650600228229401496703205376
 
使用大數模板
int main()
{
    //freopen("b.txt","r",stdin);
  	int d,m,i;
	BigNum r[105];
	while(scanf("%d %d",&m,&d)==2&&d+m)
    {
        for(i=1;i<=m;i++)         
            r[i]=i+1;           //當m<=d時,成年兔子數目不變,每月產一對
        for(i=m+1;i<=d;i++)
            r[i]=r[i-1]+r[i-m];        //當m>d之後,當月兔子總數等於上月兔子總數加上m個月前的兔子總數
 
        r[d].print();
    }
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章