Codeforces Round 287 (Div. 2)
C
Guess Your Way Out!
Codeforces
1700
原题
← BD →
翻译中…

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 height $*h*$. 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 $2^{*h*}$. The exit is located at some node $*n*$ where $1 ≤ *n* ≤ 2^{*h*}$, 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 integers $*h*, *n*$ ($1 ≤ *h* ≤ 50$, $1 ≤ *n* ≤ 2^{*h*}$).

翻译中…

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.

翻译中…

Note

翻译中…

A perfect binary tree of height $*h*$ is a binary tree consisting of $*h* + 1$ levels. Level $0$ consists of a single node called root, level $*h*$ consists of $2^{*h*}$ nodes called leaves. Each node that is not a leaf has exactly two children, left and right one.

翻译中…

Following picture illustrates the sample test number $3$. Nodes are labeled according to the order of visit.

翻译中…

image

翻译中…

样例

Input #1
1 2
Output #1
2
InputCopy OutputCopy
Input #2
2 3
Output #2
5
InputCopy OutputCopy
Input #3
3 6
Output #3
10
InputCopy OutputCopy
Input #4
10 1024
Output #4
2046
InputCopy OutputCopy
题目信息
题号
507C
时间限制
1 s
内存限制
256 MB
标签
模拟
数学
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。