Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 数学

原创 2015年07月10日 10:29:24

C. Guess Your Way Out!
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.
题意是,有一个完全二叉树,树的叶子结点从1排到2^h,给一段序列lrlrlr….这样交替,l表示向左走,r表示向右走,如果到叶子结点,返回父结点如果,已经访问过,就不动,若有两次不动,则返回父结点,最后,给出目标结点,问需要多少次可以到达目标结点。
画个图就可以发现,如果前一个是l这步是r,则要加1,如果前一个是r这步是l,则要加2^h,所以只需要求出每一位,与前一位比较奇偶性,如果同奇偶加2^h,否则加1

#define INF         9000000000
#define EPS         (double)1e-9
#define mod         1000000007
#define PI          3.14159265358979
//*******************************************************************************/
#endif
#define N 55
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
ll h,n,ten[N],ans[N],ansi,sum;
int main()
{
    ten[0] = 1;
    for(int i = 1;i<N;i++)
    ten[i] = ten[i-1] * 2;
    while(cin>>h)
    {
        cin>>n;
        ansi = 0;
        ll nn = ten[h] + n - 1;
        while(nn){
            ans[ansi++] = nn;
            nn/=2;
        }
        nn = n;sum = 0;
        bool flag = true;
        //cout<<ans[ansi-1]<<"  ";
        for(int i=ansi-2;i>=0;i--){
            //cout<<ans[i]<<"  ";
            sum+= (ans[i+1] & 1) == (ans[i] & 1)? ten[h]:1;
            h--;
        }
        cout<<sum<<endl;
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

原题链接: http://codeforces.com/contest/507/problem/C C. Guess Your Way Out! time limit per...

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

题意: 告诉你完全二叉树的高度以及它所要求的结点的位置 始终满足左右左右顺序走,不能走就返回父亲结点 问:到达给出的结点位置之前走过了几个结点 解题思路: 根据样例解释可以发现,如果...

C. Guess Your Way Out!(Codeforces Round#287(div2))

C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes ...
  • caduca
  • caduca
  • 2015年01月24日 21:19
  • 3109

Codeforces Round #312 (Div. 2) D. Guess Your Way Out! II

题意:有一棵高为h的树,出口在叶子节点上,n代表这棵树的高度,q代表q个判断,判断的样式是,i,L,R,ans,判断出口在第i层的祖宗节点是否在(L,R)中,ans == 0 代表不在,ans == ...
  • NMfloat
  • NMfloat
  • 2015年10月30日 13:52
  • 209

Codeforces 507C Guess Your Way Out!【思维】

C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes i...

Codeforces 558D Guess Your Way Out! II (简单题)

题目类型 简单题 题目意思 有一棵高度最多 50 的完美二叉树, 其中只有一个叶子结点是出口 给出最多 1e5 个说明,每个操作包含 4 个参数 i, L, R, ans 当 ans ==...

Codeforces Round #287 (Div. 2) C

507C - Guess Your Way Out!         题意:遍历一棵满二叉树,遍历方法是左右左右递归地遍历。树高度为h,最下层第n个节点是出口,问访问到出口前要访问多少个节点。 ...

CF C.Guess Your Way Out!

C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes i...

Codeforces Round #287 (Div. 2) C. (构造)

Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the ...

Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序, 贪心, 暴力(居然可以n方暴力)

题目链接: Hacker, pack your bags!题目大意有x天的假期, 有n张旅行票, 每张票有起始时间l, 结束时间r, 花费cost, 想把假期分成两部分出去旅游, 两部分时间不能重合,...
  • litmxs
  • litmxs
  • 2017年07月03日 12:09
  • 553
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 数学
举报原因:
原因补充:

(最多只允许输入30个字)