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 ;
}
随机推荐
- 最短路+线段交 POJ 1556 好题
// 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...
- 微软Azure开始支持Docker技术
前一段时间还在与微软的技术人员讨论媒体转换服务的效率问题,如果应用 Docker将会有质的提高,没想到国外的Azure已经开始支持了,相信国内Azure支持也不远了.微软正在努力确保Azure成为开发 ...
- Windows7 32位下opencv与python2.66的环境配置
刚接触Python和OpenCV,对两者都不太了解,因为今后学习会使用到这两种工具,特此学习配置.PS:本帖适用小白. 一. 需要的文件 1. OpenCV 可用OpenCV-2.3.1-win-su ...
- Intellij IDEA使用Maven构建Scala项目
1.安装IDEA的Scala插件 使用自带的在线安装方式较为简单.File--Setting--Plugins--Browse reposities 2.创建项目 File - ...
- HDU-4734 F(x) 数位DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 注意到F(x)的值比较小,所以可以先预处理所有F(x)的组合个数.f[i][j]表示 i 位数时 ...
- ProtoBuffer 简单例子
最近学了一下protobuf,写了一个简单的例子,如下: person.proto文件 message Person{ required string name = 1; required int32 ...
- 为Delphi程序增加UAC功能(管理员身份运行exe)
相关资料:http://bbs.csdn.net/topics/320071356# 操作方法: 在Source\VCL目录下应该有这样两个文件sample.manifest和WindowsXP.rc ...
- [转]Freemarker数据类型转换
转至:http://blog.sina.com.cn/s/blog_667ac0360102eaz8.html // 测试程序 package myTest; import java.io.Buffe ...
- 视频: 千重浪Linux系统调试技术培训 03-01_Basic-CPU-Register
免费轻松学习Linux系统调试技术 欢迎收看本工作室放到优酷上播放的免费教学视频. 清晰! 完整! 无病毒! 请点击:http://v.youku.com/v_show/id_XNjM1OTQ3Mj ...
- php 处理高并发的思路
1.nginx 服务器,提高网站服务器并发性能 2.控制大文件的下载,减少CPU的消耗. 3.对于sql查询做缓存. 4.静态页面文件缓存. 5.CND缓存静态文件, 6.反向代理到多个服务器,用来分 ...