UVA679 dropping Balls "Binary tree node number"

Source: Internet
Author: User

dropping BallsA number of K balls is dropped one by one from the root of a fully binary tree structure FBT. Each time the ball being dropped first visits a non-terminal node. It then keeps moving down either follows the path of the left subtree, or follows the path of the right subtree, until it Stops at one of the leaf nodes of FBT. To determine a ball ' s moving direction a flag was set up in every Non-terminal node with the values, either false or true. Initially, all of the flags is false. When visiting a non-terminal node if the flag ' s current value at this node is false and then the ball would first switch this Flag ' s value, i.e, from the false to the true, and then follow the left subtree of this node to keep moving down. Otherwise, it'll also switch this flag's value, i.e, from the true to the false, but would follow the right subtree of t He node to keep moving down. Furthermore, all nodes of FBT is sequentially numbered, starting at 1 with nodes on depth 1, and then those on depth 2, a nd sO on. Nodes on any depth is numbered from left to right.

For example, Fig. 1 represents a fully binary tree of maximum depth 4 with the node numbers 1, 2, 3, ..., 15. Since all of the flags is initially set to is false, the first ball being dropped would switch flag ' s values at node 1, no De 2, and Node 4 before it finally stops at position 8. The second ball being dropped would switch flag ' s values at node 1, node 3, and Node 6, and stop at position 12. Obviously, the third ball being dropped would switch flag ' s values at node 1, node 2, and node 5 before it stops at Positio N 10.
Fig. 1:an example of FBT with the maximum depth 4 and sequential node numbers.

Now consider a number of test cases where the values would be given for each test. The first value is D, the maximum depth of FBT, and the second one are I, the Ith ball being dropped. You may assume the value of I won't exceed the total number of the leaf nodes for the given FBT.

Determine the stop position P for each test case.

For each test cases the range of parameters D and I are as below:


\begin{displaymath}2 \le D \le, \mbox{and} 1 \le I \le 524288.
\end{displaymath}

Input

Contains l+2 lines.

Line 1 I The number of test cases
Line 2 $D _1 \ i_1$
Test Case #1, both decimal numbers that is separatedby one blank
...
Line k+1 $D _k \ i_k$
Test Case #k
Line l+1 $D _l \ i_l$
Test Case #l
Line l+2-1 a constant-1 representing the end of the input file


Output


Contains l lines.

Line 1 , the stop position P for the test case #1
...
Line k The stop position P for the test case #k
...

Line L The Stop position P for the test case #l


Sample Input

5
4 2
3 4
10 1
2 2
8 128

-1


Sample Output

12
7
512
3
255

Miguel Revilla

2000-08-14


Title: To a full binary tree with a depth of D, the node number from top to bottom from left to right for 1,2,3,4 ...

At the top of the node 1 a small ball, the ball falls on a node will change the point of the switch. If the point is open, the

Go left, or go right, give you I small ball, ask the last ball can fall to the last leaf number.

Idea: The odd number of balls on each node falls on the left sub-tree, and the first even number of small balls fall on the right sub-tree.

You can judge the last ball directly. If I is an odd number of nodes, it is the i+1/2 ball that goes left.

If it is even, it is a small ball i/2 to the right.


#include <stdio.h> #include <string.h>int main () {    int d,i,n;    while (~SCANF ("%d", &n) && n!=-1)    {        while (n--)        {            int k = 1;            scanf ("%d%d", &d,&i);            for (int i = 1; i < D; i++)            {                if (i&1)                {                    k <<= 1;                    I = (i+1) >>1;                }                else                {                    k = (k<<1|1);                    I >>= 1;                }            }            printf ("%d\n", k);        }    }    return 0;}




UVA679 dropping Balls "Binary tree node number"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.