[sicily]1029. Rabbit

本文探讨了一种解决兔子繁殖数量问题的方法,通过高精度加法算法求解在特定条件下的兔子数量。重点介绍了兔子繁殖的数学模型,如何通过通项公式和手动模拟计算得出最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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 <iostream>
#include <cstring>
#include <algorithm>
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<m )
                count[i][100] = i+1;
            //新兔子长大了,也开始生小兔子了
            //count[i] = count[i-1] + count[i-m]; 
            else
            {
                //从右到左模拟手动计算加法过程 
                for(int j=100; j>=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<<count[d][i];
        }
        cout<<endl;
            
    }
  //  system("pause");
    return 0;   
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值