[sicily]1029. Rabbit



1029. Rabbit

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

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



兔子繁殖数量问题,简单的高精度加法。要注意的是当数量很大时超过了long long 存储范围,故要采用高精度来处理。写出通项公式:count [ i ] = count [ i-1 ] + count [ i - m ],然后进行手动模拟计算即可。

#include 
#include 
#include 
using namespace std;



int main()
{
    int m,d;
    int count[101][101];
    while(cin>>m>>d && m!=0 && d!=0)
    {
        memset(count, 0, sizeof(count));
        
        count[0][100] = 1;      
        for(int i=1; i<=d; i++)
        {
            //新兔子还未长大 
            if( i=0; j--)
                {
                    //count[i][j] 已经保存了conut[i][j+1] 过来的进位值 
                    count[i][j] += count[i-1][j] + count[i-m][j];
                    if(count[i][j]>9)
                    {
                        //处理进位过程 
                        count[i][j-1] = count[i][j-1] + count[i][j]/10;
                        count[i][j] = count[i][j]%10;
                    }   
                
                }
            
            }
        }
        int index = 0;
        // 去除前缀 0; 
        while(count[d][index] == 0)
            index++;
        //输出第 d 个月后的count[d] 的值 
        for(int i=index; i<101; i++)
        {
            cout<


你可能感兴趣的:(数据结构与算法分析)