C. Guess Your Way Out!
1 second
256 megabytes
standard input
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 consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2h).
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.
1 2
2
2 3
5
3 6
10
10 1024
2046
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.
F ollowing picture illustrates the sample test number 3. Nodes are labeled according to the order of visit.

#include <iostream>
using namespace std; int main()
{
long long h, n, leaves, ans = ;
bool dir = ;
ios_base::sync_with_stdio(false);
cin >> h >> n;
leaves = (1LL << h);
while ()
{
leaves /= ;
if (leaves <= )
break;
if (n > leaves)
{
if (!dir)
ans += leaves * ; //走过走子数的所有节点和根节点
else
ans++; //加上根节点,走到右子树的根节点
n -= leaves;
dir = ;
}
else
{
if (dir)
ans += leaves * ;
else
ans++;
dir = ;
}
}
cout << ans << endl;
return ;
}
随机推荐
- AAC 格式分析
一直在做一个语音项目,到了测试阶段,近来不是很忙,想把之前做的内容整理一下. 关于AAC音频格式基本情况,可参考维基百科http://en.wikipedia.org/wiki/Advanced_Au ...
- 在windows7下配置PHP访问ICE中间件(ICE3.5.1+PHP5.4+Apache2.2 for vc9)
按照ICE的官方文档(http://doc.zeroc.com/display/Ice/Using+the+Windows+Binary+Distribution#UsingtheWindowsBin ...
- CDH5.5.1 安装Spark ON Yarn环境
CDH对我们已经封装了,我们如果需要Spark on Yarn,只需要yum安装几个包就可以了. 前面的文章我有写过如果搭建自己内网的CDH Yum服务器,请参考<CDH 5.5.1 Yum源服 ...
- POJ动态规划题目列表
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- StringReplace用法
来自:http://www.aspww.cn/View/12022801.aspx ---------------------------------------------------------- ...
- Spring InitializingBean and DisposableBean example
In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to ...
- c# socket编程简单例子
服务器端代码 using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Thr ...
- iPhone 3GS/4 / 4s/5
越努力.越幸福.----willingseal. 像素与分辨率有什么区别与联系 ????点击打开链接 像素和分辨率是成正比的,像素越大,分辨率也越高 像素 简单的说,我们通常所说的像素,就是CCD/C ...
- HttpModule的认识与深入理解及MVC运行机制
转自:http://kb.cnblogs.com/page/50130/ ASP.NET MVC架构与实战系列之二:理解MVC路由配置 http://www.cnblogs.com/jyan/arch ...
- 解决IntelliJ IDEA 13更新FindBugs 0.9.993时JRE版本过低导致启动失败问题
今晚更新FindBugs 0.9.992(FindBugs 2)至FindBugs 0.9.993(FindBugs 3)后,按要求重启IntelliJ IDEA 13.本想看看更新后多了哪些功能,结 ...