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 ...
随机推荐
- 转载 LayoutInflater的inflate函数用法详解
http://www.open-open.com/lib/view/open1328837587484.html LayoutInflater的inflate函数用法详解 LayoutInflater ...
- FINDPEAKS - matlab函数
FINDPEAKS Find local peaks in data PKS = FINDPEAKS(X) finds local peaks in the data vector X. A loca ...
- iOS实践01
去年放假之前大概完成了新浪微博项目,到现在也忘得差不多了,打算在重新写一遍.之前的一些笔记在新浪的博客SleenXiu,在这主要是把新浪微博以随笔的形式写在这,方便以后的复习. 先看看之前主要完成的几 ...
- .Net平台-MVP模式再探(二)
PS: 本文与 上一遍文章 没有什么必然的联系,可以说是对于MVP的一定的加深,或许在理解上比上一篇多有点难度. 正文 一.简单讲讲MVP是什么玩意儿 如果从层次关系来讲,MVP属于P ...
- 使用astyle格式化代码【脚本】
astyle使用基础教程 http://cppblog.com/jokes000/articles/158838.html steps: (1) apt-get install astyle 或者去主 ...
- Problem F: Exponentiation大数求幂
DescriptionProblems involving the computation of exact values of very large magnitude and precision ...
- Notes常用事件整理
① ボタンのクリック事件: Sub Click(Source As Button) Dim ws As New NotesUIWorkspace Dim uidoc As NotesUIDo ...
- Netty4.0学习教程
http://blog.csdn.net/u013252773/article/details/21046697 一些属性和方法介绍 http://blog.csdn.net/zxhoo/articl ...
- java附件上传下载大字段版
public int up2(Map map) { StringBuffer insertSQL = new StringBuffer(); insertSQL.append("insert ...
- 再探Delphi2010 Class的构造和析构顺序
发了上一篇博客.盒子上有朋友认为Class的构造和析构延迟加载.是在Unit的初始化后调用的Class的构造.在Unit的反初始化前调用的Class的析构函数. 为了证明一下我又做了个试验 unit ...