• <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
            • 評論內容較長,點擊標題查看
            • --王私江
            久久久久久久免费视频| 精品国际久久久久999波多野| 久久精品国产99国产精品澳门 | 亚洲国产成人久久综合一区77| 久久99精品久久久久久水蜜桃| 久久综合一区二区无码| 久久婷婷五月综合国产尤物app| 99久久无码一区人妻a黑| 色婷婷狠狠久久综合五月| 热re99久久精品国99热| 国产一区二区精品久久岳| 亚洲国产欧洲综合997久久| 国产精品va久久久久久久| 亚洲午夜久久久久久久久电影网 | 久久精品国产精品亚洲毛片| 久久亚洲综合色一区二区三区| 伊色综合久久之综合久久| 久久99中文字幕久久| 久久久久久国产精品无码下载| 91精品日韩人妻无码久久不卡| 久久精品九九亚洲精品| 久久人人爽人人人人爽AV| 久久精品国产亚洲AV不卡| 97久久天天综合色天天综合色hd| 日韩影院久久| 日韩欧美亚洲国产精品字幕久久久| 久久免费精品一区二区| 777米奇久久最新地址| 偷偷做久久久久网站| 伊人久久大香线蕉成人| 久久亚洲视频| 久久久久这里只有精品 | 久久婷婷五月综合成人D啪| 99久久夜色精品国产网站| AA级片免费看视频久久| 精品久久久久久久| 99久久精品国产毛片| 亚洲国产成人久久综合碰碰动漫3d| 国产成人精品久久一区二区三区 | 亚洲国产天堂久久综合| 久久人人爽人人爽人人片AV麻豆|