依旧高精度


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

Problem Source

这题主要是要理清生兔子的思路,然后就又是高精度问题了

不过这题有个不对的地方,生兔子没有怀孕的阶段,m个月后的成年兔子在成年之后直接生了。。让我压力很大


// Problem#: 1029
// Submission#: 2477009
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstring>
#define MAX 40
using namespace std;
int unadult[11][MAX];
int adult[MAX];

int main() {
    while(true) {
      int m,d;
      cin >> m >> d;
      if(m == 0 && d == 0) {
            break;
      }
      //init
      memset(adult,0,sizeof(adult));
      memset(unadult,0,sizeof(unadult));
      adult[0] = 1;
      for(int i = 0;i < d;i ++) {
            
            //unadult
            for(int j = m;j > 0;j --) {
                for(int k = 0;k < MAX;k ++) {
                    unadult[j][k] = unadult[j - 1][k];
                }
            }
            
            //adult
            //add the new adult and the old
            int carry = 0;
            for(int j = 0;j < MAX;j ++) {
                adult[j] = adult[j] + unadult[m][j] + carry;
                carry = adult[j]/10;
                adult[j] %= 10;
            }
            for(int k = 0;k < MAX;k ++) {
                unadult[0][k] = adult[k];
            }
           
            //print
            /*cout << "unadult:" <<endl;
            for(int j = 0; j < m;j ++) {
                for(int k = 0;k < MAX;k ++) {
                    cout << unadult[j][MAX - 1 - k];
                }
                cout << endl;
            }
            cout << "adult" << endl;
            for(int k = 0;k < MAX;k ++) {
                    cout << adult[MAX - 1 - k];
            }
            cout << endl;*/
            
      }
      int carry = 0;
      int result[MAX];
      memset(result,0,sizeof(result));
      for(int j = 0;j < MAX;j ++) {
            result[j] = adult[j];
            for(int i = 0;i < m;i ++) {
                result[j] += unadult[i][j];
            }
            result[j] += carry;
            carry = result[j]/10;
            result[j] %= 10;
            //cout << carry << endl;
      }
      
      int resultlen = MAX-1;
      while(result[resultlen] == 0 && resultlen > 0) {
            resultlen--;
      }
      if(resultlen < 0)
        cout << "0";
      else {
      for(int i = resultlen;i >= 0;i --) {
            cout << result[i] ;
      }
    }
      cout << endl;
            
      
        
    }
    return 0;
}                                 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值