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