BZOJ 1089 SCOI 2003 严格n元树 递推+高精度

题目大意:严格n元树的定义是所有的点都有n个儿子节点或者没有儿子节点。问m层的严格n元树的个数是多少。


思路:递推式十分简单,这题主要是再考高精度。

递推式S[i] = S[i - 1] ^P + 1,ans = S[i] - S[i - 1]。

高精度的话这个题用的还是挺多的,有+-*^还有输出,写的很爽


CODE:

#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define BASE 1000
#define MAX 1010
using namespace std;

struct BigInt{
	int num[MAX],len;

	BigInt(int _ = 0) {
		memset(num,0,sizeof(num));
		if(_) {
			num[1] = _;
			len = 1;
		}
		else	len = 0;
	}
	BigInt operator +(const BigInt &a)const {
		BigInt re;
		re.len = max(len,a.len);
		int temp = 0;
		for(int i = 1; i <= re.len; ++i) {
			re.num[i] = num[i] + a.num[i] + temp;
			temp = re.num[i] / BASE;
			re.num[i] %= BASE;
		}
		if(temp)	re.num[++re.len] = temp;
		return re;
	}
	BigInt operator -(const BigInt &a)const {
		BigInt re = *this;
		for(int i = 1; i <= len; ++i) {
			re.num[i] -= a.num[i];
			if(re.num[i] < 0)
				re.num[i] += BASE,--re.num[i + 1];
			if(re.num[i])	re.len = i;
		}
		return re;
	}
	BigInt operator *(const BigInt &a)const {
		BigInt re;
		for(int i = 1; i <= len; ++i)
			for(int j = 1; j <= a.len; ++j) {
				re.num[i + j - 1] += num[i] * a.num[j];
				re.num[i + j] += re.num[i + j - 1] / BASE;
				re.num[i + j - 1] %= BASE;
			}
		re.len = len + a.len;
		if(!re.num[re.len])	--re.len;
		return re;
	}
	void operator *= (const BigInt &a) {
		*this = *this * a;
	}
	BigInt operator ^(int a)const {
		BigInt re(1);
		for(int i = 1; i <= a; ++i)
			re *= *this;
		return re;
	}
}s[50];

ostream &operator <<(ostream &os,const BigInt &a)
{
	os << a.num[a.len];
	for(int i = a.len - 1; i; --i)
		os << fixed << setfill('0') << setw(3) << a.num[i];
	return os;
}

int n,p;

int main()
{
	cin >> n >> p;
	s[0] = BigInt(1);
	for(int i = 1; i <= p; ++i)
		s[i] = (s[i - 1] ^ n) + BigInt(1);
	BigInt ans = s[p] - s[p - 1];
	cout << ans << endl;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值