[Explanations] small ball

Title Description

  Many small balls fall one by one from a full binary tree consisting of FBT (Full Binary Tree, full binary tree), each time, a non-leaf nodes of the first visit of a ball is falling. Then proceed down or go right subtree, or go left subtree, until access to the leaf node. Determine the direction of movement of the ball is a Boolean value for each node. Initially, all nodes are false, when access to a node, if the node is false, then the ball to turn it into true, then the left subtree go, to continue its journey. If the node is true, then the ball will change it is false, and the next right subtree go. The method of marking full binary tree as shown below:

Failed to load picture

  Because after all nodes initially Boolean value is false, so the ball will first access node 1, node 4, node 2 and node transition in stopping the node 8. Second goal will access nodes 1,3,6, stop at the node 12. Obviously, the third goal before it stopped, access nodes 1,2,5, stop at the node 10.

  Now your task is given FBT depth D, and I, I represents a small ball drop, you can assume that the number of leaves I do not exceed a given FBT, writing a program seeking to stop the ball when the number of leaves.

 

Input Format

  Line, separated by a space containing two integers D and I. Which 2≤D≤20,1≤I≤524288.

 

Output Format

  Line, the corresponding output I of a small number when the ball drop leaves stopped.

 

SAMPLE INPUT

4 2

 

Sample Output

12

 

answer

  We make $ I = I - 1 $ (from $ 0 $ start count), then converted to binary, easy to find, when the depth is $ i $, $ I $ from a low to a high number of first $ I $ as if $ $ 0, then the left child node drops, if $ 1 $, on the whereabouts of the right child node.

#include <iostream>

using namespace std;

int d, I;
int ans = 1;

int main()
{
    cin >> d >> I;
    --d;
    --I;
    while(d)
    {
        ans <<= 1;
        if(I & 1) ans |= 1;
        --d;
        I >>= 1;
    }
    cout << ans;
    return 0;
} 
Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/10988762.html