Problem:
 Score : 400 points    Problem Statement In some other world, today is Christmas.  Mr. Takaha decides to make a multi-dimensional burger in his party. A level- L burger ( L is an integer greater than or equal to 0 ) is the following thing:   A level- 0 burger is a patty.  A level- L burger (L \geq 1) is a bun, a level- (L-1) burger, a patty, another level- (L-1) burger and another bun, stacked vertically in this order from the bottom.   For example, a level- 1 burger and a level- 2 burger look like BPPPB and BBPPPBPBPPPBB (rotated 90 degrees), where B and P stands for a bun and a patty.  The burger Mr. Takaha will make is a level- N burger. Lunlun the Dachshund will eat X layers from the bottom of this burger (a layer is a patty or a bun). How many patties will she eat?      Constraints  1 \leq N \leq 50  1 \leq X \leq ( the total number of layers in a level- N burger )  N and X are integers.         Input Input is given from Standard Input in the following format:  N  X       Output Print the number of patties in the bottom-most X layers from the bottom of a level- N burger.        Sample Input 1 2 7      Sample Output 1 4  There are 4 patties in the bottom-most 7 layers of a level- 2 burger ( BBPPPBPBPPPBB ).       Sample Input 2 1 1      Sample Output 2 0  The bottom-most layer of a level- 1 burger is a bun.       Sample Input 3 50 4321098765432109      Sample Output 3 2160549382716056  A level- 50 burger is rather thick, to the extent that the number of its layers does not fit into a 32 -bit integer.

Solution (Python):
layers = [1] * 51
patties = [1] * 51
for i in range(1, 51):
    layers[i] = 3 + layers[i-1] * 2
    patties[i] = 1 + patties[i-1] * 2


def count_patties(l, x):
    if l == 0 and x == 1:
        return 1
    if x <= 1:
        return 0
    y = layers[l-1]
    if x <= y+1:
        return count_patties(l-1, max(x-1, 0))
    else:
        return count_patties(l-1, min(x-y-2, y)) + patties[l-1] + 1


def main():
    N, X = map(int, input().split())
    print(count_patties(N, X))


if __name__ == "__main__":
    main()

Solution (Python):
n, x = map(int, input().split())

plist = [1]
bplist = [1]

for i in range(n):
    p = plist[i] + 2 ** (i+1)
    plist.append(p)

    bp = bplist[i] + 2 ** (i+2)
    bplist.append(bp)

ans = 0
index = n

while True:
    if index == 0:
        ans += 1
        break
    elif x > bplist[index-1] + 2:
        ans += plist[index - 1] + 1
        x -= bplist[index - 1] + 2
        index -= 1
    elif x == 1:
        break
    elif x == bplist[index-1] + 2:
        ans += plist[index-1] + 1
        break
    else:
        x -= 1
        index -= 1

print(ans)

Solution (Python):
n, x = map(int, input().split())

num = [2 ** (i + 2) - 3 for i in range(n+1)]
pnum = [2 ** (i+1) - 1 for i in range(n+1)]

def ans(m, y):
  if m == 0:
    return 1
  elif y == 1:
    return 0
  elif y < 2 ** (m + 1) - 1:
    return ans(m - 1, y - 1)
  elif y == 2 ** (m+1) -1:
    return pnum[m-1] + 1
  elif y != num[m]:
    r = y - (2 ** (m+1)-1)
    return pnum[m-1] + 1 + ans(m-1, r)
  else:
    return pnum[m]

print(ans(n, x))

Solution (Python):
N, X = [int(_) for _ in input().split()]

def solve(n, x):

    if n == 0:
        return 1

    left = 1
    if x == left:
        return 0

    right = 2 ** (n + 2) - 3
    pivot = right // 2 + 1

    if x == right:
        return 1 + 2 * solve(n - 1, pivot - 2)

    if x < pivot:
        return solve(n - 1, x - 1)

    elif x == pivot:
        return 1 + solve(n - 1, pivot - 2)

    else:
        return solve(n - 1, pivot - 2) + 1 + solve(n - 1, x - pivot)




print(solve(N, X))

Solution (Python):

N, X = map(int, input().split())
MEMO = {}


def f(n):
    if n in MEMO:
        return MEMO[n]
    if n == 0:
        return 1
    MEMO[n] = f(n - 1) * 2 + 3
    return MEMO[n]


MEMO2 = {}


def g(n, i):
    if i > f(n):
        i = f(n)
    if (n, i) in MEMO2:
        return MEMO2[(n, i)]
    if n == 0:
        return 1
    ans = 0
    if i > 1:
        ans += g(n - 1, i - 1)
    if i >= 1 + f(n - 1) + 1:
        ans += 1
    if i > 1 + f(n - 1) + 1:
        ans += g(n - 1, i - 1 - f(n - 1) - 1)
    MEMO2[(n, i)] = ans
    return ans


print(g(N, X))

Solution (Python):
nn,x=map(int,input().split())
ns=[1 for i in range(51)]
nm=[1 for i in range(51)]
for i in range(1,51):
    ns[i]=2*ns[i-1]+3
    nm[i]=2*nm[i-1]+1
def np(n,w):
    if w==0:
        return 0
    elif n==1:
        if w==1:
            return 0
        elif w==2:
             return 1
        elif w==3:
            return 2
        else:
            return 3
    else:
        if ns[n]//2+1>w:
            return np(n-1,w-1)
        elif ns[n]//2+1==w:
            return nm[n-1]+1
        else:
            return np(n-1,w-ns[n-1]-2)+nm[n-1]+1
print(np(nn,x))

Solution (Python):
def subsolve(n, x):
    if(n == 0 and x == 1):
        return 1
    if(x <= 1): 
        return 0
    L1 = (1 << (n + 1)) - 3
    Ps = (1 << n) - 1
    if(x >= 2 * L1 + 2): 
        return 2 * Ps + 1
    if(x > L1 + 2): 
        return Ps + subsolve(n - 1, x - L1 - 2) + 1
    if(x == L1 + 2): 
        return Ps + 1
    return subsolve(n - 1, x - 1) 
if __name__ == '__main__':
    n, x = map(int, input().split())
    print(subsolve(n, x))

Solution (Python):
n,x = map(int,input().split())

b = [1]
l = [1]
for i in range(n):
    b.append(b[-1]*2+1)
    l.append(l[-1]*2+3)




x -= 1
res = 0
lv = n
while lv >= 0:
    
    if lv == 0:
        res += 1
        break
    elif x <= 0:
        break
    elif x >= l[lv]-1:
        res += b[lv]
        break
    elif x == l[lv] // 2:
        res += b[lv-1] + 1
        break
    elif x > l[lv] // 2:
        res += b[lv-1] + 1
        lv -= 1
        x -= l[lv] + 2
    else:
        lv -= 1
        x -= 1
print(res)

Solution (Python):
import sys


def num_patty(level):
    return 2 ** (level + 1) - 1


def num_all(level):
    return 2 ** (level + 2) - 3


def total_patty(n, x):
    res = 0
    pos = x - 1
    for cur_level in range(n + 1)[::-1]:
        print("L = {}, P = {}, R = {}: ".format(cur_level, pos, res), file=sys.stderr, end="")
        if cur_level == 0:
            print("level zero", file=sys.stderr)
            res += 1
            break
        prev_level_total = num_all(cur_level - 1)
        cur_level_total = num_all(cur_level)
        if pos == 0:
            print("left bread", file=sys.stderr)
            break
        elif 1 <= pos <= prev_level_total:
            print("left block", file = sys.stderr)
            pos -= 1
            continue
        elif pos == prev_level_total + 1:
            print("middle patty", file = sys.stderr)
            res += num_patty(cur_level - 1) + 1
            return res
        elif prev_level_total + 2 <= pos <= cur_level_total - 2:
            print("right block", file = sys.stderr)
            res += num_patty(cur_level - 1) + 1
            pos -= prev_level_total + 2
            continue
        else:
            print("right bread", file = sys.stderr)
            res += num_patty(cur_level)
            break
    return res


def main():
    n, x = (int(z) for z in input().split())
    res = total_patty(n, x)
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
def search(x,L,ans):
    if x<=1:
        return ans
    elif 1<x and x<=2**(L+1)-3:
        return search(x-1,L-1,ans)
    elif x==2**(L+1)-2:
        ans+=2**L-1
        return ans
    elif x==2**(L+1)-1:
        ans+=2**L
        return ans
    elif 2**(L+1)-1<x and x <= 2**(L+2)-5:
        ans+=2**L
        return search(x-2**(L+1)+1,L-1,ans)
    else:
        ans+=2**(L+1)-1
        return ans

N,X=list(map(int,input().split()))

ans=search(X,N,0)
print(ans)

Solution (Python):
N, X = map(int, input().split())

f = [[0, 0] for i in range(N + 1)]


f[0] = [1, 1]
for i in range(1, N + 1):
    
    f[i][0] = 2 * f[i - 1][0] + 3
    f[i][1] = 2 * f[i - 1][1] + 1
    

"""
for i in range(0, N + 1):
    print(f[i][0], f[i][1])
"""

leftA = 0
leftB = 0

ans = 0

for i in range(N, 0, -1):
    if leftA + 1 == X:
        
        ans = leftB
        break
    elif leftA + 1 + f[i-1][0] == X:
        
        ans = leftB + f[i-1][1]
        break
    elif leftA + 1 + f[i-1][0] > X:
        leftA = leftA + 1
    elif leftA + 1 + f[i-1][0] + 1 == X:
        
        ans = leftB + 1 + f[i-1][1]
        break
    elif leftA + 1 + f[i-1][0] + 1 + f[i-1][0] == X:
        
        ans = leftB + 1 + 2 * f[i-1][1]
        break
    elif leftA + 1 + f[i-1][0] + 1 + f[i-1][0] > X:
        leftA = leftA + 1 + f[i-1][0] + 1
        leftB = leftB + 1 + f[i-1][1]
    elif leftA + 1 + f[i-1][0] + 1 + f[i-1][0] + 1 == X:
        
        ans = leftB + 1 + 2 * f[i-1][1]
        break
    

print(ans)

Solution (Python):
N,X = map(int,input().split(' '))

'''
N = 2
X = 7
'''

def ban(n):
	if n == 0:
		return 'P'
	return 'B'+ban(n-1)+'P'+ban(n-1)+'B'

def ans(n,x):
	if x <= 0:
		return 0
	if n == 0:
		return 1
	s = 2**(n+2) - 3
	a = (s-3)//2
	if x < 2+a:
		return ans(n-1,x-1)
	if x == 2+a:
		return ans(n-1,x-1)+1
	p = 2**(n)-1
	return p + 1+ans(n-1,x-2-a)

print(ans(N,X))

Solution (Python):

def inpl(): return list(map(int, input().split()))


N, X = inpl()

def main(N, X):
    B = [0]
    P = [1]
    T = [1]
    
    for i in range(50):
        b, p, t = B[-1], P[-1], T[-1]
        B.append(2*b + 2)
        P.append(2*p + 1)
        T.append(2*t + 3)
    
    ans = 0
    for n in range(N, 0, -1):
        if X <= T[n-1]:
            X -= 1
        elif X <= T[n-1] + 2:
            ans += P[n-1] + (X == T[n-1]+2)
            break
        elif X < 2*T[n-1] + 2:
            ans += P[n-1] + 1
            X -= (T[n-1] + 2)
        else:
            ans += 2*P[n-1] + 1
            break
    return ans

print(main(N, X))

Solution (Python):



n, x = (int(x) for x in input().split())

f = [0] * (n+1)
g = [0] * (n+1)
f[0] = 1
g[0] = 1
for i in range(1, n+1):
    f[i] = f[i-1]* 2 + 3
    g[i] = g[i-1]* 2 + 1

total = 0
remain = x

def eat(n):

    global total
    global f
    global remain

    if remain == 0:
        return
    if n < 0:
        return
    if f[n] <= remain:
        total += g[n]
        remain -= f[n]
        return
    if f[n] > remain:
        remain -=1
        if remain == 0:
            return
        if f[n-1] > remain:
            eat(n-1)
        else:
            total += g[n-1]
            remain -= f[n-1]
            if remain == 0:
                return
            total += 1
            remain -= 1
            eat(n-1)

eat(n)
print(total)

Solution (Python):
def kengo(n, x):
    if n == 0:
        if x == 0:
            return 0
        else:
            return 1
    if x == 0:
        return 0
    if x <= 2 ** (n + 1) - 2:
        return kengo(n - 1, x - 1)
    if x <= 2 ** (n + 2) - 4:
        return 2 ** n + kengo(n - 1, x - 2 ** (n + 1) + 1)
    else:
        return 2 ** (n + 1) - 1

n, x = map(int, input().split())
print(kengo(n, x))

Solution (Python):
n,x = map(int,input().split())
ans =0
while x > 0:
    if n==0:
        ans +=1
        break
    elif x == 1:
        break
    elif x >=2**(n+1)-1:
        ans += 2**n
        x -=2**(n+1)-1
        n -=1
    elif x == 2**(n+1)-2:
        ans += 2**n-1
        print(ans)
        exit()
    else:
        n -=1
        x -=1
print(ans)

Solution (Python):
N, X = map(int, input().split())
A = [1] * 55
memo = {}


def func(n, x):
    if x <= 0:
        return 0
    if n == 0:
        return 1
    if (n, x) in memo:
        return memo[(n, x)]

    if x == 1:
        return 0
    if 1 < x <= 1 + A[n-1]:
        memo[(n, x)] = func(n-1, x-1)
        return memo[(n, x)]
    if x == 2 + A[n-1]:
        memo[(n, x)] = func(n-1, x-1) + 1
        return memo[(n, x)]
    if 2 + A[n-1] < x < A[n]:
        memo[(n, x)] = func(n-1, A[n-1]) + func(n-1, x-A[n-1]-2) + 1
        return memo[(n, x)]
    else:
        memo[(n, x)] = 2 * func(n-1, A[n-1]) + 1
        return memo[(n, x)]


def main():
    for i in range(1, N+1):
        A[i] = 2 * A[i-1] + 3

    print(func(N, X))


if __name__ == '__main__':
    main()

Solution (Python):
a=[1]
b=[1]
for i in range(50):
    a.append(3+2*a[-1])
    b.append(1+2*b[-1])
def paty(n,x):
    if n==1:return 'bpppb'[-x:].count('p')
    if x==a[n]:return b[n]
    if x==1:return 0
    if x==a[n]//2+1: return b[n-1]+1
    if x<a[n]//2+1: return paty(n-1,x-1)
    if x>a[n]//2+1: return b[n-1]+1+paty(n-1,x-a[n]//2-1)
n,x=map(int, input().split())
print(paty(n,x))

Solution (Python):

def count(ps, N, X):
    if X ==0:
        return 0
    total = ps[N][0]
    
    half = (total +1)//2
    if X >= total:
        return ps[N][1]
    elif X < half:
        return count(ps, N-1,X-1)
    elif X == half:
        return ps[N-1][1] + 1
    elif X > half:
        return (ps[N-1][1] + 1) + count(ps, N-1, X - half)


def main(N,X):
    ps = [(1, 1)] 
    for i in range(1, N+1):
        ps.append((ps[i-1][0] * 2 + 3, ps[i-1][1] * 2 + 1))

    c = count(ps, N, X)
    print(c)
    return c




N, X = (int(x) for x in input().split())
main(N, X)

Solution (Python):
(N, X) = map(int, input().split())

l = [0 for i in range(N+1)]
l[0] = 1
p = [0 for i in range(N+1)]
p[0] = 1
for i in range(1, N+1):
    l[i] = 2*l[i-1]+3
    p[i] = 2*p[i-1]+1

x = X
d = N
ans = 0
while True:
    if x == 0:
        break
    if d == 0 and x != 0:
        ans += 1
        break

    m = (l[d]-1)//2
    if x < m:
        x = x - 1
    elif x == m:
        ans += p[d-1]
        break
    elif x > m:
        ans += p[d-1]+1
        x = x - (m+1)

    d -= 1

print(ans)

Solution (Python):
N, X = map(int, input().split())

burgersize, pnum = [1], [1]
for i in range(N):
    burgersize.append(burgersize[-1] * 2 + 3)
    pnum.append(pnum[-1] * 2 + 1)

def count_p(level, X):
    if level == 0:
        return 1
    elif X == burgersize[level]:
        return pnum[level]
    elif X == 1:
        return 0
    elif X < (burgersize[level] - 1) // 2 + 1:
        return count_p(level - 1, X - 1)
    elif X == (burgersize[level] - 1) // 2 + 1:
        return pnum[level - 1] + 1
    elif X > (burgersize[level] - 1) // 2 + 1:
        return pnum[level - 1] + 1 + count_p(level - 1, X - (burgersize[level] - 1) // 2 - 1)
    return None

ans = count_p(N, X)
print(ans)

Solution (Python):
n, x = list(map(int, input().split()))

def length_burger(k):
	if k == 0:
		return(1)
	else:
		return(2*length_burger(k-1) + 3)

def num_pate(k, s):
	if s <= 0:
		return 0
	elif k == 0:
		return 1
	else:
		middle = length_burger(k) // 2 + 1
		if s < middle:
			return num_pate(k - 1, s - 1)
		elif s == middle:
			return num_pate(k - 1, s - 2) + 1
		else:
			if s == length_burger(k):
				return 2 * num_pate(k - 1, middle - 2) + 1
			else:
				return num_pate(k - 1, middle - 2) + 1 + num_pate(k - 1, s - middle)

print(num_pate(n, x))

Solution (Python):
N,X = map(int,input().split())

s = [1]
p = [1]
for i in range(N):
    s.append(s[-1]*2 + 3)
    p.append(p[-1]*2 + 1)

ans = 0
for i in reversed(range(N)):
    if i > 0:
        if X == s[i]+2:
            ans += p[i]+1
            break
        elif X < s[i]+2:
            X -= 1
        else:
            X -= (s[i]+2)
            ans += p[i]+1
    else:
        ans += max(0,min(3, X-1))
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 9)

get_length = []
for i in range(50 + 1):
    get_length.append(1 + 4 * (2 ** i - 1))

get_p = []
t = 0
for i in range(50 + 1):
    t = t * 2 + 1
    get_p.append(t)


def dfs(kk, ss, tt):
    if ss > tt:
        return 0

    if kk == 0:
        return 1

    target_length = get_length[kk]
    m = (1 + target_length) // 2

    if 1 <= ss <= 2 and target_length - 1 <= tt <= target_length:
        return get_p[kk]

    if ss == 1:
        return dfs(kk, 2, tt)

    if tt == target_length:
        return dfs(kk, ss, tt - 1)

    if ss == m:
        return 1 + dfs(kk, ss + 1, tt)

    if tt == m:
        return dfs(kk, ss, tt - 1) + 1

    if ss < m < tt:
        return dfs(kk, ss, m - 1) + 1 + dfs(kk, m + 1, tt)

    if ss < m:
        return dfs(kk - 1, ss - 1, tt - 1)
    else:  
        return dfs(kk - 1, ss - m, tt - m)


n, x = map(int, input().split())
ans = 0
if get_length[n] < 2 * x:
    ans = get_p[n] - dfs(n, 1, get_length[n] - x)
else:
    ans = dfs(n, 1, x)
print(ans)

Solution (Python):
n, x = [int(i) for i in input().split()]

memo = dict()
size = [0] * (n+1)
size[0] = 1

for i in range(1, n+1):
    size[i] = size[i-1] * 2 + 3

def dfs(i):
    global x
    res = 0
    if x <= 0:
        return 0
    if size[i] <= x and i in memo:
        x -= size[i]
        return memo[i]
    if i == 0:
        x -= 1
        return 1
    x -= 1 
    res += dfs(i-1)
    x -= 1 
    if x < 0:
        return res
    res += 1 
    res += dfs(i-1)
    x -= 1 
    memo[i] = res
    return res


print(dfs(n))

Solution (Python):
n, x = map(int, input().split())

memol = {0: 1}
memop = {0: 1}


for i in range(1, n+1):
    memol[i] = memol[i-1] * 2 + 3
    memop[i] = memop[i-1] * 2 + 1

ret = 0
ln = 0
for i in range(n, -1, -1):
    if ln >= x:
        break
    ln += 1
    if ln >= x:
        break
    if ln + memol[i-1] > x:
        continue
    ln += memol[i-1]
    ret += memop[i-1]
    if ln == x:
        break
    ln += 1
    ret += 1
    if ln + memol[i-1] > x:
        continue
    ln += memol[i-1]
    ret += memop[i-1]
    if ln == x:
        break
    ln += 1

print(ret)

Solution (Python):
def func(N, X):
	global l, p

	if l[N] == X:
		return p[N]
	
	if X == 1:
		return 0
	temp = 1 + l[N - 1]
	s = 0
	if X <= temp:
		return func(N - 1, X - 1)
	temp += 1
	if X == temp:
		return p[N - 1] + 1
	temp += l[N - 1]
	if X <= temp:
		return func(N - 1, X - 2 - l[N - 1]) + p[N - 1] + 1
	return 2 * p[N - 1] + 1
	
		
N, X = map(int, input().split())

l = [0] * (N + 1)
p = [0] * (N + 1)

l[0] = 1
p[0] = 1

for i in range(1, N + 1):
	l[i] = 3 + l[i - 1] * 2
	p[i] = 1 + p[i - 1] * 2

ans = func(N, X)

print(ans)

Solution (Python):
N, X = map(int, input().split(' '))
length = [1]
patty = [1]
p_before = 1
before = 1
for i in range(N):
    before = before*2+3
    length.append(before)
    p_before = 2*p_before+1
    patty.append(p_before)

def search(n,i):
    if i==0:
        return 1
    if 1<n<length[i-1]+2:
        return search(n-1, i-1)
    elif length[i-1]+2<n<2*length[i-1]+3:
        return patty[i-1]+1+search(n-2-length[i-1],i-1)
    elif n == 1:
        return 0
    elif n ==length[i-1]+2:
        return patty[i-1]+1
    elif n == 2*length[i-1]+3:
        return 2*patty[i-1]+1

print(search(X,N))

Solution (Python):
N, X = map(int, input().split())
B = [0] * (N + 1)
P = [1] * (N + 1)
for i in range(N):
  B[i + 1] = 2 * B[i] + 2
  P[i + 1] = 2 * P[i] + 1
S = [B[i] + P[i] for i in range(N + 1)]
cnt = 0
def burg(L, X):
  cnt = 0
  if X <= L:
    return 0
  if L == 0:
    return 1
  if X <= S[L - 1] + 1:
    return burg(L - 1, X - 1)
  if X == S[L]:
    return P[L]
  else:
    return P[L - 1] + 1 + burg(L - 1, X - S[L - 1] - 2)
print(burg(N, X))

Solution (Python):
N, X = map(int,input().split())

a = [1 for i in range(N+1)]
p = [1 for i in range(N+1)]
for i in range(1,N+1):
	a[i] = 3 + 2 * a[i-1]
	p[i] = 1 + 2 * p[i-1]

def f(i,X):
	if i == 0 and X == 1:
		return 1
	elif X == 1:
		return 0
	elif X <= a[i-1]+1:
		return f(i-1,X-1)
	elif X == a[i-1]+2:
		return p[i-1]+1
	elif X <= 2*a[i-1]+2:
		return f(i-1,X-(a[i-1]+2)) + p[i-1] + 1
	elif X == 2*a[i-1]+3:
		return 2*p[i-1] + 1
print(f(N,X))

Solution (Python):
N, X = [int(_) for _ in input().split()]


def count(n, x):
    if [n, x] == [0, 1]:
        return 1
    elif x == 1:
        return 0
    elif x < 2 * 2 ** n - 1:
        return count(n - 1, x - 1)
    elif x == 2 * 2 ** n - 1:
        return count(n - 1, 2 * 2 ** n - 3) + 1
    elif x < 4 * 2 ** n - 3:
        return count(n - 1, 2 * 2 ** n - 3) + 1 + \
            count(n - 1, x - 2 * 2 ** n + 1)
    else:
        return 2 * count(n - 1, 2 * 2 ** n - 3) + 1


print(count(N, X))

Solution (Python):


def LBp(N,X,dLB):
    if N == 0 and X == 1:
        P = 1
    elif N >= 1:
        if X == 1:
            P = 0
        elif 2 <= X <= dLB[N-1]+1:
            P = LBp(N-1,X-1,dLB)
        elif X == dLB[N-1]+2:
            P = LBp(N-1,dLB[N-1],dLB) + 1
        elif dLB[N-1]+3 <= X <= dLB[N]-1:
            P = LBp(N-1,dLB[N-1],dLB) + LBp(N-1,X-dLB[N-1]-2,dLB) + 1
        elif X == dLB[N]:
            P = 2*LBp(N-1,dLB[N-1],dLB) + 1
    return P

def solve():
    N, X = map(int, input().split())
    dLB = [1]
    for i in range(1,N+1):
        dLB[i:] = [2*dLB[i-1]+3]
    res = LBp(N,X,dLB)
    return str(res)

if __name__ == '__main__':
    print(solve())

Solution (Python):
N, X = [int(a) for a in input().split()]
ans = 0
now_level = N
num_list = [2 ** (n + 2) - 3 for n in range(N + 1)]
while X > 0:
    if X <= now_level:
        break
    num_list = [2 ** (n + 2) - 3 for n in range(now_level + 1)]
    plus_base = [a + now_level - b for a, b in zip(num_list, range(now_level + 1))]
    eat_num = max([a for a in plus_base if a <= X])
    eat_level = plus_base.index(eat_num)
    now_level = eat_level
    ans += int((num_list[eat_level] + 1) / 2 + 0.1)
    X -= eat_num
    if X > 0:
        X -= 1
        ans += 1
print(str(ans))

Solution (Python):
def inpl(): return [int(i) for i in input().split()]

def alll(x):
    if not x:
        return 1
    return 3+2*alll(x-1)
def pate(n,x):
    if n == 0:
        return [0, 1][x == 1]
    result = 0
    if x == 0:
        return 0
    if x == alll(n):
        return 2**(n+1)-1
    if x == 2 + alll(n-1):
        return 2**n-1 + 1
    elif x > (alll(n)+1)//2:
        result += 2**n-1 + 1
        x -= (alll(n-1)+2)
    else:
        x -= 1
    return result + pate(n-1,x)      
N, X = inpl()
print(pate(N, X))

Solution (Python):
N,X=map(int, input().split())

def func(n,k):
    if n==0:
        return 1
    a=2**(n+1)-1
    if k==a:
        return 2**n
    elif k==1:
        return 0
    elif k==2**(n+2)-3:
        return 2**(n+1)-1
    elif k<a:
        return func(n-1,k-1)
    else:
        return 2**n+func(n-1,k-(2**(n+1)-1))
print(func(N,X))

Solution (Python):
N, X = map( int, input().split())
now = "P"
A = [1]*(N+1)
P = [1]*(N+1)
for i in range(N):
    A[i+1] = 2*A[i]+3
    P[i+1] = 2*P[i]+1
now = N
ans = 0
X -= 1
while now > 0:
    if A[now]//2 == X:
        ans += P[now-1]+1
        X = -1
        break
    elif A[now]//2 < X:
        ans += P[now-1]+1
        X -= A[now-1]+2
    else:
        X -= 1
    if X < 0:
        break
    now -= 1
if X >= 0:
    ans += 1
print(ans)

Solution (Python):
N,X=map(int,input().split())
a=[2**(i+2)-3 for i in range(N+1)]
p=[2**(i+1)-1 for i in range(N+1)]
def ans(n,x):
    if n==0:
        return 1
    if x<=1:
        return 0
    elif x<=2**(n+1)-2:
        return ans(n-1,x-1)
    elif x==2**(n+1)-1:
        return p[n-1]+1
    elif x<=2**(n+2)-4:
        return p[n-1]+1+ans(n-1,x+1-2**(n+1))
    else:
        return p[n]
print(ans(N,X))

Solution (Python):



N, X = map(int, input().split())

L = []
P = []

for i in range(N):
    if i == 0:
        L.append(1)
        P.append(1)
        continue
    else:
        L.append(L[i-1] * 2 + 3)
        P.append(P[i-1] * 2 + 1)

def g(k, x):
    if x == 0:
        return 0
    
    if k == 0:
        return 1
    
    if x < L[k-1] + 2:
        return g(k-1, x-1)
    
    if x == L[k-1] + 2:
        return P[k-1] + 1
    
    if x > L[k-1] + 2:
        return P[k-1] + 1 + g(k-1, x - (L[k-1] + 2))

print(g(N, X))

Solution (Python):
def levelup(X,N):
    if N==0 and X!=0:
        return 1
    if X==0:
        return 0
    elif X<2**(N+1)-1:
        return levelup(X-1,N-1)
    elif X<2**(N+2)-3:
        return 2**N - 1 + levelup(X-2**(N+1)+1,N-1) + 1
    elif X==2**(N+2)-3:
        return 2**(N+1) - 1

N,X=list(map(int,input().split()))

print(levelup(X,N))

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
n, x = map(int, input().split())
layers = [1]
pats = [1]
cnt = 1
p_cnt = 1
for _ in range(n):
  cnt = cnt * 2 + 3
  p_cnt = p_cnt * 2 + 1
  layers.append(cnt)
  pats.append(p_cnt)
def solve(lev, rest):
  if rest == 0:
    return 0
  if lev == 0:
    return 1
  if rest - 1 <= layers[lev - 1]:
    return solve(lev - 1, rest - 1)
  elif rest - 1 == layers[lev - 1]:
    return pats[lev - 1] + 1
  else:
    return pats[lev - 1] + 1 + solve(lev - 1, rest - 1 - layers[lev - 1] - 1)

print(solve(n, x))

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import random
import re
import string
import sys

N, X = map(int, input().split())

all_elements = [1]
ps = [1]
bs = [0]
for i in range(50):
    all_elements.append(all_elements[-1] * 2 + 3)
    ps.append(ps[-1] * 2 + 1)
    bs.append(bs[-1] * 2 + 2)

def from_bottom(n, x):
    if n == 0:
        if x == 1: return 1
        else: return 0

    curr_elements = all_elements[n]
    prev_elements = all_elements[n - 1]

    if x <= 1:
        return 0
    if x <= prev_elements + 1:
        return from_bottom(n - 1, x - 1)
    if x == prev_elements + 1:
        return from_bottom(n - 1, x - 2) + 1
    if x <= prev_elements * 2 + 2:
        return 1 + ps[n - 1] + from_bottom(n - 1, x - 2 - prev_elements)
    if x == curr_elements:
        return ps[n]

def slow(n, x):
    burger = ['P']
    for i in range(n):
        burger = ['B'] + burger + ['P'] + burger + ['B'] 
    
    ans = 0
    for i in range(x):
        ans += burger[i] == 'P'
    return ans











print(from_bottom(N, X))

Solution (Python):
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

burger = [1] + [0 for i in range(50)]
niku = [1] + [0 for i in range(50)]
for i in range(50):
    burger[i+1] = 2 * burger[i] + 3
    niku[i+1] = 2 * niku[i] + 1

n, x = getMN()

def singleburger(x):
    if x <= 1:
        return 0
    elif x <= 4:
        return x-1
    else: return 3

def intonburger(n,x):
    if n == 1:
        return singleburger(x)
    if x <= 1:
        return 0
    elif x <= burger[n-1] + 1:
        return intonburger(n-1, x-1)
    elif x == burger[n-1] + 2:
        return niku[n-1] + 1
    elif x <= 2 * burger[n-1] + 2:
        return niku[n-1] + 1 + intonburger(n-1, x-burger[n-1]-2)
    else:
        return 2 * niku[n-1] + 1

print(intonburger(n,x))

Solution (Python):

L=[];
P=[];

def dfs(n,x):
    if(x==0):
        return 0;
    if(n==0):
        return 1;
    if(x==1):
        return 0;
    if(x>=L[n]-1):
        return P[n];
    nx=x-1;
    if(nx<=L[n-1]):
        return dfs(n-1,nx);
    if(nx==L[n-1]+1):
        return P[n-1]+1;
    if(nx>L[n-1]+1):
        return P[n-1]+1+dfs(n-1,nx-(L[n-1]+1));
    
def main():
    N,X = input().split();
    n=int(N);
    x=int(X);
    for i in range(0,51):
        L.append(0);
        P.append(0);
    L[0]=1;
    P[0]=1;
    for i in range(1,n+1):
        L[i]=L[i-1]*2+3;
        P[i]=P[i-1]*2+1;
    print(dfs(n,x));
    
main();

Solution (Python):

n,x = map(int, input().split())

d = [0 for _ in range(51)]
d[0] = 1
for i in range(50):
    d[i+1] = 3 + 2*d[i]

p = [0 for _ in range(51)]
p[0] = 1
for i in range(50):
    p[i+1] = 1 + 2*p[i]

def f(l,x):
    if l==1:
        if x>3:
            return 3
        elif x<2:
            return 0
        else:
            return x-1
    if x==0:
        return 0
    if d[l]==x:
        return p[l]

    if 2+d[l-1]<=x:
        return f(l-1,d[l-1]) + 1 + f(l-1, x-1-d[l-1]-1)
    else:
        return f(l-1, x-1)

print(f(n,x))

Solution (Python):

def p_(l):
    if l==0:
        return 1;
    return 1+2*p_(l-1)
def len_(l):
    if l==0:
        return 1;
    return 3+2*len_(l-1)
def solve(l,s):
    if s==0:
        return 0
    if s==le[l]:
        return p[l]
    elif s>=2+le[l-1]:
        return 1+p[l-1]+solve(l-1,s-2-le[l-1])
    elif s>=1+le[l-1]:
        return p[l-1]+solve(l-1,s-2-le[l-1])
    elif s>=2:
        return solve(l-1,s-1)
    return 0

n,x=map(int,input().split())
p=[0]*(n+1)
le=[0]*(n+1)
for i in range(n+1):
    p[i]=p_(i)
    le[i]=len_(i)
print(solve(n,x))

Solution (Python):
import sys
import string
n = raw_input()
n,k = n.split(' ') 

l=[1]
f=[1]
for i in range(55):
    l.append(l[len(l)-1]*2+3)
    f.append(f[len(f)-1]*2+1)

def s(k,level):
    if (level==0):
        if (k>0): return 1
        return 0
    if (k==1): return 0
    if (k>=l[level]): return f[level]
    if (k<l[level]):
        if (k<=l[level-1]+1):
            return s(k-1,level-1)
        if (k==l[level-1]+2):
            return 1+s(k-1,level-1)
        return 1+f[level-1]+s(k-1-1-l[level-1],level-1)

n = string.atoi(n)
k = string.atoi(k)
print (s(k,n))

Solution (Python):
def c(n, x):
  if n==0:
    return 1
  s=0
  if x==1:
    return 0
  elif 1<x<=1+l[n-1]:
    return c(n-1,x-1)
  elif x==2+l[n-1]:
    return c(n-1,x-1)+1
  else:
    return p[n-1]+1+c(n-1,x-l[n-1]-2)

N, X = map(int, raw_input().split())
l = [0]*(N+1)
l[0]=1
p = [0]*(N+1)
p[0]=1
for i in range(1, N+1):
  l[i] = 2*l[i-1]+3
  p[i] = 2*p[i-1]+1
a = c(N,X)
print a

Solution (Python):
n,x = (int(i) for i in input().split())
def pc(n,k):
	if n and k==1: return 0
	elif k==1: return 1
	elif k<2**(n+1)-1: return pc(n-1,k-1)
	elif k==2**(n+1)-1: return 2**n
	elif k<2**(n+2)-3: return 2**n+pc(n-1,k-2**(n+1)+1)
	else: return 2**(n+1)-1
print(pc(n,x))

Solution (Python):
n, x = map(int, input().split())
x -= 1
thick = [1]
for i in range(n):
    thick.append(thick[i] * 2 + 3)


def f(level, layer):
    if level == 0:
        return 1
    elif layer == 0:
        
        return 0
    elif 1 <= layer <= thick[level-1]:
        
        return f(level-1, layer-1)
    elif layer == thick[level-1]+1:
        
        return f(level-1, thick[level-1]-1) + 1
    elif thick[level-1]+2 <= layer <= thick[level-1] * 2 + 1:
        
        return f(level-1, thick[level-1]-1) + 1 + f(level-1, layer-thick[level-1]-2)
    elif layer == thick[level-1] * 2 + 2:
        
        return f(level-1, thick[level-1]-1) * 2 + 1


print(f(n, x))

Solution (Python):
N,K = map(int,input().split())

l = [1]*(N+2)
p = [1]*(N+2)


for i in range(N+1):
	l[i+1] = 2*l[i]+3
	p[i+1] = 2*p[i]+1

def dfs(level,dist):
	ans = 0
	if level==0:
		return 1
	elif dist==1:
		return 0
	elif dist==l[level]:
		return p[level]
	elif dist==2+l[level-1]:
		return p[level-1]+1
	else:
		if dist <= 1+l[level-1]:
			ans = dfs(level-1,dist-1)
		else:
			ans = p[level-1]+1+dfs(level-1,dist-2-l[level-1])
	return ans

print(dfs(N,K))

Solution (Python):
N, X = map(int, input().split())

length = [1]
paty = [1]
for i in range(N):
  length.append(3 + length[-1] * 2)
  paty.append(1 + paty[-1] * 2)

def num_paty(n, f, t):
  global length
  global paty

  if f >= length[n] or t < 0:
    ret = 0
  elif f <= 0 and t >= length[n]-1:
    ret = paty[n]
  else:
    half = length[n] // 2
    ret = 1 if f <= half and t >= half else 0
    ret += num_paty(n-1, f-1, t-1)
    ret += num_paty(n-1, f-half-1, t-half-1)
  return ret

print(num_paty(N, 0, X-1))

Solution (Python):
def l(n):
    return 2 ** (n+2) - 3


def p(n):
    return 2 ** (n+1) - 1


def countp(n, x):
    if n == 0:
        return 1
    if x == 1:
        return 0
    if 1 < x <= 1 + l(n-1):
        return countp(n-1, x-1)
    if x == 1 + l(n-1) + 1:
        return p(n-1) + 1
    if 1 + l(n-1) + 1 < x <= 1 + l(n-1) + 1 + l(n-1):
        return p(n-1) + 1 + countp(n-1, x-1-l(n-1)-1)
    return p(n-1) + 1 + p(n-1)


N, X = map(int, input().split())
print(countp(N, X))