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. ES6——函数-参数

    函数的参数: 1.参数扩展/数组展开      1)收集(剩余的)参数          function show(a,b,...args){}   // 三点运算符           *Rest ...

  2. webpack的理解、总结

    weabpck的基础应用 https://blog.zhangjd.me/2016/06/19/webpack-your-bags/ https://juejin.im/post/5cc26dfef2 ...

  3. 创建一个java项目并部署到weblogic服务器

    转自:https://blog.csdn.net/krystal_sl/article/details/52847953 新建一个项目的步骤 打开eclipse,右键点击new–>java pr ...

  4. 03.LNMP架构-PHP源码包编译部署详细步骤

    一.环境准备 操作系统:CentOS_Server_7.5_x64_1804.iso 部署组件:yasm+libmcrypt+libvpx+tiff+libpng+freetype+jpeg+libg ...

  5. CS184.1X 计算机图形学导论(第三讲)

    第一单元(介绍关于变换的数学知识) :基本二维变换 模型坐标系,世界坐标系 1.缩放 Scale(规模,比例) Sx表示在x方向上放大的倍数,Sy表示在y方向上放大的倍数,因此X坐标乘以Sx,Y坐标乘 ...

  6. fastjson 1.1.1填坑

    java封装Echart数据模型 xAxis  yAxis 属性  转json赋值失败..换名即可 解决办法:升级1.2.2后即可

  7. poj 2505 乘法博弈论

    转自:http://hzwer.com/1921.html 题目大意: 题意:Stan从1开始,可以乘上2~9中任何一个数,Ollie也如此操作,只到某个人本回合的操作超过N为之..1<N< ...

  8. ajax使用jsonp请求方式

    /* //简写形式,效果相同 $.getJSON("http://app.example.com/base/json.do?sid=1494&busiId=101&jsonp ...

  9. boost serialization

    Archive An archive is a sequence of bytes that represented serialized C++ objects. Objects can be ad ...

  10. jmeter+ant+jenkins搭建接口自动化测试环境

    jmeter+ant+jenkins搭建接口自动化测试环境(基于win) 1.jmeter jmeter依赖java运行环境,所以需要提前下载jdk并配置好环境变量 官网下载(http://jmete ...