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 (卡特兰数)的更多相关文章

  1. HDU 1131 Count the Trees 大数计算

    题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方 ...

  2. HDU 1131 Count the Trees

    卡特兰数再乘上n的阶乘 #include<iostream> #include<cstdio> using namespace std; #define base 10000 ...

  3. hdu 1130How Many Trees?(卡特兰数)

    卡特兰数又称卡塔兰数,英文名Catalan number,是组合数学中一个常出现在各种计数问题中出现的数列. 以比利时的数学家欧仁·查理·卡塔兰 (1814–1894)的名字来命名,其前几项为(从第零 ...

  4. uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

    option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...

  5. HDU 1023 Train Problem II (卡特兰数,经典)

    题意: 给出一个数字n,假设火车从1~n的顺序分别进站,求有多少种出站序列. 思路: 卡特兰数的经典例子.n<101,用递推式解决.需要使用到大数.n=100时大概有200位以下. #inclu ...

  6. 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 ...

  7. 【HDU 5184】 Brackets (卡特兰数)

    Brackets Problem Description We give the following inductive definition of a “regular brackets” sequ ...

  8. UVa 10007 - Count the Trees(卡特兰数+阶乘+大数)

    题目链接:UVa 10007 题意:统计n个节点的二叉树的个数 1个节点形成的二叉树的形状个数为:1 2个节点形成的二叉树的形状个数为:2 3个节点形成的二叉树的形状个数为:5 4个节点形成的二叉树的 ...

  9. uva 10007 Count the Trees

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

随机推荐

  1. 下拉刷新控件(4)SwipeRefreshLayout官方教程(上)如何在应用中使用它

    http://developer.android.com/training/swipe/add-swipe-interface.html 1,在布局xml和代码中使用它 2,在menu中添加它 The ...

  2. BZOJ 2743 采花(树状数组)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2743 题意:给出一个数列,每个询问查询[L,R]中至少出现两次的数字有多少种? 思路:(1 ...

  3. JavaScript 高级篇之闭包、模拟类,继承(五)

    本篇主要分享我对闭包的理解及使用闭包完成私有属性.模拟类.继承等,结合大量例子,希望大家能快速掌握!首先让我们先从一些基本的术语开始吧     一.javascript中的闭包 1.我们一起先来理解什 ...

  4. SQL计算实际工作日(天)及两个时间(工作日)间隔(小时)!

    Code highlighting produced by Actipro CodeHighlighter (freeware)-->去掉法定节假日(周六,周天)和指定节假日 USE [DBNa ...

  5. XManager介绍、安装、使用

    简介 Xmanager是一款小巧.便捷的浏览远端X窗口系统的工具.在工作中经常使用Xmanager来登录远端的Linux系统,在X窗口系统上作图形化的操作.Xmanager可以将PC变成X Windo ...

  6. Repeater上下排序按钮

    aspx代码 <table cellspacing="0" cellpadding="0" width="100%" align=&q ...

  7. (转载)目前最细致清晰的NSDictionary以及NSMutableDictionary用法总结

    文章转载自:http://www.cnblogs.com/wengzilin/archive/2012/03/15/2397712.html 做过Java语言 或者 C语言 开发的朋友应该很清楚 关键 ...

  8. 使用Phalcon开发工具碰到的数据库问题"Table 'XXX' doesn't exist in database when dumping meta-data for XXX"

    使用Phalcon开发工具,通过命令行生成程序框架 设置好config.php,在对数据库进行读取.保存数据的时候出现了问题“Table 'XXX' doesn't exist in database ...

  9. phonegap配置启动动画

    以下有2种方式 1 主Active中 onCreate函数里添加代码 2 config.xml文件进行配置(对通过命令行模式下cordova命令行生成的可行) 确保自己安装了SplashScreen插 ...

  10. 《Write Optimized B-Trees》读书报告

    论文原作者:Goetz Graefe, Microsoft.我读完这篇论文后颇有收获,所以写了一篇论文报告,旨在更精炼准确地阐述论文核心思想. 摘要:论文提出了一种方法,这种方法可以优化B树索引写性能 ...