Codeforces -- 507C -- Guess Your Way Out! (On the importance of reading questions ..), Codeforces
Guess Your Way Out! Time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output
Amr bought a new video game "Guess Your Way Out! ". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of heightH. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.
Let's index all the leaf nodes from the left to the right from 1 to 2H. The exit is located at some nodeNWhere 1 limit ≤ limitNLimit ≤ limit 2H, The player doesn' t know where the exit is so he has to guess his way 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 current node ";
- Character 'R' means "go to the right child of the current node ";
- If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;
- If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command;
- If he reached a leaf node that is not the exit, 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 is going to visit before reaching the exit?
Input
Input consists of two integersH, Bytes,N(1 digit ≤ DigitHLimit ≤ limit 50, 1 limit ≤ limitNLimit ≤ limit 2H).
Output
Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the 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
Note
A perfect binary tree of heightHIs a binary tree consistingHLimit + limit 1 levels. Level 0 consists of a single node called root, levelHConsists of 2HNodes called leaves. Each node that is not a leaf has exactly two children, left and right one.
Following picture into strates the sample test number 3. Nodes are labeled according to the order of visit.
The depth h of the tree is given. Note that the tree is calculated from layer 0 and the nth node at the bottom layer. Ask LRLRLRLR .... The number of nodes accessed by that node.
(PS: the nth node of the entire tree is read. If you test the sample directly, you will not be able to get through it ....)
1. L go to the left subtree of the node.
2. R goes to the right subtree of the node.
3. skip this step if you want to access the node.
4. If you skip two consecutive steps, return the parent node of the node.
5. If a leaf node does not exit, the parent node of the node is returned.
Calculate the path from the root node to the node. (LR sequence)
Step 1 from L. If the direction is the same as the path, then the number of nodes is + 1. Otherwise, all nodes in the subtree are completed and then in the opposite direction.
#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define LL __int64LL num ;char s[100] , ch ;int main(){ LL h , n , temp , i , j , ans ; while( scanf("%I64d %I64d", &h, &n) != EOF ) { temp = 1 ; for(i = 0 ; i < h ; i++) temp *= 2 ; temp-- ; temp += n ; n = temp ; h++ ; ans = 1 ; num = 0 ; temp = n ; while( temp > 1 ) { if( temp % 2 == 1 ) s[num++] = 'R' ; else s[num++] = 'L' ; temp /= 2 ; } s[num] = '\0' ; ch = 'L' ; for(i = num-1 ; i >= 0 ; i--) { if( ch == s[i] ) { ans++ ; if( ch == 'L' ) ch = 'R' ; else ch = 'L' ; } else { temp = 1 ; for(j = 0 ; j < h-(num-i) ; j++) temp *= 2 ; temp -= 1 ; ans += temp ; ans++ ; } } ans-- ; printf("%I64d\n", ans) ; } return 0;}