CF C.Guess Your Way Out!

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.


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;

__int64 pow1(__int64 x){
 __int64 s;
 s = 1;
 for(int i =1;i<=x;i++)
 s*=2;
 return s;
}
int main(){
 
 int f,f2;
 __int64 n,m;
 
 while(~scanf("%I64d%I64d",&n,&m)){
  
  f = 1;
     __int64 s;
     __int64 d;
     s = pow1(n+1)-1;
  while(n){
   d = pow1(n-1);
   //printf("%这一次的中点%I64d",d);
   if(m<=d){
    f2 = -1;
    //printf("左\n");
   }
   else{
    m-=d;
    f2 = 1;
    //printf("右\n");
   }
   if(f*f2<0)s-=pow1(n)-1;
   f = f2;
   n--;
  }
  printf("%I64d\n",s-1);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERROR in ./src/views/User.vue?vue&type=script&lang=js (./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/User.vue?vue&type=script&lang=js) Module build failed (from ./node_modules/vue-loader/dist/index.js): TypeError: Cannot read properties of null (reading 'content') at selectBlock (D:\Wecat\WeChat Files\wxid_drwgcpvvtvz322\FileStorage\File\2023-07\综合项目\vue-demo\node_modules\vue-loader\dist\select.js:23:45) at Object.loader (D:\Wecat\WeChat Files\wxid_drwgcpvvtvz322\FileStorage\File\2023-07\综合项目\vue-demo\node_modules\vue-loader\dist\index.js:92:41) @ ./src/views/User.vue?vue&type=script&lang=js 1:0-196 1:0-196 1:197-382 1:197-382 @ ./src/views/User.vue 2:0-55 3:0-50 3:0-50 6:49-55 @ ./src/router/index.js 27:21-47 @ ./src/main.js 4:0-30 18:7-13 ERROR in ./src/views/User.vue?vue&type=template&id=e0b47cf6 (./node_modules/babel-loader/lib/index.js??clonedRuleSet-40.use[0]!./node_modules/vue-loader/dist/templateLoader.js??ruleSet[1].rules[3]!./node_modules/vue-loader/dist/index.js??ruleSet[0].use[0]!./src/views/User.vue?vue&type=template&id=e0b47cf6) Module Error (from ./node_modules/vue-loader/dist/templateLoader.js): [vue/compiler-sfc] Unexpected keyword 'const'. (6:8) D:\Wecat\WeChat Files\wxid_drwgcpvvtvz322\FileStorage\File\2023-07\综合项目\vue-demo\src\views\User.vue 127| data(){ 128| return{ 129| const options = [ | ^ 130| { 131| value: 'Option1', @ ./src/views/User.vue?vue&type=template&id=e0b47cf6 1:0-265 1:0-265 @ ./src/views/User.vue 1:0-65 6:68-74 16:64-18:3 17:29-35 16:2-18:4 @ ./src/router/index.js 27:21-47 @ ./src/main.js 4:0-30 18:7-13
最新发布
07-25

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值