POJ 1095 Trees Made to Order(计数问题)

Trees Made to Order
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6882 Accepted: 3940

Description

We can number binary trees using the following scheme: 
The empty tree is numbered 0. 
The single-node tree is numbered 1. 
All binary trees having m nodes have numbers less than all those having m+1 nodes. 
Any binary tree having m nodes with left and right subtrees L and R is numbered n such that all trees having m nodes numbered > n have either Left subtrees numbered higher than L, or A left subtree = L and a right subtree numbered higher than R. 

The first 10 binary trees and tree number 20 in this sequence are shown below: 

Your job for this problem is to output a binary tree when given its order number. 

Input

Input consists of multiple problem instances. Each instance consists of a single integer n, where 1 <= n <= 500,000,000. A value of n = 0 terminates input. (Note that this means you will never have to output the empty tree.)

Output

For each problem instance, you should output one line containing the tree corresponding to the order number for that instance. To print out the tree, use the following scheme: 

A tree with no children should be output as X. 
A tree with left and right subtrees L and R should be output as (L')X(R'), where L' and R' are the representations of L and R. 
If L is empty, just output X(R'). 
If R is empty, just output (L')X. 

Sample Input

1
20
31117532
0

Sample Output

X
((X)X(X))X
(X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X)

Source






#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i < b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 20

int dp[N];

void inint()
{
	int i,j;
	dp[0]=dp[1]=1;
	for(i=2;i<N;i++)
	{
		fre(j,0,i)
		  dp[i]+=dp[j]*dp[i-1-j];  //i个节点的情况有列举左边放j,因为根节点需要一个,那么右边i-1-j
	}

}

void dfs(int n,int k)    //n个节点,第k大
{
	if(n==1)    //开始是k==1,一直错
	{
		pf("X");
		return ;
	}

	int i=0;
    for(i=0;;i++)
		if(dp[i]*dp[n-i-1]>=k) break;  //左边有dp[i]种情况,右边有dp[n-i-1],因为有一个点当根节点
	 else  k-=dp[i]*dp[n-i-1];

    if(i)       //左边有节点
	{
		pf("(");
		dfs(i,(k-1)/dp[n-i-1]+1);
		pf(")");
	}
	pf("X");
	if(n-i-1)    //右边有节点
	{
		pf("(");
		dfs(n-1-i,(k-1)%dp[n-1-i]+1);
		pf(")");
	}
}
int main()
{
	int i,j,n;
	inint();
	while(sf(n),n)
	{
		for(i=1;;i++)
		  if(dp[i]>=n) break; //已经锁定i个节点是满足条件的
		 else n-=dp[i];
		dfs(i,n);
      pf("\n");
	}
    return 0;
}



【音视频基础】一篇搞懂视频的各种封装格式 在现代多媒体处理中,视频封装格式扮演着至关重要的角色。不同的封装格式决定了视频文件的存储方式、兼容性以及功能特性。无论是观看高清视频、编辑电影剪辑,还是在互联网上流式传输内容,理解各种视频封装格式的基础知识都是必不可少的。在这篇文章中,我们将详细介绍常见的视频封装格式,探讨为什么需要这些格式,以及它们各自的功能和区别,帮助你在选择和使用视频格式时做出更明智的决定。WEBM理解各种视频封装格式的功能和区别对于选择合适的格式至关重要。 阅读详情

相关推荐

五、e2studio VS STM32CubeIDE之汉化

介绍stm32cubeide汉化方案和汉化包e2studio自带汉化包,在安装过程中可选择安装e2studio STM32CubeIDE切换中英文。

cl234583745的博客 1766

BZOJ 1192 [HNOI2006] 鬼谷子的钱袋 题解

#include #include usingnamespacestd; structdata{ intl,r; longlongtag,min; }tr[4000001]; inta[1000001],n,m; boolflag; voidbuild(intk,ints,intt) { tr[k].l=s;tr[k].r=t; if(s==t)

Silence的程序实验室 1616

ArcGIS小白必看:5分钟搞定shp矢量文件分割(附常见错误排查)

本文为ArcGIS新手详细解析了分割shp矢量文件的四种核心方法:裁剪工具、分割工具、按属性选择导出及ArcPy脚本自动化。重点强调了投影匹配等关键步骤,并提供了常见错误排查指南,帮助用户快速掌握矢量数据分割技巧,提升GIS数据处理效率。

h9j8k7l6m5n的博客 798

poj 1095 Trees Made to Order

<br />//poj 1095 Trees Made to Order 很经典的递归题 <br />/* <br />题解:先判断有多少节点,左右各有多少节点,再递归构造左右子数 <br />做了好几个小时,看到好多人都是三两下清洁溜溜,惭愧至极。。。<br />后来看了别人的题解,才知道自己的思路是完全正确的,只是有个小BUG,<br />哎,真是没自信啊。。。 <br />*/<br />#include <iostream><br />using namespace std;<br />int n,

birdforever的专栏 625

手把手教你用Gazebo+ROS搭建智能小车机械臂系统(含完整配置文件)

本文提供了一份详细的实践指南,手把手教你使用Gazebo和ROS搭建智能小车机械臂仿真系统。内容涵盖从URDF/Xacro模型设计、Gazebo控制器配置到ROS话题通信与系统集成的完整流程,并提供了可复用的配置文件与调试技巧,帮助开发者快速构建并控制一个功能完备的移动操作机器人平台。

sugar的博客 971

PostgreSQL:十五. 性能优化

十五. 性能优化 优化简介: PostgreSQL优化一方面是找出系统的瓶颈,提高PostgreSQL数据库整体的性能; 另一方面,需要合理的结构设计和参数调整,以提高用户操作响应的速度; 同时还要尽可能的节省系统资源,以便系统可以提供更大负荷的服务。 PostgreSQL数据库优化是多方面的,原则是减少系统的瓶颈,减少资源的占用,增加系统的反应速度。例如: 通过优化文件系统,提高磁盘IO的读写速度; 通过优化操作系统调度策略,提高PostgreSQL的在高负荷情况下负载能力; 优化表结构、索引、查询

数据知道的博客 4万+

POJ 1095 Trees Made to Order

题目大意: 按照题意将数用二叉树的形式表示出来。将二叉树按照要求形式输出。 解题思路: 由于有图示,可以轻易看出这个顺序跟卡特兰数列有关。且对于任意一个树的任意子树来说,右子树相当于分针,左子树相当于秒针。也就是说当右子树要变换到下一种状态时,左子树要将它所能变换的状态全变换完才可以。 下面是代码: #include #include #include #inclu

脑残的孩子只有慢慢学。。 4692

XPATH注入漏洞检测工具汇总

环境 > Python3.7。

墨痕诉清风的博客 742

POJ 1095 Trees Made to Order 卡特兰数以及递归分治

转载请注明出处,谢谢 http://blog.youkuaiyun.com/ACM_cxlove?viewmode=contents           by---cxlove 题目链接 http://poj.org/problem?id=1095 debug到蛋碎的一题,原本是熟悉下卡特兰数,结果卡特兰数部分很水,一直在调试递归 预处理卡特兰数之后,可以推出当前是多少个节点组成的二叉树,也知道

窝不是爱酱,喵~~~~ 3697

以太网_RGMII接口与GMII接口的转化_03

RGMII接口转GMII接口!

weixin_42499230的博客 378

FPGA/IC秋招经典100题(含详解)

FPGA/IC秋招笔试面试经典100题(含答案及详解),持续更新。。。

Crazzy_M的博客 1万+

【POJ 1095】Trees Made to Order(卡特兰+递归)

Trees Made to Order Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 7155Accepted: 4094 Description We can number binary trees using the following scheme: The empty tree is numbered 0. The sin

ACMer' 2415

[毕设项目-MCP+ENSP]一句话搞定ENSP自动化:MCP + LLM实战(架构图、工具清单、踩坑全解)

用 MCP + LLM 把“人话”变成对华为 ENSP 的安全自动化操作,三款工具覆盖配置/查询/落盘,配套架构、实战与排坑清单。

逐梦苍穹的博客 2538

POJ 1095 Trees Made to Order 已被翻译

woniupengpeng的专栏 312

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

poj-1095-Trees Made to Order

#include #include #include #include #include #include #define LL long long #define MAXN 550000000 using namespace std; LL dp[201]; LL ans[201]; struct list { LL l,r; }node[51]; void init() { L

Silence的程序实验室 861

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

POJ1095 Trees Made to Order(JAVA)

这题用到了卡特兰数,比较麻烦。关于卡特兰数的基本概念百度一下你就知道。 使用卡特兰数对数组元素进行分组之后,需要具体计算一下要求的是第几组的第几个数,然后向下递归。 首先来看利用卡特兰数分组:   从1开始前4个卡特兰数是 C[1]=1, C[2]=2, C[3]=5, C[4]=14 (C[0]也是有定义的,C[0]=1)   于是我们把第1个元素归为第1组,第2,3个元素归...

ditumao9111的博客 210

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

poj 1095--Trees Made to Order

 原题地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1095 DescriptionWe can number binary trees using the following scheme: The empty tree is numbered 0. The single-node tree is numbered 1. All

scut_lyq00 3633

POJ 1095 Trees Made to Order (卡特兰数)

Trees Made to Order Time Limit:1000MS Memory Limit:10000K Total Submissions:6290 Accepted:3619 Description We can number binary trees using the following schem...

anvt77205的博客 143

POJ - 1095 Trees Made to Order 卡特兰数+递归

题目: 求第n棵二叉树 思路:用卡特兰数确定一共有几个节点,然后递归输出 代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include #include #include #include #include #include #include #include #include #include #incl

ACVevtor的博客 444

POJ—1095 Trees Made to Order(卡特兰数+递归)

题目链接 题意: 如图编号,结点数依次增加,每次先对左子树进行递归操作,再对右子树进行递归,用X表示一个节点,每一对括号表示一棵树,以 (左子树)X(右子树) 的形式输出这棵树,当然如果是空树就不输出, 求编号为n的二叉树 思路: 1.下面先提供比较简单好理解的写法 1 首先二叉树的种类数满足卡特兰数,所以有递推关系可得出任意结点的卡特兰数ct[],根据编号找到它的结点数,和在该结点数下...

没有期待的日子反而会顺顺利利 293

POJ 1095 Trees Made to Order 笔记

给出数字,输出它对应二叉树的括号形式。 如图。m个节点的二叉树小于m+1节点的二叉树。 任何有m个节点的二叉树有左右子树为L、R,编号为n。其他有m个节点编号大于n的二叉树,不是左子树的编号大于L的编号,就是左子树编号等于L的编号且右子树编号大于R的编号。

woniupengpeng的专栏 770
上一篇: Acdream 1113 The Arrow(概率dp)
下一篇: HDU 1800 Flying to the Mars(字典树)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值