兔子

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
 
 
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值