HDU 1130 How Many Trees?
裸的卡特兰数
C++
#include<iostream>
#include<cstdio>
using namespace std;
#define base 10000
#define len 100
void multiply(int a[],int max,int b)
{
int i,array=;
for(i=max-;i>=;i--)
{
array+=b*a[i];
a[i]=array%base;
array/=base; }
} void divide(int a[],int max,int b)
{
int i,div=;
for(i=;i<max;i++)
{
div=div*base+a[i];
a[i]=div/b;
div%=b;
}
} int main()
{
int n,i;
int a[][len];
memset(a[],,len*sizeof(int));
for(i=,a[][len-]=;i<;i++)
{
memcpy(a[i],a[i-],len*sizeof(int));
multiply(a[i],len,*i-);
divide(a[i],len,i+);
}
while(scanf("%d",&n)!=EOF)
{
for(i=;i<len&&a[n][i]==;i++);
printf("%d",a[n][i++]);
for(;i<len;i++)
{
printf("%04d",a[n][i]);
}
printf("\n");
}
return ;
}
pascal
const
modnum=;
type
arraytype=array[..] of longint;
var
s:string;
i,j,l,m,n,le:longint;
h:arraytype;
procedure mul(var h:arraytype; k:longint);
var
j:longint;
begin
for j:= to le do h[j]:=h[j]*k;
for j:= to le- do
begin
h[j+]:=h[j+]+h[j] div modnum;
h[j]:=h[j] mod modnum;
end;
while h[le]>modnum do begin
inc(le);
h[le]:=h[le-] div modnum;
h[le-]:=h[le-] mod modnum;
end;
end;
procedure devide(var h:arraytype; k:longint);
var
d,o,r,i:longint;
begin
r:=;
for i:=le downto do
begin
d:=modnum*r+h[i];
h[i]:=d div k;
r:=d mod k;
end;
end;
begin
readln(n);
h[]:=;
for i:= to n do
begin
mul(h,i+n); devide(h,i);
end;
devide(h,n+);
while h[le]= do dec(le);
write(h[le]);
for i:=le- downto do
begin
str(h[i],s);
while length(s)< do s:=''+s;
write(s);
end;
end.
HDU 1130 How Many Trees?的更多相关文章
- HDU——1130 How Many Trees?
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1130 How Many Trees?(Catalan数)
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu 1130 How Many Trees? 【卡特兰数】
题目 题意:给你一个数字n,问你将1~n这n个数字,可以组成多少棵不同的二叉搜索树. 1,2,5,14--根据输出中的规律可以看出这是一个卡特兰数的序列.于是代用卡特兰数中的一个递推式: 因为输入可取 ...
- HDU ACM 1134 Game of Connections / 1130 How Many Trees?(卡特兰数)
[题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1134 [解题背景]这题不会做,自己推公式推了一段时间,将n=3和n=4的情况列出来了,只发现第n项与 ...
- hdu 1693 Eat the Trees——插头DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...
- hdu 1130,hdu 1131(卡特兰数,大数)
How Many Trees? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 1392 Surround the Trees(凸包入门)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- HDU 1392 Surround the Trees (凸包周长)
题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...
随机推荐
- Javascript知识四(DOM)
[箴 10:4] 手懒的,要受贫穷:手勤的,却要富足. He becometh poor that dealeth with a slack hand: but the hand of the di ...
- jquery + ajax调用后台方法
前台js: var parameter = ""; $.ajax({ type: "POST", //提交方式 url: "Default.aspx/ ...
- oracle中 some、any、all 三者的区别及个人总结,归纳!
any :如果是大于号时( a > b ),那么a中所有大于b中最小的那个数的值,就会被输出出来. 如果是小于号时( a < b ),那么a中所有小于b中最大的那个数的值,就会被输出出来. ...
- BZOJ 1509: [NOI2003]逃学的小孩( 树形dp )
树形dp求出某个点的最长3条链a,b,c(a>=b>=c), 然后以这个点为交点的最优解一定是a+2b+c.好像还有一种做法是求出树的直径然后乱搞... ----------------- ...
- Cglib学习站点(转)
1.CGlib简单介绍,访问地址:http://www.blogjava.net/stone2083/archive/2008/03/16 /186615.html,从简单的示例到不同业务场景的变化, ...
- .Net 类型、对象、线程栈、托管堆运行时的相互关系
JIT(just in time)编译器 接下来的会讲到方法的调用,这里先讲下JIT编译器.以CLR书中的代码为例(手打...).以Main方法为例: static void Main(){ Cons ...
- java.el.PropertyNotFoundException解决方法
今天在开发中遇到了java.el.PropertyNotFoundException异常,检查JSP页面.Action.Bean.都没有发现错误 在网上搜了一下可能是我的bean不是一个标准的bean ...
- firebug如何使用
1.怎么安装firebug: a.打开火狐浏览器--------b.点击火狐浏览器的右上角这个小图标-------c.点击<获取附件组件>,在右上角的搜索框()内,输入firebug,点击 ...
- 一个失败的操作系统MULTICS
Unix的诞生和Multics(Multiplexed Information and Computing System)是有一定渊源的.当时开发者Brian Kernighan开玩笑地戏称这个不完善 ...
- 用Java实现 ,冒泡排序与普通排序的区别
冒泡排序与普通排序的区别 /** *个人网址: http://www.lipengfei2013.tk * 功能:冒泡排序与普通排序的区别 */ package www.csdn ...