Problem B

Complete Tree Labeling!

Input: standard input

Output: standard output

Time Limit: 45 seconds

Memory Limit: 32 MB

A complete k-ary tree is a k-ary tree in which all leaves have same depth and all internal nodes have degree k. This k is also known as the branching factor of a tree. It is very easy to determine the number of nodes of such a tree. Given the depth and branching factor of such a tree, you will have to determine in how many different ways you can number the nodes of the tree so that the label of each node is less that that of its descendants. You should assume that for numbering a tree with N nodes you have the (1, 2, 3, N-1, N) labels available.

Input

The input file will contain several lines of input. Each line will contain two integers k and d. Here k is the branching factor of the complete k-arytree and d is the depth of the complete k-ary tree (k>0, d>0, k*d<=21).

Output

For each line of input, produce one line of output containing a round number, which is the number of ways the k-ary tree can be labeled, maintaining the constraints described above.

Sample Input:

2 2

10 1

Sample Output:

题意:k叉d层树最多组成几种搜索树。

思路:参考http://www.2cto.com/kf/201310/251470.html

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>

#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)

const int MAXSIZE = 10000;

struct bign {
	int s[MAXSIZE];
	bign ()	{memset(s, 0, sizeof(s));}
	bign (int number) {*this = number;}
	bign (const char* number) {*this = number;}

	void put();
	bign mul(int d);
	void del();
	void init() { memset(s, 0, sizeof(s)); }

	bign operator =  (char *num);
	bign operator =  (int num);

	bool operator <  (const bign& b) const;
	bool operator >  (const bign& b) const { return b < *this; }
	bool operator <= (const bign& b) const { return !(b < *this); }
	bool operator >= (const bign& b) const { return !(*this < b); }
	bool operator != (const bign& b) const { return b < *this || *this < b;}
	bool operator == (const bign& b) const { return !(b != *this); }

	bign operator + (const bign& c);
	bign operator * (const bign& c);
	bign operator - (const bign& c);
	int  operator / (const bign& c);
	bign operator / (int k);
	bign operator % (const bign &c);
	int  operator % (int k);
	void operator ++ ();
	bool operator -- ();
};

bign f[25][25];
int node[25][25];
int n, m;

bign c(int n, int m) {
    bign ans = 1;
    m = min(m, n - m);
    for (int i = 0; i < m; i ++) {
		bign save = (n - i);
		ans = ans * save / (i + 1);
	}
    return ans;
}

void init() {
	int i, j, k;
	for (i = 1; i <= 21; i ++) {
		f[i][0] = 1; node[i][0] = 1;
		for (j = 1; j <= 21 / i; j ++) {
			f[i][j] = 1;
			for (k = 0; k < i; k ++) {
				node[i][j] = node[i][j - 1] * i + 1;
				f[i][j] = f[i][j] * c(node[i][j] - 1 - k * node[i][j - 1], node[i][j - 1]) * f[i][j - 1];
			}
		}
	}
}

int main() {
    init();
    while (~scanf("%d%d", &n, &m)) {
		f[n][m].put();
		printf("\n");
    }
    return 0;
}

bign bign::operator = (char *num) {
	init();
	s[0] = strlen(num);
	for (int i = 1; i <= s[0]; i++)
		s[i] = num[s[0] - i] - '0';
	return *this;
}

bign bign::operator = (int num) {
	char str[MAXSIZE];
	sprintf(str, "%d", num);
	return *this = str;
}

bool bign::operator < (const bign& b) const {
	if (s[0] != b.s[0])
		return s[0] < b.s[0];
	for (int i = s[0]; i; i--)
		if (s[i] != b.s[i])
			return s[i] < b.s[i];
		return false;
}

bign bign::operator + (const bign& c) {
	int sum = 0;
	bign ans;
	ans.s[0] = max(s[0], c.s[0]);

	for (int i = 1; i <= ans.s[0]; i++) {
		if (i <= s[0]) sum += s[i];
		if (i <= c.s[0]) sum += c.s[i];
		ans.s[i] = sum % 10;
		sum /= 10;
	}
	return ans;
}

bign bign::operator * (const bign& c) {
	bign ans;
	ans.s[0] = 0; 

	for (int i = 1; i <= c.s[0]; i++){
		int g = 0;  

		for (int j = 1; j <= s[0]; j++){
			int x = s[j] * c.s[i] + g + ans.s[i + j - 1];
			ans.s[i + j - 1] = x % 10;
			g = x / 10;
		}
		int t = i + s[0] - 1;

		while (g){
			++t;
			g += ans.s[t];
			ans.s[t] = g % 10;
			g = g / 10;
		}  

		ans.s[0] = max(ans.s[0], t);
	}
	ans.del();
	return ans;
}

bign bign::operator - (const bign& c) {
	bign ans = *this;
	int i;
	for (i = 1; i <= c.s[0]; i++) {
		if (ans.s[i] < c.s[i]) {
			ans.s[i] += 10;
			ans.s[i + 1]--;;
		}
		ans.s[i] -= c.s[i];
	}

	for (i = 1; i <= ans.s[0]; i++) {
		if (ans.s[i] < 0) {
			ans.s[i] += 10;
			ans.s[i + 1]--;
		}
	}

	ans.del();
	return ans;
}

int bign::operator / (const bign& c) {
	int ans = 0;
	bign d = *this;
	while (d >= c) {
		d = d - c;
		ans++;
	}
	return ans;
}

bign bign::operator / (int k) {
	bign ans;
	ans.s[0] = s[0];
	int num = 0;
	for (int i = s[0]; i; i--) {
		num = num * 10 + s[i];
		ans.s[i] = num / k;
		num = num % k;
	}
	ans.del();
	return ans;
}

int bign:: operator % (int k){
	int sum = 0;
	for (int i = s[0]; i; i--){
		sum = sum * 10 + s[i];
		sum = sum % k;
	}
	return sum;
} 

bign bign::operator % (const bign &c) {
	bign now = *this;
	while (now >= c) {
		now = now - c;
		now.del();
	}
	return now;
}

void bign::operator ++ () {
	s[1]++;
	for (int i = 1; s[i] == 10; i++) {
		s[i] = 0;
		s[i + 1]++;
		s[0] = max(s[0], i + 1);
	}
}

bool bign::operator -- () {
	del();
	if (s[0] == 1 && s[1] == 0) return false;

	int i;
	for (i = 1; s[i] == 0; i++)
		s[i] = 9;
	s[i]--;
	del();
	return true;
}

void bign::put() {
	if (s[0] == 0)
		printf("0");
	else
		for (int i = s[0]; i; i--)
			printf("%d", s[i]);
}

bign bign::mul(int d) {
	s[0] += d;
	int i;
	for (i = s[0]; i > d; i--)
		s[i] = s[i - d];
	for (i = d; i; i--)
		s[i] = 0;
	return *this;
}

void bign::del() {
	while (s[s[0]] == 0) {
		s[0]--;
		if (s[0] == 0) break;
	}
}

10247 - Complete Tree Labeling(递推高精度)的更多相关文章

  1. PKU 2506 Tiling(递推+高精度||string应用)

    题目大意:原题链接有2×1和2×2两种规格的地板,现要拼2×n的形状,共有多少种情况,首先要做这道题目要先对递推有一定的了解.解题思路:1.假设我们已经铺好了2×(n-1)的情形,则要铺到2×n则只能 ...

  2. [BZOJ1089][SCOI2003]严格n元树(递推+高精度)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1089 分析: 第一感觉可以用一个通式求出来,但是考虑一下很麻烦,不好搞的.很容易发现最 ...

  3. 【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度

    1002: [FJOI2007]轮状病毒 Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同 ...

  4. 【BZOJ】1089: [SCOI2003]严格n元树(递推+高精度/fft)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1089 题意:求深度为d的n元树数目.(0<n<=32, 0<=d<=16) ...

  5. 递推+高精度 UVA 10497 Sweet Child Makes Trouble(可爱的孩子惹麻烦)

    题目链接 题意: n个物品全部乱序排列(都不在原来的位置)的方案数. 思路: dp[i]表示i个物品都乱序排序的方案数,所以状态转移方程.考虑i-1个物品乱序,放入第i个物品一定要和i-1个的其中一个 ...

  6. Uva10328 dp(递推+高精度)

    题目链接:http://vjudge.net/contest/136499#problem/F 题意:给你一个硬币,抛掷n次,问出现连续至少k个正面向上的情况有多少种. 一个比较好理解的题解:原题中问 ...

  7. HUD 5086 Revenge of Segment Tree(递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=5086 题目大意: 给定一个序列,求这个序列的子序列的和,再求所有子序列总和,这些子序列是连续的.去题目给的第二组 ...

  8. BZOJ 1089 严格n元树 (递推+高精度)

    题解:用a[i]表<=i时有几种树满足度数要求,那么这样就可以递归了,a[i]=a[i-1]^n+1.n个节点每个有a[i-1]种情况,那么将其相乘,最后加上1,因为深度为0也算一种.那么答案就 ...

  9. Buy the Ticket(卡特兰数+递推高精度)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...

随机推荐

  1. HTML5零基础学习Web前端需要知道哪些?

    HTML零基础学习Web前端网页制作,首先是要掌握一些常用标签的使用和他们的各个属性,常用的标签我总结了一下有以下这些: html:页面的根元素. head:页面的头部标签,是所有头部元素的容器. b ...

  2. QuickWebApi:使用Lambada方式,完成对WebApi的开发和调用。

    QuickWebApi 目的:使用Lambada方式,完成对WebApi的开发和调用. 缘由:为了解耦服务和展现,将越来越多的使用WebApi提供各种服务:随着服务的细化,WebApi的接口将越来越多 ...

  3. Keepalived + nginx实现高可用性和负载均衡

    在前面的一篇中讲到了Heartbeat作为高可用服务架构的解决方案,今天有试验了一种全新的解决方案,即采用Keepalived来实现这个功能. Keepalived 是一种高性能的服务器高可用或热备解 ...

  4. 【Android Studio安装部署系列】四十二、Android Studio使用Eclipse中的keystore为App签名

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 从eclipse迁移到AndroidStudio,要用原Eclipse的签名文件,这样才能保证转到AndroidStudio后更新的 ...

  5. HTTP中的重定向和请求转发的区别(转)

    一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下: request.getRequestDispatcher("new.jsp").forward(reques ...

  6. tplink路由器DMZ设置

    设置完成后,DMZ主机访问不了? 请排查以下方面: 1.确认服务器搭建成功,即内网可以正常访问: 2.确认在DMZ主机中填写的服务器IP地址正确: 3.宽带直接连接服务器并配置上网,确认外网可以正常访 ...

  7. 剑指Offer 50. 数组中重复的数字 (数组)

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  8. sort和uniq的应用实例

    sort 排序 uniq 1.语法:sort [option]... [file]... 2.选项:-k key,关键子,指定以那个列来排序.如果不指定,默认将正行作为关键字排序-n 对数值排序.默认 ...

  9. httpd.yml实例

    httpd.ymlapiVersion: apps/v1beta1kind: Deploymentmetadata: name: httpdspec: replicas: 4 template: me ...

  10. 用R的dgCMatrix包来构建稀疏矩阵 | sparse matrix by dgCMatrix

    sparse matrix是用来存储大型稀疏矩阵用得,单细胞表达数据基本都用这个格式来存储,因为单细胞很大部分都是0,用普通文本矩阵存储太占空间. 使用也是相当简单: library("Ma ...