Codeforces Round #287 (Div. 2) C. Guess Your Way Out!
2015-01-24 15:09
417 查看
原题链接:
http://codeforces.com/contest/507/problem/C
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
output
input
output
input
output
input
output
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.
![](http://espresso.codeforces.com/2e42de5cebf0bfdae7dd1e2ecf97673b77881867.png)
思路:大概就是每次判断每往下一层是否需要翻过(之前需要走)另外一边的子树。
代码:
http://codeforces.com/contest/507/problem/C
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.
![](http://espresso.codeforces.com/2e42de5cebf0bfdae7dd1e2ecf97673b77881867.png)
思路:大概就是每次判断每往下一层是否需要翻过(之前需要走)另外一边的子树。
代码:
#include "stdio.h" #include "iostream" #include "string.h" #include "stdlib.h" #include "algorithm" using namespace std; long long n,ans=0,b=0,p[51]; int h; void getp() { p[0]=1; for(int i=1;i<=50;i++) p[i]=p[i-1]*2; } int main() { getp(); scanf("%I64d%I64d",&h,&n); for(int i=h;i>0;i--) { if(n<=p[i-1]) { ans++; n=p[i-1]-n+1; } else { ans+=p[i]; n-=p[i-1]; } } printf("%I64d\n",ans); return 0; }
相关文章推荐
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 数学
- 【codeforces】 Round #287 (Div. 2) C. Guess Your Way Out!
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! ACM解题报告(二叉树)
- C. Guess Your Way Out!(Codeforces Round#287(div2))
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out!
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out!+二叉树
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 思路
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II (求区间的补、交)
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 水题
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out!
- Codeforces Round #287 (Div. 2)-Guess Your Way Out!
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 二叉树遍历
- Codeforces Round #287 (Div. 2)C. Guess Your Way Out!
- Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II 贪心排序
- Codeforces Round #287 (Div. 2)C. Guess Your Way Out!
- CF #287 div2 C Guess Your Way Out!
- Codeforces Round #287 (Div. 2) C. Guess Your Way Out!(线段树思想)
- CF 558D(Guess Your Way Out! II-set解决区间问题)
- CodeForces_507C_Guess Your Way Out!(规律)