兔子

来源:互联网 发布:数据侠客行女主 编辑:程序博客网 时间:2024/03/29 12:52

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 33 51 1000 0

Sample Output

591267650600228229401496703205376

Author

HYNU


代码:

/*
取决于初始的兔子是成年兔[f(m,d)=1+d]还是幼兔[f(m,d)=1];

最大的可能数字 f(1,100)==2^100 大约为31位的十进制数;

每个数字用一个int num[36]来保存;
*/
#include <stdio.h>void add(int num[10][36], int i, int j){    int c = 0;    int idx;    for (idx = 35; idx > 0 ; idx--)    {        c = c + num[i][idx] + num[j][idx];        num[j][idx] = c % 10;        c = c / 10;    }}void inc(int num[10][36], int i, int j){    int c = 1;    int idx;    for (idx = 35; idx > 0 && c > 0; idx--)    {        c = c + num[i][idx];        num[j][idx] = c % 10;        c = c / 10;    }}void prt(int num[10][36], int i){    int idx;    for (idx = 0; idx < 36 && num[i][idx] == 0; idx++);    for (; idx < 36; idx++) printf("%d", num[i][idx]);}int main(){    int m, d;    while (scanf("%d %d", &m, &d) == 2 && !(m == 0 && d == 0))    {        int num[10][36] = {0};        int k = 1;        int i, j;        j = 0;        num[0][35] = 1;        for (i = 1; i <= d ; i++)        {            ++j;            j = j % m;            if (i >= m) add(num, (j - 1 + m) % m, j);            else inc(num, (j - 1 + m) % m, j);            //prt(num,j);        }        prt(num, j);        printf("\n");    }    return 0;}


0 0