Rabbit
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 49 Accepted Submission(s) : 12
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 pairs of rabbits in each month is called Fibonacci seqence. But when m<>2, the problem sees 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 assure that none of the rabbits dies in this period.
As we all know, when m=2, the sequence of the number pairs of rabbits in each month is called Fibonacci seqence. But when m<>2, the problem sees 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 assure that none of the rabbits dies in this period.
Input
The input may have multiple test case. 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 0 0
Sample Output
5 9
#include<iostream> #include<sstream> #include<string.h> #include<stdio.h> #include<algorithm> #define maxn 2100 using namespace std; struct bign { int len,s[maxn]; bign() { memset(s,0,sizeof(s)); len=1; } bign(int num) {*this=num;} bign(const char* num){*this=num;} bign operator = (int num) { char s[maxn]; sprintf(s,"%d",num); *this=s; return *this; } bign operator = (const char* num) { len=strlen(num); int i; for(i=0;i<len;i++) s[i]=num[len-i-1]-'0'; return *this; } string str() const { string res = ""; int i; for(i=0;i<len;i++) res=(char)(s[i]+'0')+res; if(res=="") res="0"; return res; } void clean() {while(len>1&&!s[len-1]) len--;} bign operator + (const bign& b) const { bign c; c.len=0; int i,g; for(i=0,g=0;g||i<max(len,b.len);i++) { int x=g; if(i<len) x+=s[i]; if(i<b.len) x+=b.s[i]; c.s[c.len++]=x%10; g=x/10; } return c; } }; istream& operator >> (istream &in, bign& x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream &out, const bign& x) { out << x.str(); return out; } int main() { int m,d; bign a[110]; a[0]=1; a[1]=2; while(cin>>m>>d) { int i; if (m==0&&d==0) break; else { for (i=1; i<=m-1; i++) a[i]=a[i-1]+1; for (i=m; i<110; i++) { if(m>=d) a[i]=a[i-1]+1; else a[i]=a[i-1]+a[i-m]; } cout<<a[d]<<endl; } } return 0; }