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

來源:互聯網
上載者:User

標籤:

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

題意:給出n,給出一顆高度為h+1的完全二叉樹,對於每一行的元素左到右,從1到2^h標號。剛開始位於root,然後按左右左右交替的走。
1.如果當前目的地走過了,他會跳過當前指令.
2.如果連續的跳過兩條指令,他會回到父節點.
3.如果到達一個不存在的點,他會回到父節點
4.到達標號為n的點,他就贏了.
求中途經過點的個數。
動手畫畫,可以發現,如果他是由父親直接走過來,他經過的點的個數就是經過他父親的個數加1.否則就是他的兄弟節點走完,在走完父親節點在加1.因為要求的是途中經過,最後要減去本身的1個點。然後遞迴求解即可。注意從第一層到第二層,奇偶性相同的是直接由父親走過去,其他的則是有奇偶性不同的直接走過去。

#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <set>#include <bitset>#include <queue>#include <stack>#include <string>#include <iostream>#include <cmath>#include <climits>using namespace std;typedef long long LL;LL h;LL cifang(LL x,LL y){    LL ans =1;    for(LL i = 0;i<y;i++)        ans*=x;    return ans;}LL gao(LL x,LL y){    LL t = (y+1)/2;    if(x==1) return 1;    if(x==2){        if(y==1){            return 2;        }        else return cifang(2,h-x+1) + 1;    }    if(t%2==y%2){        LL k = cifang(2,h-x+1)+gao(x-1,t);        return k;    }    else return 1+gao(x-1,t);}int main(){    LL n;    cin>>h>>n;    h++;    LL ans = 100;    for(LL i = 1;i<=h;i++){        LL t = cifang(2,i-1);        if(t>=n){            ans = i;break;        }    }    LL Min = (1LL<<60);   // for(LL i = ans;i<=h;i++){     //   Min = min(Min,gao(i,n));        //cout<<i<<" "<<Min<<endl;    //}    cout<<gao(h,n)-1<<endl;    return 0;}

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.