Count the Trees[HDU1131]
Count the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1248 Accepted Submission(s): 812
Problem Description
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 exactly n 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 number n ( 1 ≤ n ≤ 100 ) 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
Source
UVA
Recommend
Eddy
If the N nodes are the same,there are h[N] different kinds of shapes.h[N] is the n-th Catalan Number.Now the N nodes are labled from 1 to N,so frac(N) should be multiplied.
#include<iostream>
using namespace std;
#ifndef HUGEINT
#define HUGEINT
#include<string>
using namespace std;
class hugeint
{
friend istream operator>> (istream&,hugeint&);
friend ostream operator<< (ostream&,hugeint&);
public:
hugeint()
{
len=0;
memset(num,0,sizeof(num));
}
hugeint(int x)
{
int p;
memset(num,0,sizeof(num));
p=x;
len=0;
while (p>0)
{
len++;
num[len]=p%10;
p/=10;
}
}
hugeint(string s)
{
int i;
len=s.size();
for (i=1;i<=len;i++)
num[i]=int(s[len-i])-48;
}
istream& operator >> (istream& is)
{
int i;
is>>s;
len=s.size();
for (i=1;i<=len;i++)
num[i]=int(s[len-i])-48;
return is;
}
ostream& operator << (ostream& os)
{
int i;
for (i=len;i>=1;i--)
cout<<num[i];
return os;
}
void clear()
{
int i;
for (i=1;i<=len;i++)
{
num[i+1]+=num[i]/10;
num[i]%=10;
}
while ((num[len]==0)&&(len>1)) len--;
}
int compare(hugeint t)
{
int i;
(*this).clear();
t.clear();
if (len>t.len) return 1;
if (len<t.len) return -1;
for (i=len;i>=1;i--)
{
if (num[i]>t.num[i]) return 1;
if (num[i]<t.num[i]) return -1;
}
return 0;
}
hugeint operator = (hugeint t)
{
int i;
len=t.len;
for (i=1;i<=len;i++)
num[i]=t.num[i];
return *this;
}
hugeint operator + (hugeint t)
{
int i;
if (t.len>len) len=t.len;
for (i=1;i<=t.len;i++) num[i]+=t.num[i];
len++;
(*this).clear();
return *this;
}
hugeint operator - (hugeint t)
{
hugeint temp;
int i;
if ((*this).compare(t)<0)
{
temp=t;
t=(*this);
}
else temp=(*this);
for (i=1;i<=temp.len;i++)
{
temp.num[i+1]--;
temp.num[i]+=(10-t.num[i]);
}
temp.clear();
return temp;
}
hugeint operator * (hugeint t)
{
hugeint temp;
int i,j;
for (i=1;i<=(*this).len;i++)
for (j=1;j<=t.len;j++)
temp.num[i+j-1]+=(*this).num[i]*t.num[j];
temp.len=(*this).len+t.len;
temp.clear();
return temp;
}
hugeint operator / (hugeint t)
{
hugeint c=0,d=0;
int i,j,p;
c.len=(*this).len; d.len=1;
for (j=(*this).len;j>=1;j--)
{
d.len++;
for (p=d.len;p>=2;p--)
d.num[p]=d.num[p-1];
d.num[1]=(*this).num[j];
while (d.compare(t)>=0)
{
c.num[j]++;
d=d-t;
}
}
c.clear();
d.clear();
return c;
}
hugeint operator % (hugeint t)
{
hugeint c,d;
int i,j,p;
for (i=1;i<=1000;i++) c.num[i]=0;
for (i=1;i<=1000;i++) d.num[i]=0;
c.len=len; d.len=1;
for (j=len;j>=1;j--)
{
d.len++;
for (p=d.len;p>=2;p--)
d.num[p]=d.num[p-1];
d.num[1]=num[j];
while (d.compare(t)>=0)
{
c.num[j]++;
d=d-t;
}
}
c.clear();
d.clear();
return d;
}
hugeint operator ++ ()
{
(*this)=(*this)+1;
return *this;
}
hugeint operator -- ()
{
(*this)=(*this)-1;
return *this;
}
hugeint operator += (hugeint t)
{
(*this)=(*this)+t;
return *this;
}
hugeint operator -= (hugeint t)
{
(*this)=(*this)-t;
return *this;
}
hugeint operator *= (hugeint t)
{
(*this)=(*this)*t;
return *this;
}
hugeint operator /= (hugeint t)
{
(*this)=(*this)/t;
return *this;
}
hugeint operator %= (hugeint t)
{
(*this)=(*this)%t;
return *this;
}
bool operator == (hugeint t)
{
int i;
if (len!=t.len) return false;
for (i=1;i<=len;i++)
if (num[i]!=t.num[i]) return false;
return true;
}
bool operator >= (hugeint t)
{
int x;
x=(*this).compare(t);
if (x>=0) return true;
return false;
}
bool operator <= (hugeint t)
{
int x;
x=(*this).compare(t);
if (x<=0) return true;
return false;
}
bool operator > (hugeint t)
{
int x;
x=(*this).compare(t);
if (x>0) return true;
return false;
}
bool operator < (hugeint t)
{
int x;
x=(*this).compare(t);
if (x<0) return true;
return false;
}
~hugeint() {}
private:
int num[1001];
int len;
string s;
};
#endif
hugeint h[125];
hugeint frac[125];
int i,N;
int main()
{
frac[0]=1;
h[0]=1;
for (i=1;i<=100;i++) frac[i]=frac[i-1]*i;
for (i=1;i<=100;i++)
{
h[i]=h[i-1]*(4*i-2);
hugeint tmp=i+1;
h[i]=h[i]/tmp;
}
while (scanf("%d",&N)!=EOF)
{
if (N==0) return 0;
hugeint ans=frac[N]*h[N];
ans<<cout;
cout<<endl;
}
return 0;
}
Count the Trees[HDU1131]的更多相关文章
- zjuoj 3602 Count the Trees
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...
- Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)
Count the Trees Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...
- TZOJ 4292 Count the Trees(树hash)
描述 A binary tree is a tree data structure in which each node has at most two child nodes, usually di ...
- HDU 1131 Count the Trees 大数计算
题目是说给出一个数字,然后以1到这个数为序号当做二叉树的结点,问总共有几种组成二叉树的方式.这个题就是用卡特兰数算出个数,然后因为有编号,不同的编号对应不同的方式,所以结果是卡特兰数乘这个数的阶乘种方 ...
- 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 ...
- HDU 1131 Count the Trees
卡特兰数再乘上n的阶乘 #include<iostream> #include<cstdio> using namespace std; #define base 10000 ...
- ZOJ3602:Count the Trees
我是在neuqvj上交的这题:http://vj.acmclub.cn/problem/viewProblem.action?id=17848 本来是挺容易的树同构题,可是节点数比较多,愣是把普通ha ...
- 2012-2014 三年浙江 acm 省赛 题目 分类
The 9th Zhejiang Provincial Collegiate Programming Contest A Taxi Fare 25.57% (166/649) (水 ...
随机推荐
- [Effective JavaScript 笔记] 第5条:避免对混合类型使用==运算符
“1.0e0”=={valueOf:function(){return true;}} 是值是多少? 这两个完全不同的值使用==运算符是相等的.为什么呢?请看<[Effective JavaSc ...
- unity3d进行脚本资源打包加载
原地址:http://www.cnblogs.com/hisiqi/p/3204752.html 本文记录如何通过unity3d进行脚本资源打包加载 1.创建TestDll.cs文件 public c ...
- HDU 1203 I NEED A OFFER (01背包&&概率dp)
M - I NEED A OFFER! Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 【SpringMVC】SpringMVC系列15之SpringMVC最佳实践
15.SpringMVC最佳实践 15.1.遵循Restful API最佳实践 参考:http://segmentfault.com/a/1190000002949234 15.2.统一返回字段 15 ...
- JavaScript常用事件
一般事件 事件 浏览器支持 描述 onClick HTML: 2 | 3 | 3.2 | 4 Browser: IE3 | N2 | O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 on ...
- WriteFile实现下载
TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { /* ...
- 【转】windows下安装和调用curl的方法
本文转自:http://1316478764.iteye.com/blog/2100778 curl是利用URL语法在命令行方式下工作的开源文件传输工具.它支持很多协议:FTP, FTPS, HTTP ...
- 【JAVA、C++】LeetCode 012 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- UITableView:下拉刷新和上拉加载更多
[转载请注明出处] 本文将说明让UIScrollView支持"下拉刷新"和"上拉加载更多"的实现机制,并实现一个可用的tableView子类,以下主要以&quo ...
- iOS设备通知中心精品推荐消息删除
如要彻底解决这个问题可以通过手机连接PC端91助手—功能大全—文件管理,进入/Library/MobileSubstrate/DynamicLibraries 文件夹,把AppSafety开头的文件都 ...