A Number ofKBalls 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 are set up in every Non-terminal node with the values, eitherfalseOrtrue. Initially, all of the flags isfalse. When visiting a non-terminal node if the flag ' s current value at this node isfalse, then the ball would first switch this flag ' s value, i.e., thefalseto thetrue, 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 thetrueto thefalse, but would follow the right subtree of this 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 isfalse, the first ball being dropped would switch flag ' s values at No De 1, Node 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 isD, the maximum depth of FBT, and the second one are i, the ith ball B Eing dropped. You may assume the value ofI 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:
Input
Contains l+2 lines.
Line 1 I The number of test cases Line 2 test Case #1, and the numbers that is separatedby one blank ... k+1 test Case #Kl+1 test Case #L-1 a constant-1 Representing the end of the input file
Output
Contains l lines.
Line 1 P for the test case #1 ... k P for the test case #K ... L P for the test case #L
Sample Input
54 23 410 12 28 128-1
Sample Output
1275123255
First consider the ball falling to the root node. The first one will fall to the left subtree. The second one falls to the right subtree, the third left dial hand tree. I is an even number to the left, odd to the right, and half to the left and to the right.
#include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include <queue > #include <algorithm> #define MEM (f) memset (F,0,sizeof (f)) #define M 100005#define mod 1000000007#define Lson o& Lt;<1, L, M#define Rson o<<1|1, m+1, rusing namespace std;typedef long long ll;const int MAX = 0x3f3f3f3f;const I NT MAXN = 2111111;int N, m, D;int main () {while (~scanf ("%d", &n) && n! =-1) {while (n--) { scanf ( "%d%d", &d, &m); int k = 1; for (int i = 0; i < d-1; i++) { if (m%2) { k = 2*k; m = (m+1)/2; } else { k = 2*k+1; m = M/2; } } printf ("%d\n", k); } } return 0;}
UVa 679-dropping Balls