首页 > 其他分享 >Codeforces Round #287 (Div. 2)-Guess Your Way Out!

Codeforces Round #287 (Div. 2)-Guess Your Way Out!

时间:2023-06-12 17:37:45浏览次数:47  
标签:node Guess ch Codeforces exit ans input Round he


原题链接


C. Guess Your Way Out!



time limit per test



memory limit per test



input



output


h. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.

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!

LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:

  • L' means "go to the left child of the current node";
  • 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



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.


Examples



input



1 2



output



2



input



2 3



output



5



input



3 6



output



10



input



10 1024



output



2046


Note



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

3. Nodes are labeled according to the order of visit.


Codeforces Round #287 (Div. 2)-Guess Your Way Out!_#define


若树的高度是h, 则叶子的数目为 p = 1<<h, 出口的位置为n,现在要往ch方向走(ch = 'l'或'r'), ans为走的步数

1.若n <= p && ch == 'r'(出口在左子树上要往右走), ans += p(右子树的节点个数加上一个根节点), 现在走到了根节点的左孩子上,ch == 'r'

2.若n > p && ch == 'l'(出口在右子数上要往左走), ans +=p(左字数的节点个数加上一个根节点), 现在走到了根节点的右孩子上, ch == 'l', n -= p

3.(n <= p && ch == 'l') || (n > p && ch == 'r')时,走一步ans++, s = 'l' + 'r', ch = s - ch, (若n >p 则 n -= p)

直到走到叶子节点输出ans


#include <bits/stdc++.h> 
#define maxn 100005
using namespace std;
typedef long long ll;

int main(){
	
//	freopen("in.txt", "r", stdin);
	ll h, n, ans = 0;
	int d = 0;
	char ch = 'l', s = 'l' + 'r';
	
	scanf("%I64d%I64d", &h, &n);
	
	while(d < h){
		
		ll p = (ll)1 << (h-d);
		p /= 2;
		if((n <= p && ch != 'l') || (n > p && ch != 'r')){
			ans += p * 2;
			if(n > p)
			 n -= p;
			ch = s - ch; 
		}
		else{
			ans++;
			if(n > p)
			 n -= p;
		}
		ch = s - ch;
	    d++;
	}
	printf("%I64d\n", ans);
	
	return 0;
}




标签:node,Guess,ch,Codeforces,exit,ans,input,Round,he
From: https://blog.51cto.com/u_16158872/6464257

相关文章