题目链接

中间计算的各种细节。有的细节没处理好,就wa了。。。主要思路就是根据卡特兰数列的:

h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0) (n>=2)

 #include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL __int64
LL ctl[];
LL sum[];
struct node
{
int l,r;
}tree[];
int t;
void build(LL n,int rt)
{
int i,pos;
if(n == ||n == )
{
return ;
}
for(i = ;i <= ;i ++)
{
if(sum[i] >= n)
break;
}
pos = i;
n -= sum[pos-];
for(i = ;i <= pos-;i ++)
{
if(n <= ctl[i]*ctl[pos--i])
break;
n -= ctl[i]*ctl[pos--i];
}
if(i != )
{
tree[rt].l = t ++;
if(n%ctl[pos--i] != )
build(sum[i-]+n/ctl[pos--i]+,t-);
else
build(sum[i-]+n/ctl[pos--i],t-);
}
if(i != pos-)
{
tree[rt].r = t ++;
if(n%ctl[pos--i] == )
build(sum[pos--i]+ctl[pos--i],t-);
else
build(sum[pos--i]+n%ctl[pos--i],t-);
}
}
void show(int x)
{
if(x == -)
return ;
if(tree[x].l != -)
printf("(");
show(tree[x].l);
if(tree[x].l != -)
printf(")");
printf("X");
if(tree[x].r != -)
printf("(");
show(tree[x].r);
if(tree[x].r != -)
printf(")"); }
int main()
{
int i;
LL n;
ctl[] = ;
ctl[] = ;
sum[] = ;
for(i = ;i <= ;i ++)
{
ctl[i] = ctl[i-]*(*i-)/(i+);
sum[i] = sum[i-] + ctl[i];
}
while(cin>>n)
{
if(n == ) break;
t = ;
for(i = ;i <= ;i ++)
tree[i].l = tree[i].r = -;
build(n,);
show();
printf("\n");
}
return ;
}

POJ 1095 Trees Made to Order(卡特兰数列)的更多相关文章

  1. poj 1095 Trees Made to Order 卡特兰数

    这题用到了卡特兰数,详情见:http://www.cnblogs.com/jackge/archive/2013/05/19/3086519.html 解体思路详见:http://blog.csdn. ...

  2. POJ 1095 Trees Made to Order 最详细的解题报告

    题目来源:Trees Made to Order 题目大意:根据下面的规则给一棵二叉树编号: 规则1:如果二叉树为空,则编号为0: 规则2:如果二叉树只有一个节点,则编号为1: 规则3:所有含有m个节 ...

  3. poj 1095 Trees Made to Order

    http://poj.org/problem?id=1095 先求出n个节点数的二叉树的形态有多少种.卡特兰数f[n]=f[n-1]*(4*n-2)/(n+1);再递归求. #include < ...

  4. Trees Made to Order——Catalan数和递归

    Trees Made to Order Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7155   Accepted: 40 ...

  5. 「BZOJ1485」[HNOI2009] 有趣的数列 (卡特兰数列)

    「BZOJ1485」[HNOI2009] 有趣的数列   Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai ...

  6. poj 1095 题解(卡特兰数+递归

    题目 题意:给出一个二叉树的编号,问形态. 编号依据 1:如果二叉树为空,则编号为0: 2:如果二叉树只有一个节点,则编号为1: 3:所有含有m个节点的二叉树的编号小于所有含有m+1个节点的二叉树的编 ...

  7. HDU.P1100 Trees Made to Order 解题报告

    http://www.cnblogs.com/keam37/p/3637717.html  keam所有 转载请注明出处 Problem Description We can number binar ...

  8. POJ 2084 Game of Connections(卡特兰数)

    卡特兰数源于组合数学,ACM中比较具体的使用例子有,1括号匹配的种数.2在栈中的自然数出栈的种数.3求多边形内三角形的个数.4,n个数围城圆圈,找不相交线段的个数.5给定n个数,求组成二叉树的种数…… ...

  9. POJ 1095

    #include <iostream> #define MAXN 20 using namespace std; __int64 cat[MAXN]; int sum; void give ...

随机推荐

  1. 坚持不懈之linux haproxy 配置文件 详情

    ####################全局配置信息######################## #######参数是进程级的,通常和操作系统(OS)相关######### global maxc ...

  2. 【Docker】来自官方映像的 6 个 Dockerfile 技巧

    本文将根据我从官方镜像学到的经验,讲解编写Dockerfile的技巧.   1. 选择Debian  官方镜像的大多数Dockerfile,不管是直接还是通过其他镜像,都是基于Debian的.Dock ...

  3. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  4. hdu3555

    基本的数位dp #include <cstdio> #include <cstring> using namespace std; #define D(x) x ; long ...

  5. ini 文件操作记要(1): 使用 TIniFile

    ini 文件操作记要(1): 使用 TIniFile unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Class ...

  6. codeforces B.Fence 解题报告

    题目链接:http://codeforces.com/problemset/problem/363/B 题目意思:给定整数n和k,需要从n个数中找出连续的k个数之和最小,输出这连续的k个数中的第一个数 ...

  7. [Android UI] Shape详解 (GradientDrawable)

    转载自:http://blog.csdn.net/feng88724/article/details/6398193 在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来 ...

  8. [Android Pro] 关于Android的HTTP客户端的小秘密

    原文:http://android-developers.blogspot.com/2011/09/androids-http-clients.html 译文:http://yunfeng.sinaa ...

  9. Windows环境下配置Vim为Python的IDE

    (一)安装Python 2.7 在官网下载Python,并安装,我的安装路径是D:\Program Files\Python.安装完成后编辑环境变量Path,在其后添加;D:\Program File ...

  10. ubuntu 彻底删除卸载

    1911 sudo apt-get install zabbix-agent 1916 sudo apt-get autoremove zabbix_agent root@(none):~# apt- ...