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. 1 2
Output
  1. 2
Input
  1. 2 3
Output
  1. 5
Input
  1. 3 6
Output
  1. 10
Input
  1. 10 1024
Output
  1. 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.

  1. #include <cstdio>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <set>
  8. #include <vector>
  9. #include <sstream>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <fstream>
  13. typedef long long ll;
  14. using namespace std;
  15. //freopen("D.in","r",stdin);
  16. //freopen("D.out","w",stdout);
  17. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  18. #define maxn 100001
  19. const int inf=0x7fffffff; //无限大
  20. int main()
  21. {
  22. ll h,n;
  23. cin>>h>>n;
  24. ll t=;
  25. for (int i=;i<h;i++)
  26. t*=;
  27. ll ans=;
  28. int hehe=;
  29. for(int i=;i<h;i++) {
  30. t/=;
  31. if(n>t)
  32. {
  33. if (hehe==)
  34. ans+=t*;
  35. else
  36. ans++;
  37. n-=t,hehe=;
  38. } else
  39. {
  40. if(hehe==)
  41. ans+=t*;
  42. else
  43. ans++;
  44. hehe=;
  45. }
  46. }
  47. cout<<ans<<endl;;
  48. return ;
  49. }

Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 水题的更多相关文章

  1. Codeforces Round #368 (Div. 2) A. Brain's Photos (水题)

    Brain's Photos 题目链接: http://codeforces.com/contest/707/problem/A Description Small, but very brave, ...

  2. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题

    C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...

  3. Codeforces Round #185 (Div. 2) A. Whose sentence is it? 水题

    A. Whose sentence is it? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  4. Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题

    A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...

  5. Codeforces Round #371 (Div. 2) A. Meeting of Old Friends 水题

    A. Meeting of Old Friends 题目连接: http://codeforces.com/contest/714/problem/A Description Today an out ...

  6. Codeforces Round #355 (Div. 2) B. Vanya and Food Processor 水题

    B. Vanya and Food Processor 题目连接: http://www.codeforces.com/contest/677/problem/B Description Vanya ...

  7. Codeforces Round #310 (Div. 2) B. Case of Fake Numbers 水题

    B. Case of Fake Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

  8. Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题

    B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...

  9. Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题

    A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...

随机推荐

  1. 两个Bounding Box的IOU计算代码

    Bounding Box的数据结构为(xmin,ymin,xmax,ymax) 输入:box1,box2 输出:IOU值 import numpy as np def iou(box1,box2): ...

  2. Java不为人知的小秘密

    Java中的main方法必须有一个外壳类,而且必须是静态的! Java中的所有函数都属于某个类的方法,所以main方法也不例外,必须放在一个类中才能编译运行. 例如: public class tex ...

  3. Mac 升级一次,php 就崩溃一次,有味,苹果....

    Mac升级系统macOS Sierra后PHP不编译 Mac下搭建PHP开发环境(Apache+PHP+MySQL+phpMyAdmin),当Mac 从OS 10.11升级至macOS Sierra( ...

  4. Java编程思想第四版第二章练习题答案

    练习1:创建一个类,它包含一个int域和一个char域,它们都没有被初始化.将他们的值打印出来,以验证Java执行了默认初始化 public class JavaThinking { private ...

  5. 两种方法设置nginx并发限制下面的白名单策略

    前言: 今天,公司主站突然出现IDE创建应用没反应的问题,经过预发布环境.非代理环境下面的服务测试,均没有问题,定位问题出现在前端.而我们前端有两层代理,一是青松抗D系统,一是我们自己的nginx代理 ...

  6. USACO 4.3 Buy Low, Buy Lower

    Buy Low, Buy Lower The advice to "buy low" is half the formula to success in the stock mar ...

  7. Warning -27077: The "vuser_init" section contains web function(s) when the "Simulate a new user on each iteration" Run-Time Setting is ON.

    通过LR来录制登录过程并生成脚本,设置了自动关联,并回放录制脚本,观察回放日志发现没有报error信息,说明脚本没有问题,将脚本放入Controller中设置100个用户设置运行,发现运行一段时间开始 ...

  8. HDU - 4420 2013icpc长春A 函数离散化 + st表

    思路:我们定义F(x) 为以x点为起点,向后(a - b)个里面有多少个白球,虽然x的范围是LL范围内的,但是白球的 个数只有1e5, 那么我们可以把连续一段相同的离散化到一起, 对于一个确定的长度为 ...

  9. Pycharm 激活码(转) 有效期到2019/10月

    Pycharm 激活码(转) 有效期到2019/10月 2018年11月13日 17:15:32 may_ths 阅读数:64   [激活码激活] 修改hosts文件 添加下面一行到hosts文件,目 ...

  10. 自然语言处理系列-4条件随机场(CRF)及其tensorflow实现

    前些天与一位NLP大牛交流,请教其如何提升技术水平,其跟我讲务必要重视“NLP的最基本知识”的掌握.掌握好最基本的模型理论,不管是对日常工作和后续论文的发表都有重要的意义.小Dream听了不禁心里一颤 ...