• <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 閱讀(169) 評論(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
            • 評論內容較長,點擊標題查看
            • --王私江
            久久久精品国产| 国产精品99久久久久久宅男 | 亚洲AV无码一区东京热久久| 久久人人添人人爽添人人片牛牛| 亚洲va久久久噜噜噜久久| 996久久国产精品线观看| 久久久久亚洲AV成人网人人网站| 久久只这里是精品66| 72种姿势欧美久久久久大黄蕉| 久久er国产精品免费观看8| 日本强好片久久久久久AAA | 99久久国产精品免费一区二区| 国产精品美女久久久m| 伊人伊成久久人综合网777| 久久国产精品成人免费| 亚洲国产精品一区二区久久hs| 蜜桃麻豆www久久| 久久久久久国产精品无码超碰| 亚洲国产精品嫩草影院久久| 午夜不卡888久久| 99精品久久精品一区二区| 久久国产免费直播| 久久综合狠狠综合久久97色| 久久精品国产秦先生| 久久天天躁狠狠躁夜夜网站 | 国产精品视频久久| 国产成人精品三上悠亚久久| 午夜精品久久久久久影视777 | 久久综合久久综合久久综合| 国产V综合V亚洲欧美久久| 久久久久久九九99精品| 日产精品99久久久久久| 欧美牲交A欧牲交aⅴ久久| 一本一本久久A久久综合精品| 久久亚洲精品无码VA大香大香| 久久综合九色欧美综合狠狠| 久久久久亚洲?V成人无码| 久久久久国产一级毛片高清板| 精品国产91久久久久久久a| 久久无码人妻精品一区二区三区| 精品久久久久久无码人妻蜜桃 |