10247 - Complete Tree Labeling(递推高精度)
来源:互联网 发布:淘宝上怎么找卖片的 编辑:程序博客网 时间:2024/06/05 18:54
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:
80
3628800
题意: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");elsefor (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(递推高精度)
- UVa 10247 Complete Tree Labeling (组合数学&高精度)
- uva 10247 - Complete Tree Labeling(dp)
- uva 10247 complete tree labeling
- uva 10247 - Complete Tree Labeling
- UVa Problem 10247 Complete Tree Labeling (完全树标号)
- UVa Problem Solution: 10247 - Complete Tree Labeling
- uva10247 - Complete Tree Labeling
- poj2506(递推+高精度)
- hdu1919(递推+高精度+HashMap)
- POJ 2506 Tiling(高精度+递推)
- POJ 2506 Tiling (递推+高精度)
- 【POJ】2506 - Tiling(递推,高精度)
- poj2506 高精度递推
- POJ2506(递推+高精度)
- POJ2506 高精度+递推
- HDU2506 Tiling (高精度+递推)
- POJ2506 Tiling【高精度】【递推】
- openSSL使用手册
- JBMP中文乱码问题的解决方案
- 解决linux安装JDK时Error occurred during initialization of VM
- GNU汇编器 编写汇编函数 [李园7舍_404]
- Android提供的获取流量的方法
- 10247 - Complete Tree Labeling(递推高精度)
- java调用外部脚本或者程序,看了别人的代码,算是转载吧。
- C#学习(messageBox,pictureBox,窗口切换)
- HashMap的工作原理
- 提高android应用的效率--主要讲解listview的优化
- Xcode view button 链接到 事件
- 【神のDebug】C++作业,10小时debug的结果
- Js控制录入域为浮点数
- UIImage 简介