• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            poj1095

            Trees Made to Order
            Time Limit: 1000MS Memory Limit: 10000K
            Total Submissions: 6010 Accepted: 3459

            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

            不錯的題目

            考察卡特蘭數的遞歸式的

            等會把找到的卡特蘭數的資料發一篇上來


            code

            #include <cstdio>
            #include 
            <cstdlib>
            #include 
            <cstring>
            #include 
            <cmath>
            #include 
            <ctime>
            #include 
            <cassert>
            #include 
            <iostream>
            #include 
            <sstream>
            #include 
            <fstream>
            #include 
            <map>
            #include 
            <set>
            #include 
            <vector>
            #include 
            <queue>
            #include 
            <algorithm>
            #include 
            <iomanip>
            using namespace std;
            long long dx[20]={1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,742900,2674440,9694845,35357670,129644790,477638700,1767263190};
            void dg(long long n,long long k)
            {
                
            int i;
                
            long long sum;
                
            if(n==1)
                {
                    printf(
            "X");
                    
            return ;
                }
                sum
            =0;
                
            for(i=0;k>sum;i++) sum+=dx[i]*dx[n-i-1];
                i
            =i-1;
                sum
            -=dx[i]*dx[n-i-1];
                k
            -=sum;
                
            //printf("%d %d %d\n",n,k,i);
                if(i)
                {
                    printf(
            "(");
                    dg(i,(k
            -1)/dx[n-i-1]+1);//沒有也是一種
                    printf(")");
                }
                printf(
            "X");
                
            if(n-i-1)
                {
                    printf(
            "(");
                    dg(n
            -i-1,(k-1)%dx[n-i-1]+1);
                    printf(
            ")");
                }
            }
            int main()
            {
                
            int i;
                
            long long n;
                
            long long sum;
                
            while(scanf("%I64d",&n)!=EOF&&n!=0)
                {
                    
            if(n==1)
                    {
                        printf(
            "X\n");
                    }
                    
            else
                    {
                        sum
            =0;
                        
            for(i=1;n>sum;i++) sum+=dx[i];
                        i
            --;
                        sum
            -=dx[i];
                        dg(i,n
            -sum);
                        printf(
            "\n");
                    }

                }
                
            return 0;
            }

            posted on 2012-08-02 16:56 jh818012 閱讀(168) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            人人狠狠综合久久亚洲| 久久国产精品无| 欧美一区二区精品久久| A级毛片无码久久精品免费| 婷婷久久五月天| 久久久久久久久久久久中文字幕| 久久精品9988| 精品久久久久久中文字幕大豆网| 囯产精品久久久久久久久蜜桃| 日产精品久久久久久久| 久久国产视屏| 久久精品国产亚洲77777| 久久国内免费视频| 国产精品激情综合久久| 久久精品国产精品亚洲毛片| 久久久久国色AV免费观看| 久久91亚洲人成电影网站| 久久强奷乱码老熟女网站| 久久久久久亚洲精品不卡 | 久久久九九有精品国产| 精品欧美一区二区三区久久久| 亚洲国产精品热久久| 国产精品久久久久久久久免费| 99久久免费国产精品特黄| 久久99精品久久久久久野外| 狠狠色噜噜狠狠狠狠狠色综合久久 | 色综合色天天久久婷婷基地| 无码人妻久久久一区二区三区| 久久久久综合中文字幕| 国产成人精品久久| 久久91精品国产91久久户| 99久久免费国产精精品| 久久精品国产亚洲av影院| 亚洲第一极品精品无码久久| 少妇熟女久久综合网色欲| 综合久久给合久久狠狠狠97色 | 狠狠色丁香久久婷婷综合五月| 亚洲国产日韩欧美久久| 亚洲国产成人精品女人久久久| 久久亚洲电影| 亚洲国产综合久久天堂|