• <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

            不錯的題目

            考察卡特蘭數(shù)的遞歸式的

            等會把找到的卡特蘭數(shù)的資料發(fā)一篇上來


            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 閱讀(169) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


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

            導(dǎo)航

            統(tǒng)計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當(dāng)于是 取余3的意思 因?yàn)?3 的 二進(jìn)制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內(nèi)容較長,點(diǎn)擊標(biāo)題查看
            • --王私江
            久久香蕉国产线看观看猫咪?v| 97精品伊人久久久大香线蕉| 日本精品久久久久久久久免费| 日日狠狠久久偷偷色综合0| 久久精品国产只有精品66| 无码精品久久一区二区三区| 亚洲中文字幕无码久久2017| 国产亚洲精品久久久久秋霞 | 亚洲国产成人精品久久久国产成人一区二区三区综 | 婷婷综合久久中文字幕蜜桃三电影| 伊人久久大香线蕉综合Av| 94久久国产乱子伦精品免费| 欧美精品乱码99久久蜜桃| www.久久热.com| 国内精品人妻无码久久久影院导航| 国产精品99久久久久久人| 久久精品免费一区二区| 国产精久久一区二区三区 | 国产精品久久久久久| 少妇久久久久久被弄到高潮| 国产亚洲精久久久久久无码| 一个色综合久久| 久久精品国产亚洲av瑜伽| 国产精品久久自在自线观看| 中文精品久久久久人妻不卡| 四虎久久影院| 国产69精品久久久久99尤物| 精品国产乱码久久久久久郑州公司| 久久久SS麻豆欧美国产日韩| 狠狠色丁香婷婷综合久久来来去| 69久久夜色精品国产69| 久久国产亚洲高清观看| 久久一日本道色综合久久| 日本久久中文字幕| 亚洲国产精品一区二区三区久久 | 国产亚洲精品久久久久秋霞| 久久久国产一区二区三区| 国产叼嘿久久精品久久| 久久久久久毛片免费看| 久久国产成人午夜aⅴ影院| 国产精品一区二区久久精品无码 |