C. Guess Your Way Out!
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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 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 2h. The exit is located at some node n where 1 ≤ n ≤ 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 integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 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 height h is a binary tree consisting of h + 1 levels. Level 0 consists of a single node called root, level h consists of 2h 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.

题意是按照“LRLRLRLR....”这个指令在数上走 。。

然后给了几个返回的条件, 让你算一下经过了多少个结点。

其实就是一个根据指令递归判点在左右子树还是右子树的情况。

做法就是分成4种情况

出口在左子树,指令是L

出口在左子树,指令是R

出口在右子树,指令是L

出口在右子树,指令是R

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n , h , ok ;
LL cal( LL dep , LL pos , int tag ) {
if( ok ) return ;
if( dep == ) { ok = ; return ; }
LL tmp ;
if( !tag ) {
if( pos >(1LL<<(dep-))) tmp = (1LL<<dep)+cal(dep-,pos-(1LL<<(dep-)),) ;
else tmp = 1LL + cal(dep-,pos,);
}
else {
if( pos > (1LL<<(dep-)) ) tmp = 1LL+ cal( dep-,pos-(1LL<<(dep-)),) ;
else tmp = (1LL<<dep) + cal(dep-,pos,);
}
return tmp ;
}
int main()
{
while( cin >> h >> n ) {
ok = ;
cout << cal(h,n,) << endl ;
}
}

最新文章

  1. Mysql查询——学习阶段
  2. The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
  3. VC++6.0编译器标记的那些内存值
  4. OC语言-08-深拷贝与浅拷贝详解(示例)
  5. [Json.net]快速入门
  6. iOS 项目审核被拒原因汇总
  7. 安装完Apache和PHP之后访问PHP文件页面提示下载而没有解析 解决办法
  8. getgrgid()函数
  9. 基于visual Studio2013解决面试题之0307最后谁剩下
  10. MongoDB学习(翻译7)
  11. 反射-Emit
  12. redis(三)
  13. .Net进阶系列(14)-异步多线程(async和await)(被替换)
  14. java1.8新特性(optional 使用)
  15. echarts柱形图x轴显示不全或者每隔一个不显示的问题
  16. Is it possible to display icons in a PopupMenu?
  17. 【Servlet】Java Serlvet Listener 监听器
  18. 用java创建UDF,并用于Hive
  19. 获取本地IP和mac等信息
  20. 5.2离线使用xadmin包

热门文章

  1. antd desgin vue 报错 Warning: Each record in table should have a unique `key` prop,or set `rowKey` to an unique primary key.
  2. CGAffineTransform 图像处理类
  3. 【记录】微信emoji表情存入数据库
  4. Mystery——团队作业——系统设计
  5. IDA Pro - 如何得到比较清楚的逆向伪代码
  6. STREAM Benchmark及其操作性能分析
  7. 人生苦短_我用Python_openpyxl库读取Excel文件数据_008
  8. noteone
  9. 【HDOJ6595】Everything Is Generated In Equal Probability(期望DP)
  10. webbench(web性能压力测试工具)