CF1237E 【Balanced Binary Search Trees】

Immortal title

\(CF1237E ~Balanced~ Binary ~Search ~Trees\)

Meaning of the questions:

How many trees need to find points to (n (n \ le 10 ^ 6) \) \ point is right \ ({1,2, ..., n } \) binary search tree satisfies:

\ (1): \) in addition to the bottom layer, all the layers are full;

\ (2): \) for each point, if it had left his son, then left his son the right point and its different parity; if it has the right son, the right point and the right son of its same parity.

The answer to \ (998 244 353 \) modulo


\ (\ Rm Sol: \)

It can be found in the definition of \ (1 \) That tree is a full binary tree, then for the root node, there is still removed after it is a full binary tree

Set meets the definition \ (2 \) is a full binary tree for the perfect tree, you can find since the original tree is a binary search tree, so that the rightmost point necessarily \ (the n-\) , so there is parity with the root \ (n-\) of the same parity

It is easy to see two properties:

\ (1): \) a perfect tree subtree is still perfect tree

\ (2): \) Since a weight of \ (. 1-n-\) , so that the sequence tree traversal binary search tree is \ (. 1-n-\) , each of them corresponding to sub-tree may be a section

Consider a number of assumptions \ (X \) as the root, then there is \ (X \) parity and \ (n-\) are the same, its left subtree interval can be expressed as \ ([1, x-1 ] \) , the right subtree interval can be expressed as \ ([X +. 1, n-] \) , and with a left subtree of size \ (X-. 1 \) , the right subtree of size \ (NX \) , since \ (n-\) parity and \ (X \) identical, \ (NX \) necessarily an even number, and \ (x-1 \) parity and \ (X \) in contrast, since the left subtree it is still a perfect tree, so the above conclusion can be reused:

A perfect tree roots meet its left child parity with the sub-tree of the same size, while the right sub-tree size is even

Next, since the binary search tree only concerned with relative magnitude relation and which one sub-tree can be represented as a section \ ([L, R & lt] \) , so we use \ ([1, r-l + 1] \ ) corresponds to replace this tree all the nodes have no effect on the answer, it is easy to find after the original hypothesis is a perfect replacement tree is still a perfect tree

So the question number and the interval corresponding to the original tree and regardless of the size of this tree

Next, consider the two merged into a big tree perfect perfect tree and its legitimacy, according to the previous conditions for the perfect tree tree after the merger we can get if and only if: \ (1 \) meet after the merger of the original tree for the full binary tree, \ (2 \) right subtree size is even

We can get the hand to play: the size of \ (1,2,3,4,5 \) perfect tree form are as follows:

\(size=1:\quad 1\)

\(size=2:\quad 2.left\to 1\)

\ (size = 3: \) there is no legal

\ (size = 4: \) for the sample

\ (size = 5: \) exists only one, of:

\(3.left\to 2,2.left\to1,3.right\to 5,5.left \to 4\)

It can be observed removing \ (size = 1 \) all perfect than the height of the tree are \ (> 1 \) and not as a full binary tree

Due to the nature of \ (1 \) we know that for a size \ (> 5 \) perfect tree, its lowest level there is still a perfect tree, in other words after the removal of the root of their discontent is inevitable, so we can get a frightening conclusion:

The two are combined into a perfect tree size \ (> 5 \) of the tree if and only if a perfect \ (1): \) which left and right subtrees of the same height and the perfect tree, \ (2): \) right child tree size is even

Now we can design a very rough \ (dp \) , and so \ (dp_ {i, j} \) indicates the size as \ (i \) perfect tree height \ (j \) Number Scheme of time, then use this transfer thing, this is \ (O (n \ log n ) \) approach

Careful consideration will find a more terrible things

We knew before \ (5 \) height and program number (before the height, after the program number) in much the same terms:

\((1,1),(2,1),(-,-),(3,1),(3,1)\)

Noting the size of the right subtree is only \ (2 \) and \ (4 \)

For the right subtree \ (2 \) in terms of the merger is the only left \ (2 \) Right \ (2 \) , combined to give \ (5 \)

For the right subtree \ (4 \) in terms of the merger is the only left \ (4/5 \) Right \ (4 \) , combined to give the size of \ (9/10 \) perfect tree, height \ (4 \)

Thus it is for size \ (6-8 \) tree which has not a perfect tree, then the next size can only \ (9/10 \)

Similar consolidation can be found in \ (9/10 \) can only be obtained by combining the \ (20/21 \) , then feasible for the \ (41/42 \) ... can be found legitimate trees are only \ (1 \) kind

So just bring \ (4/5 \) as the initial value down to recursive complexity \ (O (\ log n) \)

Time \ (3s \) and \ (n \) only \ (10 ^ 6 \) should be put to other large non-constant positive solution approach too ....

\(Code:\)

#include<bits/stdc++.h>
using namespace std ;
#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )
#define re register
int gi() {
    char cc = getchar() ; int cn = 0, flus = 1 ;
    while( cc < '0' || cc > '9' ) {  if( cc == '-' ) flus = - flus ; cc = getchar() ; }
    while( cc >= '0' && cc <= '9' )  cn = cn * 10 + cc - '0', cc = getchar() ;
    return cn * flus ;
}
int n ; 
signed main()
{
    n = gi() ; 
    if( n == 1 || n == 2 ) puts("1") ;
    else {
        int x = 4, y = 5 ; 
        while( max( x, y ) < n ) {
            int ux = x, uy = y ;
            if( ux & 1 ) x = ux + uy + 1, y = uy + uy + 1 ;
            else x = ux + uy + 1, y = ux + ux + 1 ; 
            if( x > y ) swap( x, y ) ;
        }
        if( x == n || y == n ) puts("1") ;
        else puts("0") ;
    }
    return 0 ;
}

Guess you like

Origin www.cnblogs.com/Soulist/p/11698794.html