pku1095卡特兰数+递归

 

题目来源:http://poj.org/problem?id=1095

题目分类:卡特兰数与递归

此题心得:学习了卡特兰数

时间:2011-7-21

Trees Made to Order

Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 5265

 

Accepted: 3016

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

East Central North America 2001

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page    Go Back   To top


All Rights Reserved 2003-2011 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator

 

题意与分析

题意:如图所示,对二叉树的编号遵循如下规则,节点少的编号小,节点相同的从右子树开始编号,从全部为右子树到全部为左子树,现在题目给你一棵二叉树的编号,让你把这棵树中序输出。。。

分析:做这个题目需要知道卡特兰数的知识,不同节点的二叉树的形状个数遵循卡特兰数。所以我们先把范围内的所有卡特兰数先求出来,然后对每棵树,我们先求出有多少个节点,然后再算出左子树的节点数和其值和右子树的节点数和其值,然后递归求解即可。此题关键在于求其左子树和右子树的值---理解一个关系,在左右字树节点个数不变情况下,左子树“动一下”就相当于右子树整棵子树“动一遍”,也就是这个节点数的卡特兰数。。。所以就会有除和取余的关系了。。。

源代码

·                /*
·                 
·                #include <iostream>
·                #include <cstdio>
·                #include <cstring>
·                using namespace std;
·                 
·                typedef __int64 llg;
·                const int N = 25;
·                 
·                llg n, f[N], sum[N];
·                 
·                void Print(int t, llg n)
·                {
·                    int i, l, r;
·                    llg tmp;
·                    for(i = 0; i < t; i++)
·                    {
·                        tmp = f[i]*f[t-i-1];
·                        if(tmp < n)  n-= tmp;
·                        else  break;
·                    }
·                    l = n/f[t-i-1] + 1;
·                    if(n%f[t-i-1] == 0)  l--;
·                    r = (n-1)%f[t-i-1] + 1;
·                    if(i > 0)
·                    {
·                        printf("(");
·                        Print(i, l);
·                        printf(")");
·                    }
·                    printf("X");
·                    if(t-i-1 > 0)
·                    {
·                        printf("(");
·                        Print(t-i-1, r);
·                        printf(")");
·                    }
·                }
·                 
·                int main()
·                {
·                    int i, j;
·                    memset(f, 0, sizeof(f));
·                    f[0] = f[1] = 1;
·                    for(i = 2; i < N; i++)
·                        for(j = 0; j < i; j++)
·                            f[i] += f[j]*f[i-j-1];
·                    sum[0] = 0;
·                    for(i = 1; i < N; i++)  sum[i] = sum[i-1] + f[i];
·                    while(scanf("%I64d", &n))
·                    {
·                        if(n == 0)  break;
·                        for(i = 1; i < N; i++)
·                            if(sum[i] >= n)
·                            {
·                                Print(i, n-sum[i-1]);
·                                printf("\n");
·                                break;
·                            }
·                    }
·                    return 0;
·                }
·                 
·                */
·                 
·                 
·                #include<iostream>
·                using namespace std;
·                 
·                const int N=21;
·                __int64 a[N], b[N], n;
·                 
·                void dfs(int num, int th)
·                {
·                      printf("(");
·                      if(num==1)
·                      {
·                            printf("X)");
·                            return;
·                      }
·                 
·                      int i, tmp = 0;
·                      for(i=0; i<num; i++)
·                      {
·                            tmp += a[i]*a[num-1-i];
·                            if(tmp>=th)
·                                  break;
·                      }
·                      tmp -= a[i]*a[num-1-i];
·                      th -= tmp;
·                      int l, r;
·                      l = 1+th/a[num-1-i];
·                      if(th%a[num-1-i]==0) l--;
·                      r = (th-1)%a[num-1-i]+1;
·                      if(i>0)
·                            dfs(i, l);
·                      printf("X");
·                      if(num-1-i>0)
·                            dfs(num-1-i, r);
·                      printf(")");
·                }
·                 
·                void d(int num, int th)
·                {
·                      if(num==1)
·                      {
·                            printf("X");
·                            return;
·                      }
·                 
·                      int i, tmp = 0;
·                      for(i=0; i<num; i++)
·                      {
·                            tmp += a[i]*a[num-1-i];
·                            if(tmp>=th)
·                                  break;
·                      }
·                      tmp -= a[i]*a[num-1-i];
·                      th -= tmp;
·                      int l, r;
·                      if(th%a[num-1-i]!=0)
·                            l = 1+th/a[num-1-i];
·                      else
·                            l = th/a[num-1-i];
·                      r = (th-1)%a[num-1-i]+1;
·                      if(i>0)
·                            dfs(i, l);
·                      printf("X");
·                      if(num-1-i>0)
·                            dfs(num-1-i, r);
·                }
·                 
·                int main()
·                {
·                      int i, j, k;
·                      a[0] = 1;
·                      a[1] = 1;
·                      b[1] = 1;
·                      b[0] = 0;
·                      for(i=2; i<N; i++)
·                      {
·                 
·                            a[i] = (i*4-2)*a[i-1]/(i+1);
·                            b[i] = a[i]+b[i-1];
·                      }
·                      while(scanf("%d", &n)!=EOF)
·                      {
·                            if(n==0)
·                                  break;
·                            for(i=1; i<N; i++)
·                            {
·                                  if(b[i]>=n)
·                                        break;
·                            }
·                            d(i, n-b[i-1]);
·                            printf("\n");
·                      }
·                      return 0;
·                }

                                            

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值