1029. Rabbit
1029. Rabbit
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 3 3 5 1 100 0 0
Sample Output
5 9 1267650600228229401496703205376
Problem Source
ZSUACM Team Member
这题需要用高精度加法。递推关系是 f [n] = f [n - 1] + f [n - m] (其中 n 为月数,m 为题中的每对兔子长大成为成年兔子需要的月数)。 关于递推关系的解释: 这个月兔子的总数 = 上个月兔子的总数 + 新出生兔子的总数。而新出生兔子的总数 = n - m 个月前兔子的总数,因为那时新出生的幼年兔子到这个月已经变成了成年兔子可以生育,而那时其他的兔子当然也可以生育,即到 n 个月时,n - m 个月前的全部兔子都会生兔子,而在 n - m 个月 和 n 个月之间新出生的兔子到 n 个月时是没有生育能力的。代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int f[110][51];
void init(int idx, int num) //初始化 m 个月前兔子的总数
{
for (int i = 50; num != 0 && i >= 0; i --)
{
f[idx][i] = num % 10;
num /= 10;
}
}
void addition(int idx, int m) //高精度加法
{
int temp;
for (int i = 50; i >= 1; i --)
{
temp = f[idx][i];
f[idx][i] = (temp + f[idx - 1][i] + f[idx - m][i]) % 10; //注意是先加再模
f[idx][i - 1] = (temp + f[idx - 1][i] + f[idx - m][i]) / 10; //注意是先加再除
}
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int m, d, c;
while (scanf("%d%d", &m, &d), m > 0 && d > 0)
{
memset(f, 0, sizeof(f));
for (int i = 0; i <= d; i ++)
{
if (i <= m)
init(i, i + 1); //初始化 m 个月前兔子的总数
else
addition(i, m); //递推兔子的总数
}
for (int i = 0; i <= 50; i ++) //去掉结果开头的 0 。
{
if (f[d][i] != 0)
{
c = i;
break;
}
}
for (int i = c; i <= 50; i ++)
printf("%d", f[d][i]);
printf("\n");
}
return 0;
}
时间 大小
0.02s | 336KB |
好吧,上面是别人的答案,下面才是我自己的:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int convertToInt(char s)
{
switch(s)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return 0;
}
}
char convertToChar(int s)
{
switch(s)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
}
}
string bigadd(string a,string b)
{
int up=0,i=a.length(),j=b.length(),temp;
string result="";
if(i>j)
for(int l=i-j;l>0;l--)
{
b='0'+b;
}
else
for(int l=j-i;l>0;l--)
{a='0'+a;}
i=a.length();
i--;
while(true)
{
if(i<0&&up==0){break;}
if(i>-1)
temp=convertToInt(a[i])+convertToInt(b[i])+up;
else
temp=up;
if(temp/10>0)
{
up=1;
}
else
{
up=0;
}
result=convertToChar(temp%10)+result;
i--;
}
return result;
}
string f(int m,int d)
{
vector<string> v;
for(int i=0;i<=d;i++)
{
if(i==0)
v.push_back("1");
if(i<m&&i>0){v.push_back(bigadd(v[i-1],"1"));}
if(i>=m){v.push_back(bigadd(v[i-1],v[i-m]));}
}
/*
if(d==0){return "1";}
if(d<m&&d>0){return bigadd(f(m,d-1),"1");}
return bigadd(f(m,d-1),f(m,d-m));
*/
return v[d];
}
int main()
{
int m,d;
// ifstream cin("input.txt");
while(true)
{
cin>>m>>d;
if(m==0&&d==0)break;
cout<<f(m,d)<<endl;
}
return 0;
}
时间 大小
0.08s | 312KB |
足足是别人的4倍,惭愧~继续努力。