兔子

来源:互联网 发布:韩信点兵算法程序员 编辑:程序博客网 时间:2024/04/21 00:18

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
 
 
#include<stdio.h>#include<string.h>#include<malloc.h>int main(){    int i, j, k, m, n, d, x, y, *c, temp, sum;    char a[200][200] = {0};    while(scanf("%d%d", &m, &d) != EOF)    {        if(m == 0 && d == 0)  break;        for(i = 1, j = 0; i <= m; i++)  //           {            if((i + 1) > 9)            {                a[i][j++] = ((i + 1) / 10) + '0';                a[i][j] = ((i + 1) % 10) + '0';            }            else   a[i][j] = i + 1 + '0';            a[i][++j] = '\0';            j = 0;        }        for(i = m + 1; i <= d; i++)    //   经过m个月长大的兔子数
        {            x = strlen(a[i - 1]);            y = strlen(a[i - m]);   //关系是 a[i]=a[i-1]+a[i-m]            sum = 0;            k = 0;            temp = 0;            if(x > y) temp = x;            else temp = y;            c = (int *)malloc(sizeof(int ) * (temp + 2));               //.............. 以下是大数处理,作加法运算.............. //            for(temp = 0, n = x - 1, j = y - 1; n >= 0, j >= 0; n--, j--)            {                sum = (a[i - 1][n] - '0' + a[i - m][j] - '0') + temp;                if(sum > 9)                {                    c[k++] = sum % 10;                    temp = sum / 10;                }                else                {                    c[k++] = sum;                    temp = 0;                }            }            if(n < 0 && j < 0 && temp)   c[k++] = temp;            while(j >= 0)            {                sum = a[i - m][j] - '0' + temp;                if(sum > 9)                {                    c[k++] = sum % 10;                    temp = sum / 10;                }                else                {                    c[k++] = sum;                    temp = 0;                }                j--;            }            while(n >= 0)            {                sum = a[i - 1][n] - '0' + temp;                if(sum > 9)                {                    c[k++] = sum % 10;                    temp = sum / 10;                }                else                {                    c[k++] = sum;                    temp = 0;                }                n--;            }            for(n = k - 1, j = 0; n >= 0; n--)                a[i][j++] = c[n] + '0';            a[i][j] = '\0';            free(c);             //.............. 以上是大数处理.............. //        }        printf("%s\n", a[d]);    }    return 0;}

0 0