现在的位置: 首页 > 综合 > 正文

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

2017年11月21日 ⁄ 综合 ⁄ 共 1398字 ⁄ 字号 评论关闭

题目大意:严格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;
}

抱歉!评论已关闭.