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 ...
随机推荐
- 关于group by的一段SQl语句——Oracle
select cc.fformulacode, cc.rangedate, dd.fpervalue from (select n1.fformulacode, max(n1.frangedate) ...
- jsp建立错误页自动跳转
在各个常用的web站点中,经常会发现这样一个功能:当一个页面出错后,会自动跳转到一个页面上进行错误信息的提示. 想要完成错误页的操作,则一定要满足两个条件: 1.指定错误出现时的跳转页,通过error ...
- Android 中Java和JavaScript交互入门
如何实现JavaScript 和java 交互 实现Java和js交互十分便捷.通常只需要以下几步. WebView开启JavaScript脚本执行 WebView设置供JavaScript调用的交互 ...
- HTML5 调用手机相册和摄像头的方法并上传微信下测试通过
<input type="file" capture="camera" accept="image/*" id="camer ...
- poj 1966 Cable TV Network 顶点连通度
题目链接 给一个图, n个点m条边, 求至少去掉多少个点可以使得图不再联通.随便指定一个点为源点, 枚举其他点为汇点的情况, 跑网络流, 求其中最小的情况. 如果最后ans为inf, 说明是一个完全图 ...
- python网络编程-01
python网络编程 1.socket模块介绍 ①在网络编程中的一个基本组件就是套接字(socket),socket是两个程序之间的“信息通道”. ②套接字包括两个部分:服务器套接字.客户机套接字 ③ ...
- 如何取消一个本地svn目录与svn的联系(即恢复原有图标等)
在使用svn 的时候容易手抖错选update地址,使其目录所有同级文件夹上出现蓝色“?”图样,非常烦人,下面记录一下解决方案. 首先在该目录下打开同级文件件,工具→文件夹选项→查看→隐藏文件和文件夹→ ...
- mysql数据库字段区分大小写的设置方法
alter table t_25_thread_info modify column thread_id varchar(8) character set utf8 collate utf8_bin;
- java类的封装、继承、多态
一.封装(encapsulation) 封装性就是把类(对象)的属性和行为结合成一个独立的相同单位,并尽可能隐蔽类(对象)的内部细节,对外形成一个边界,只保留有限的对外接口使之与外部发生联系.封装的特 ...
- 一些tcp通讯代码
1,nginx-lua 需要设置nginx配置文件 resolver 223.5.5.5 223.6.6.6; lua_package_path "/usr/local/nginx/conf ...