CF507C.Guess Your Way Out!

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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?

Amr 购买了一款新视频游戏《猜猜你的出路!》。游戏的目标是找到一棵高度为 hh 的完美二叉树迷宫的出口。玩家初始时位于树的根节点,而出口位于某个叶节点上。

我们将所有叶节点从左到右依次编号为 11 到 2h2^h。出口位于某个节点 nn 上,其中 1 ≤ n ≤ 2h1 \leq n \leq 2^h;玩家并不知道出口的具体位置,因此他必须靠猜测来走出迷宫!

Amr 采用一种简单的算法来选择路径。我们考虑一个无限长的指令字符串 "LRLRLRLRL..."(由字符 'L' 和 'R' 交替组成)。Amr 按顺序执行该字符串中的每个字符,遵循如下规则:

  • 字符 'L' 表示“前往当前节点的左子节点”;
  • 字符 'R' 表示“前往当前节点的右子节点”;
  • 若目标节点已被访问过,Amr 跳过当前指令;否则,他移动至目标节点;
  • 若 Amr 连续跳过了两条指令,则他在执行下一条指令前,先返回当前节点的父节点;
  • 若他到达了一个非出口的叶节点,则他返回当前节点的父节点;
  • 若他到达了出口,则游戏结束。

现在 Amr 想知道:若他严格遵循这一算法,那么在抵达出口前,他总共会访问多少个节点?

输入格式

Input consists of two integers h, n (1 ≤ h ≤ 50, 1 ≤ n ≤ 2_h_).

输入包含两个整数 hh 和 nn(1≤h≤501 \leq h \leq 50,1≤n≤2h1 \leq n \leq 2^h)。

输出格式

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.

输出一个整数,表示 Amr 在按照该算法行进并到达出口前将访问的节点数(不包括出口节点)。

输入输出样例

  • 输入#1

    1 2

    输出#1

    2
  • 输入#2

    2 3

    输出#2

    5
  • 输入#3

    3 6

    输出#3

    10
  • 输入#4

    10 1024

    输出#4

    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 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.

高度为 hh 的完美二叉树是一棵包含 h+1h+1 层的二叉树。第 00 层仅包含一个节点,称为根节点;第 hh 层包含 2h2^h 个节点,称为叶节点。每个非叶节点恰好有两个子节点:左子节点和右子节点。

下图展示了样例测试用例 3。节点按照访问顺序进行编号。

输入解题思路,AI测评打分。不知道怎么写?

首页