• <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>
            posts - 7,comments - 3,trackbacks - 0
            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)


            思路:
            a數組表示節點數為j所能表示最大的數。
            則第j個節點所能表示的數a[j]符合卡特蘭數:
            a[j] = a[0] * a[j - 1] + a[1] * a[j - 2] + ...... + a[j - 1] * a[0];
            表示:有j個節點 = 左邊0個節點的個數 * 右邊j - 1個節點的個數 + ...... + 左邊j - 1個節點的個數 * 右邊0個節點的個數。

            之后根據讀入的n,判斷出節點數,在再判斷出左右的節點數和左右所代表的數。
            然后調用遞歸。

            #include <cstdio>
            #include 
            <cstring>
            using namespace std;

            int a[25], b[25];

            void solve(int n)
            {
                
            int t, i, j;
                
            if (n == 0return;
                
            if (n == 1)
                {
                    printf(
            "X");
                    
            return;
                }
                
            for (j = 1;; ++j)
                {
                    
            if (b[j] >= n)
                        
            break;
                }
                n 
            = n - b[j - 1];
                
            for (i = 0; i < j; ++i)
                {
                    t 
            = a[i] * a[j - 1 - i];
                    
            if (n > t)
                    {
                        n 
            = n - t;
                    }
                    
            else
                        
            break;
                }
                
            if (i != 0)
                {
                    printf(
            "(");
                    solve(b[i 
            - 1+ 1 + (n - 1)/ a[j - 1 - i]);
                    printf(
            ")");
                }
                printf(
            "X");
                
            if (i != j - 1)
                {
                    printf(
            "(");
                    solve(b[j 
            - 2 - i] + 1 + (n - 1% a[j - 1 - i]);
                    printf(
            ")");
                }
            }

            int main()
            {
                
            int n;
                
            int i, j;
                b[
            0= 0;
                a[
            0= b[1= a[1= 1;
                
            for (i = 2; i < 20++i)
                {
                    a[i] 
            = 0;
                    
            for (j = 0; j < i; ++j)
                    {
                        a[i] 
            += a[j] * a[i - j - 1];
                    }
                    b[i] 
            = b[i - 1+ a[i];
                }
                
            while (scanf("%d"&n) && n)
                {
                    solve(n);
                    printf(
            "\n");
                }
                
            return 0;
            }
            posted on 2011-10-25 20:55 LLawliet 閱讀(420) 評論(0)  編輯 收藏 引用 所屬分類: 數論
            国产成年无码久久久久毛片| 99久久免费国产精品| 久久99国产精品久久99小说| 久久强奷乱码老熟女网站| 亚洲国产日韩综合久久精品| 亚洲色婷婷综合久久| 久久久久久免费一区二区三区| 国产精品久久久久乳精品爆| 色狠狠久久综合网| 久久91精品国产91久久小草| 免费一级做a爰片久久毛片潮| 久久国产色av免费看| 999久久久国产精品| 亚洲日韩中文无码久久| 久久精品这里只有精99品| 日韩av无码久久精品免费| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 亚洲熟妇无码另类久久久| 久久精品一区二区| 亚洲愉拍99热成人精品热久久| 国产亚洲色婷婷久久99精品91| 久久免费看黄a级毛片| 久久99亚洲综合精品首页| 久久99国产综合精品女同| yy6080久久| 久久久WWW成人| 91精品国产综合久久香蕉| 欧美一区二区三区久久综合| 久久久久亚洲AV成人网人人网站 | 韩国三级中文字幕hd久久精品 | 韩国无遮挡三级久久| 亚洲va久久久噜噜噜久久| 97精品依人久久久大香线蕉97| 久久免费国产精品| 久久亚洲AV永久无码精品| 91久久精品国产成人久久| 国产精品久久一区二区三区| 久久精品人人做人人爽97 | 久久精品日日躁夜夜躁欧美| 亚洲国产成人精品91久久久 | 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 |