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 ;
}
}

Codefores 507C Guess Your Way Out!(递归)的更多相关文章

  1. .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]

    方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...

  2. 算法笔记_013:汉诺塔问题(Java递归法和非递归法)

    目录 1 问题描述 2 解决方案  2.1 递归法 2.2 非递归法 1 问题描述 Simulate the movement of the Towers of Hanoi Puzzle; Bonus ...

  3. Android 算法 关于递归和二分法的小算法

     // 1. 实现一个函数,在一个有序整型数组中二分查找出指定的值,找到则返回该值的位置,找不到返回 -1. package demo; public class Mytest { public st ...

  4. 二叉树的递归实现(java)

    这里演示的二叉树为3层. 递归实现,先构造出一个root节点,先判断左子节点是否为空,为空则构造左子节点,否则进入下一步判断右子节点是否为空,为空则构造右子节点. 利用层数控制迭代次数. 依次递归第二 ...

  5. 递归实现n(经典的8皇后问题)皇后的问题

    问题描述:八皇后问题是一个以国际象棋为背景的问题:如何能够在8×8的国际象棋棋盘上放置八个皇后, 使得任何一个皇后都无法直接吃掉其他的皇后?为了达到此目的,任两个皇后都不能处于同一条横行.纵行或斜线上 ...

  6. C语言用分别用递归和循环求数字的阶乘的方法

    以下代码均为 自己 实现,嘻嘻! 参考文章:http://blog.csdn.net/talk_8/article/details/46289683 循环法 int CalFactorial(int ...

  7. C#递归解决汉诺塔问题(Hanoi)

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace MyExamp ...

  8. Java之递归求和的两张方法

    方法一: package com.smbea.demo; public class Student { private int sum = 0; /** * 递归求和 * @param num */ ...

  9. C#语言基础——递归

    递归 一.概念conception: 函数体内调用本函数自身,直到符合某一条件不再继续调用. 二.应满足条件factor: (1)有反复执行的过程(调用自身): (2)有跳出反复执行过程的条件(函数出 ...

随机推荐

  1. ubuntu安装supervisor以及使用supervisor启动.net core进程

    1.下载.net core项目ubuntu系统运行容器dotnet      1.版本:dotnet-sdk-2.1.3-linux-x64.tar.gz      2.将下载好的包上传到ubuntu ...

  2. JS书目推荐(私教推荐)

    下面几本书是私教推荐的,从入门到提高,从易到难,想找电子版的可以去下面这个网站找找,挺多书籍的 鸠摩搜书https://www.jiumodiary.com/ JavaScript编程精解 (第二版) ...

  3. 二、Spring Boot系列:Spring Initializer快速创建Spring Boot项目

    1.点击创建新工程 2.选择Spring Initializer和jdk1.8 注意:项目名称要小写字母 3.现在只需要一个创建一个web,选择一个就好 4.没有用的文件,可以删除 5.pom.xml ...

  4. Vue-native绑定原生事件

    首先介绍一下是什么意思: 意思就是当你给一个vue组件绑定事件时候,要加上native!如果是普通的html元素!就不需要 <div id = "app"> <m ...

  5. $NOIP2018$ 暴踩全场计划实施方案

    \(NOIP2018\) 暴踩全场计划实施方案 改完题辣!该更博辣!(这么激动干嘛反正又没人看) 我要取一个霸气的名字.于是就这样了.原本打算是暴踩yyb计划实施方案的(来啊互相伤害啊) 信心流选手就 ...

  6. 浅析 http 接口

     一.HTTP接口 Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通过TCP建立起一个到服务器的连接通道,当本次请求需要的数据完毕 ...

  7. ImageField 字段的使用

    Django模型中的ImageField和FileField的upload_to选项是必填项,其存储路径是相对于MEIDA_ROOT而来的.

  8. kali网络源配置

    使用vim对sources.list文件进行修改: $   vim /etc/apt/sources.list 随便挑选一个源添加到该文件中 ----------------------------- ...

  9. 【leetcode】1012. Complement of Base 10 Integer

    题目如下: Every non-negative integer N has a binary representation.  For example, 5 can be represented a ...

  10. Spring Security 安全认证

    Spring Boot 使用 Mybatis 依赖 <dependency> <groupId>org.mybatis.spring.boot</groupId> ...