sicily 1029. Rabbit

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

ZSUACM Team Member



高精度加法思想:


/* 本题思想:
 * 首先肯定要用string型变量
 * 创建一个arr[101]数组,每个元素代表这个月中新出生的小兔对数。 (其中a[0]为初始状态,a[1]-a[100]分别代表第一个月到第一百个月)
 * 第i个月新出生数量是a[0] -> a[i - m] 所有元素相加的和
 * 然后当计算出a[d]后,将a[0]+...a[d]以高精度加法算出来即为结果
 */
#include<iostream>
#include<string>

using namespace std;

string arr[101];


string LongAdd(string a, string b) {
	int al = a.length();
	int bl = b.length();
	int ma = al > bl ? al : bl;
	for (int i = al; i < ma; i++) {
		a = "0" + a;
	}
	for (int i = bl; i < ma; i++) {
		b = "0" + b;
	}
	//cout << "a:" << a << "  b:" << b << endl;
	if (al == bl) {
		a = "0" + a;
		b = "0" + b;
		//cout << "a:" << a << "  b:" << b << endl;
	}
	al = a.length();
	int num;
	for (int i = al - 1; i >= 0; i--) {
		num = (int)(a[i] - '0') + (int)(b[i] - '0');
		if (num >= 10) {
			a[i] = (char)(num - 10 + '0');
			int k = i - 1;
			while ((int)(a[k] - '0') + 1 >= 10 && k >= 0) {
				//cout << "while" << endl;
				int n = (int)(a[k] - '0') + 1;
				a[k] = (char)(n - 10 + '0');
				k--;
			}
			a[k] = (char)((a[k] - '0') + 1 + '0');
		}
		else {
			a[i] = (char)(num + '0');
		}
	}
	while (a[0] == '0') {
		a.erase(a.begin());
	}
	return a;
}

string newborn(int i, int m) {
	string sum = "0";
	if (i <= m) {
		return "1";
	}
	else {
		for (int j = 0; j <= i - m; j++) {
			sum = LongAdd(sum, arr[j]);
		}
		return sum;
	}
}

int main() {
	int m, d;
	cin >> m >> d;
	while (m != 0 || d != 0) {
		arr[0] = "1";
		for (int i = 1; i <= d; i++) {
			arr[i] = newborn(i, m);
			//cout << arr[i] << endl;
		}
		string sum = "0";
		for (int i = 0; i <= d; i++) {
			sum = LongAdd(sum, arr[i]);
		}
		cout << sum << endl;
		cin >> m >> d;
	}
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值