#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
long long h,n;
long long DFS(long long p, long long n, int sign){
if(p == h) return 0;
if(!sign && n > (1LL << (h - p - 1)))//规则向左 应该向右 整个左子树都要被遍历+下一层继续向左搜索
return ((1LL << (h - p)) + DFS(p + 1, n - (1LL << (h - p - 1)), sign));
if(!sign && n <= (1LL << (h - p - 1)))//规则向左 应该向左 加1继续向右搜索
return (DFS(p + 1, n, !sign) + 1);
if(sign && n <= (1LL << (h - p - 1)))
return (DFS(p + 1, n, sign) + (1LL << (h - p)));
if(sign && n > (1LL << (h - p - 1)))
return (DFS(p + 1, n - (1LL << (h - p -1)), !sign) + 1);
}
int main(){
scanf("%I64d%I64d",&h,&n);
printf("%I64d\n",DFS(0, n, 0));
}
C. Guess Your Way Out!
最新推荐文章于 2019-08-30 16:10:56 发布