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 ;
}
随机推荐
- bzoj 2594 [Wc2006]水管局长数据加强版(LCT+最小生成树)
[深坑勿入] [给个链接] http://blog.csdn.net/popoqqq/article/details/41348549 #include<cstdio> #include& ...
- Spark中的编程模型
1. Spark中的基本概念 Application:基于Spark的用户程序,包含了一个driver program和集群中多个executor. Driver Program:运行Applicat ...
- 快速备份sqlserver2005以上版本数据库的方法-摘自网络
使用优化参数的备份命令 BACKUP DATABASE [test]TO DISK = N'D:\test_FULL_20110311_050001_1.bak',DISK = N'D:\test_F ...
- 关于 终端 ls 命令 不能区分文件和目录的问题
默认的,使用ls命令来显示目录内容的时候,“终端”对于目录.可执行文件等特殊类型的文件并没有使用颜色来显示,只有使用“ls -G”时,才能显示颜色,这可真是不方便.有没有方法可以默认显示颜色呢?方法当 ...
- 最近的bug列表总结(C++)
最近写了一大段代码,抽象得厉害,容易绕进去,因为写单测的代价很大(借口),所以很多问题到联调的是否才发现. 而且花费了很大的经历才查出来,主要问题有如下几个问题 1. 变量未初始化 具体来说,就是指针 ...
- codeforces 630H (组合数学)
H - Benches Time Limit:500MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- codeforces 630F Selection of Personnel(组合数)
F. Selection of Personnel time limit per test 0.5 seconds memory limit per test 64 megabytes input s ...
- AutoCAD.NET二次开发:创建自定义菜单(COM)
当我们要在CAD中创建自定菜单时,可以引用COM组件来实现. 下面是实现方式: 1.新建类库项目,并引用CAD目录(我这里用的是CAD2008)下的acdbmgd.dll.acmgd.dll,并将引用 ...
- 开发WebForm时遇到的问题
在做一个小项目时,一个很长的页面,页面底部有一个contact us form 整个页面我没有使用MVC,而是使用ASP.NET WebForm(.aspx)来实现,实现功能后发现,当用户在页面底部输 ...
- POJ 1860 Currency Exchange (SPFA松弛)
题目链接:http://poj.org/problem?id=1860 题意是给你n种货币,下面m种交换的方式,拥有第s种货币V元.问你最后经过任意转换可不可能有升值.下面给你货币u和货币v,r1是u ...