UVA 679 dropping Balls

Source: Internet
Author: User

Original question:
A 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
Position 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 i-th 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:
2≤d≤20, and 1≤i≤524288.
Input
Contains L + 2 lines.
Line 1 L The number of test cases
Line 2 D 1 I 1 test Case #1, both decimal numbers that is separated by 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

English:
Give you a depth of D for a complete binary tree, and then give you I a small ball, each of the nodes of the binary tree above has a switch, the start switch is off, the ball from the root node whereabouts, if the ball hit the current switch is off, then switch to open state, small ball to the left, if the current open state, switch turned off Ball to the right. Ask you what the number of the last leaf node you fell to.

#include <bits/stdc++.h>
using namespace std;
int main ()
{
    Ios::sync_with_stdio (false);
    Long long d,i;
    int k;
    cin>>k;
    while (k--)
    {
        cin>>d;
        if (d==-1) break
            ;
        cin>>i;
        Long long Ans=1;
        For (long long i=1;i<d;i++)
        {
            if (i%2)
            {
                ans=ans*2;
                I= (i+1)/2;
            }
            else
            {
                ans=ans*2+1;
                i/=2;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

Answer:
Rujia Classic examples, purple books, there are also small white books. You can open an array of 1 million to simulate, but you can also find the law. If a node is gone n times, then if n is odd, when the ball falls on this node, it must be to the right sub-tree line, if the even number must be to the left subtree direction. So, by calculating the number of the ball can get it through the node is to be traversed odd times or even several times, judge can.

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.