hdu 1294 rooted trees problem
来源:互联网 发布:台湾图解gv新域名 编辑:程序博客网 时间:2024/05/17 23:33
Rooted Trees Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 337 Accepted Submission(s): 108
Problem Description
Give you two definitions tree and rooted tree. An undirected connected graph without cycles is called a tree. A tree is called rooted if it has a distinguished vertex r called the root. Your task is to make a program to calculate the number of rooted trees with n vertices denoted as Tn. The case n=5 is shown in Fig. 1.
Input
There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer means n(1<=n<=40) .
Output
For each test case, there is only one integer means Tn.
Sample Input
125
Sample Output
119
Author
SmallBeer(CML)
Source
杭电ACM集训队训练赛(VIII)
Recommend
lcy
分情况,分为有 1, 2, 3, ..., n-1 个子树的情况,然后相加。
#include <cstdio>#include <cstdlib>#include <cassert>#include <cstring>#include <cmath>#include <vector>#include <algorithm>using namespace std;long long GCD (long long a, long long b) { return b ? GCD (b, a % b) : a;}long long C (long long n, long long m) { long long num = 1; // numerator long long don = 1; // doneminator if (n - m < m) { m = n - m; } long long gcd; while (m) { gcd = GCD (num, don); num /= gcd; don /= gcd; num *= n--; don *= m--; } assert (num % don == 0); return num / don;}int a [10000][50]; // once this array is accessed violation, variable sz's value may be modified!!!int sz;void Devide (int last, int len, int rest, int ind) {#ifdef _DEBUG //printf ("Devide (last = %d, len = %d, rest = %d, ind = %d)\n", last, len, rest, ind);#endif if (ind == len) { assert (sz + 1 < 10000); // this assertion can catch the bug. assert (rest == 0); for (int i=0; i<len; ++i) { a [sz + 1][i] = a [sz][i]; } ++sz; return; } assert (sz < 100000); // this assertion cannot catch the bug, because ...#ifdef _DEBUG int t = lrint (ceil (1.0 * rest / (len - ind))); assert (t * (len - ind) >= rest); assert ((t - 1) * (len - ind) < rest);#endif for (int i=lrint(ceil(1.0*rest/(len-ind))); i<=last && len-ind-1<=rest-i; ++i) { a [sz][ind] = i; Devide (i, len, rest-i, ind+1); }}void Make (int n, int len) {#ifdef _DEBUG //printf ("Make (n = %d, len = %d)\n", n, len);#endif sz = 0;#ifdef _DEBUG int t = lrint (ceil (1.0 * n / len)); assert (t * len >= n); assert ((t - 1) * len < n);#endif for (int i=lrint(ceil(1.0*n/len)); i<=n-len+1; ++i) { a [sz][0] = i; Devide (i, len, n-i, 1); }#ifdef _DEBUG for (int i=0; i<sz; ++i) { for (int k=0; k<len; ++k) { printf ("%d%s", a [i][k], k==len-1 ? "\n" : " "); } }#endif}long long dp [50];long long DP (long long n) { // it's no longer a dp!!! what it is named is just for historic reason.#ifdef _DEBUG //printf ("DP (n = %lld)\n", n);#endif for (int i=1; i<n; ++i) { sz = 0; Make (n - 1, i); for (int s=0; s<sz; ++s) { long long t = 1; int k = 0, m = 0; while (k < i && m < i) { m = k; while (++m < i && a [s][m] == a [s][k]) {} t *= C (dp [a [s][k]] + m - k - 1, m - k); k = m; } dp [n] += t;#ifdef _DEBUG printf ("dp [%lld] = %lld\n", n, dp [n]);#endif } } return dp [n];}void MakeDP () { memset (dp, 0, sizeof (dp)); dp [1] = dp [2] = 1; for (int i=3; i<=40; ++i) { DP (i); }}int main () { MakeDP (); int n; while (scanf ("%d", &n) == 1) { printf ("%lld\n", dp [n]); } return 0;}
- hdu 1294 rooted trees problem
- hdu 1294 Rooted Trees Problem 组合数+整数划分
- hdu 1234 Rooted Trees Problem 有重复的组合+乘法原理
- hdu1294 Rooted Trees Problem(重复组合+整数拆分+DFS)
- Aizu ALDS1_7_A Rooted Trees
- UVa Problem 115 - Climbing Trees
- UVa Problem 115 - Climbing Trees
- HDU 2841 Visible Trees
- HDU 1633 ( Orchard Trees )
- hdu 3015 Disharmony Trees
- HDU 2841 Visible Trees
- hdu 3015 Disharmony Trees
- HDU 3015 Disharmony Trees
- HDU - 3015 Disharmony Trees
- HDU P5200 Trees
- HDU 5200 Trees
- hdu 5200 Trees
- HDU 2841 Visible Trees
- chp6_MapReduce工作机制_20111016
- RHEL5.6下安装RAC11.2.0.3
- Linux 终端中常用的快捷键
- 判断一个序列是否为另一个序列的子序列(两种算法)
- 将信息放入xml中
- hdu 1294 rooted trees problem
- zarafa-webaccess右下角加入更改密码的链接
- Android游戏开发菜鸟之路(一)
- wince对象存储(Object Store)
- innotop 的安装和使用
- hdmi简介
- Linux操作系统基础知识之八:文件系统
- 动态规划的状态表示分析
- 招商银行信用卡使用说明