Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)
Count the Trees |
Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewhat common among programmers. It can be described as the temporary (although frequent) loss of the faculty of speech when the whole power of the brain is applied to something extremely interesting or challenging.
Juan is a very gifted programmer, and has a severe case of ACM (he even participated in an ACM world championship a few months ago). Lately, his loved ones are worried about him, because he has found a new exciting problem to exercise his intellectual powers, and he has been speechless for several weeks now. The problem is the determination of the number of different labeled binary trees that can be built using exactlyn different elements.
For example, given one element A, just one binary tree can be formed (using A as the root of the tree). With two elements, A and B, four different binary trees can be created, as shown in the figure.

If you are able to provide a solution for this problem, Juan will be able to talk again, and his friends and family will be forever grateful.
Input
The input will consist of several input cases, one per line. Each input case will be specified by the numbern ( 1 ≤ n ≤ 300 ) of different elements that must be used to form the trees. A number 0 will mark the end of input and is not to be processed.
Output
For each input case print the number of binary trees that can be built using the n elements, followed by a newline character.
Sample Input
- 1
- 2
- 10
- 25
- 0
Sample Output
- 1
- 4
- 60949324800
- 75414671852339208296275849248768000000
Miguel Revilla
2000-08-21
解题思路:result(n) = Canlatan(n)*n!
这题完全是水过去的,给你的测试数据前两个还是可以算出来,但第三个数据以后就很大了,因为是Cantalan的一个系列,所以我试出了10的sampleOutput刚好是:10的阶乘*Cantalan数,然后计算了25的输出,发现也是如此,在原有的代码上稍加改进在杭电过了之后交到了Uva,Uva的数据比较大 n<=300
- #include<cstdio>
- #include<cstring>
- #define SIZE 200
- #define MAXN 302
- #define BASE 10000
- using namespace std;
- int Canlan[MAXN][SIZE];
- int temp[SIZE];
- void A(int n)
- {
- memcpy(temp, Canlan[n], sizeof(temp));
- int e = ;
- for(int fac = n; fac>; --fac)
- {
- for(int i = SIZE-, remain = ; i>=; --i)
- {
- e = temp[i] * fac + remain;
- temp[i] = e % BASE;
- remain = e / BASE;
- }
- }
- memcpy(Canlan[n], temp, sizeof(int)*SIZE);
- }
- void multiply(int elem)
- {
- for(int i=SIZE-, remain = ; i >= ; --i)
- {
- remain = temp[i] * elem + remain;
- temp[i] = remain % BASE;
- remain = remain / BASE;
- }
- return;
- }
- void divide(int elem)
- {
- int i;
- for(i=; i<SIZE && temp[i] == ; i++);
- for(int remain = ; i < SIZE; ++i)
- {
- remain += temp[i];
- temp[i] = remain / elem;
- remain = (remain % elem) * BASE;
- }
- return;
- }
- void Cantalan()
- {
- memset(Canlan[], , sizeof(int)*SIZE);
- Canlan[][SIZE-] = ;
- for(int i=; i<MAXN; ++i)
- {
- memcpy(temp, Canlan[i-], sizeof(int)*SIZE);
- multiply(*i-);
- divide(i+);
- memcpy(Canlan[i], temp, sizeof(int)*SIZE);
- }
- return;
- }
- int main()
- {
- int n;
- Cantalan();
- for(int i=; i<MAXN; ++i) A(i);
- while(scanf("%d", &n) != EOF && n)
- {
- int i;
- for(i=; i<SIZE && Canlan[n][i] == ; i++);
- printf("%d", Canlan[n][i]);
- if(i+ == SIZE)
- {
- printf("\n");
- continue;
- }
- for(i=i+; i<SIZE; ++i)
- printf("%04d", Canlan[n][i]);
- printf("\n");
- }
- return ;
- }
Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)的更多相关文章
- HDU 1131 Count the Trees 大数计算
题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方 ...
- HDU 1131 Count the Trees
卡特兰数再乘上n的阶乘 #include<iostream> #include<cstdio> using namespace std; #define base 10000 ...
- hdu 1130How Many Trees?(卡特兰数)
卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列. 以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)的名字来命名,其前几项为(从第零 ...
- uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)
option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...
- HDU 1023 Train Problem II (卡特兰数,经典)
题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #inclu ...
- HDU 1133 Buy the Ticket 卡特兰数
设50元的人为+1 100元的人为-1 满足前随意k个人的和大于等于0 卡特兰数 C(n+m, m)-C(n+m, m+1)*n!*m! import java.math.*; import java ...
- 【HDU 5184】 Brackets (卡特兰数)
Brackets Problem Description We give the following inductive definition of a “regular brackets” sequ ...
- UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)
题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的 ...
- uva 10007 Count the Trees
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
随机推荐
- poj-3616 Milking Time (区间dp)
http://poj.org/problem?id=3616 bessie是一头工作很努力的奶牛,她很关心自己的产奶量,所以在她安排接下来的n个小时以尽可能提高自己的产奶量. 现在有m个产奶时间,每个 ...
- 【HDOJ】1362 The Bermuda Triangle
1. 题目描述给定几个三角形拼成一个百慕大三角形. 2. 基本思路基本思路肯定是搜索,关键点是剪枝.(1) 若存在长度为$l$的边,则一定可以拼成长度为$k \cdot l$的三角形,则可拼成长度为$ ...
- 《OD大数据实战》HDFS入门实例
一.环境搭建 1. 下载安装配置 <OD大数据实战>Hadoop伪分布式环境搭建 2. Hadoop配置信息 1)${HADOOP_HOME}/libexec:存储hadoop的默认环境 ...
- MFC弹出菜单隐藏解决
http://social.msdn.microsoft.com/Forums/en-US/5482103e-272b-4c9f-bac4-be15f14782bd/cmfcmenubar-remov ...
- create-maximum-number(难)
https://leetcode.com/problems/create-maximum-number/ 这道题目太难了,花了我很多时间.最后还是参考了别人的方法.还少加了个greater方法.很难. ...
- iOS9 class dump header
获取系统私有API,网上有很多资料总结了一下就三种方式: 使用class-dump可以提取系统私有API列表 使用class-dump+DumpFrameworks.pl,这个可以一次性提取所有系统F ...
- 基于百度地图js进行地理定位
http://www.mengxiangchaoren.com/jquery.select.position.min.js 使用方法 $("#myCity").renderSele ...
- /bin/bash: [xxxx]: command not found
/******************************************************************************** * /bin/bash: [xxxx ...
- 20160127.CCPP体系详解(0006天)
程序片段(01):msg.c 内容概要:线程概念 #include <stdio.h> #include <stdlib.h> #include <Windows.h&g ...
- .htaccess 文件的使用
用于服务器对文件夹的控制 官方解释为 :分布式配置文件 ,提供了针对目录改变配置的方法; 项目可以有多个这样的配置文件,子目录文件会覆盖父目录的配置 在apache(这里泛指服务器)中,/conf/v ...