Codeforces Round #287 (Div. 2) C. Guess Your-out!

Source: Internet
Author: User

AMR bought a new video game "Guess Your-out!". The goal of the game is to find a exit from the maze so looks like a perfect binary tree of height h. The player is initially standing at the root of the tree and the exit from the tree was located at some leaf node.

Let's index all the leaf nodes from the left of the right from 1 to 2H. The exit is located at some node n where 1≤ n ≤2H, the player doesn ' t know wher E The exit is so he had to guess his out!

Amr follows simple algorithm to choose the path. Let ' s consider infinite command string "lrlrlrlrl ..." (consisting of alternating characters ' l ' and ' R '). AMR sequentially executes the characters of the string using following rules:

    • Character 'L ' means "go to the left child of the";
    • Character 'R ' means "go to the right child of the";
    • If The destination node is already visited, AMR skips current command, otherwise he moves to the destination node;
    • If AMR skipped the consecutive commands, he goes back to the parent of the current node before executing next command;
    • If He reached a leaf node that's not the exit and he returns to the parent of the current node;
    • If he reaches an exit, the game is finished.

Now AMR wonders, if he follows this algorithm, how many nodes he was going to visit before reaching the exit?

Input

Input consists of integers h, n (1≤ h ≤50, 1≤ n ≤2h).

Output

Output A single integer representing the number of nodes (excluding the exit node) AMR was going to visit before reaching T He exit by following this algorithm.

Sample Test (s) input
1 2
Output
2
Input
2 3
Output
5
Input
3 6
Output
10
Input
10 1024
Output
2046

Test instructions: Give n, give a full binary tree with a height of h+1, for each row of elements left to right, from 1 to 2^h marking. Just start at root, then follow the left and right alternately.
1. If the current destination has passed, he will skip the current instruction.
2. If you skip two instructions consecutively, he will return to the parent node.
3. If a nonexistent point is reached, he will return to the parent node
4. Reach the point labeled N, and he wins.
The number of points to pass halfway through.
Hands-on painting, you can find that if he came directly from his father, he passed by the number of points is through his father's number plus 1. Otherwise his brother node is gone, after the Father node in the Add 1. Because the request is to pass through, and finally subtract itself from the 1 points. Then the recursive solution can be. Note from the first layer to the second level, parity is the same as directly from the Father walk past, others are the odd and even different direct walk past.

#include <cstdio>#include<cstring>#include<algorithm>#include<map>#include<Set>#include<bitset>#include<queue>#include<stack>#include<string>#include<iostream>#include<cmath>#include<climits>using namespaceStd;typedefLong LongLL; LL h; ll Cifang (ll X,ll y) {ll ans=1;  for(LL i =0; i<y;i++) ans*=x; returnans;} ll Gao (ll X,ll y) {ll T= (y+1)/2; if(x==1)return 1; if(x==2){        if(y==1){            return 2; }        Else returnCifang (2, h-x+1) +1; }    if(t%2==y%2) {LL k= Cifang (2, h-x+1) +gao (x1, T); returnK; }    Else return 1+gao (x1, t);}intMain () {LL n; CIN>>h>>N; H++; LL ans= -;  for(LL i =1; i<=h;i++) {LL T= Cifang (2, I-1); if(t>=N) {ans= i; Break; }} LL Min= (1ll<< -); //For (LL i = ans;i<=h;i++) {//min = min (Min,gao (i,n)); //cout<<i<< "" <<Min<<endl; //}Cout<<gao (H,n)-1<<Endl; return 0;}

Codeforces Round #287 (Div. 2) C. Guess Your-out!

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.