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.



Sample input:
2 7


Sample output:
4

Submission status: Runtime Error

Solution code:
len_burger_cache = {}

def len_burger(n:int):
    if n == 0:
        return 1
    elif n in len_burger_cache:
        return len_burger_cache[n]
    else:
        tmp = (len_burger(n-1)*2) + 3
        len_burger_cache[n] = tmp
        return tmp

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

def culc_eat(n:int, x:int):
    if n == 0:
        return 1
    if n < len_burger(x):
        # 次の次元まで計算すればOK
        return gen_burger(n)[:x].count('P')
    else:
        # 次の次元の計算を行う
        return culc_eat(n-1, x-1)

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

# gen burger
print(culc_eat(n, x))

Submission status: Runtime Error

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

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

def f(N, X): # X <= 0 や X > a_N を許容し解説本文から簡略化
  if N == 0:
    return 0 if X <= 0 else 1
  elif X <= 1 + a[N-1]:
    return f(N-1, X-1)
  elif X == 2 + a[N-1]:
    return p[N-1] + 1
  elif X == 3 + 2 * a[N-1]:
    return 2 * p[N-1] + 1
  else:
    return p[N-1] + 1 + f(N-1, X-2-a[N-1])
print(f(N, X))

Submission status: Runtime Error

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

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

def f(N, X): # X <= 0 や X > a_N を許容し解説本文から簡略化
　if N == 0:
　　return 0 if X <= 0 else 1
　elif X <= 1 + a[N-1]:
　　return f(N-1, X-1)
　else:
　　return p[N-1] + 1 + f(N-1, X-2-a[N-1])

print(f(N, X))

Submission status: Runtime Error

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

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

 def f(N, X): # X <= 0 や X > a_N を許容し解説本文から簡略化
 if N == 0:
 return 0 if X <= 0 else 1
 elif X <= 1 + a[N-1]:
 return f(N-1, X-1)
else:
 return p[N-1] + 1 + f(N-1, X-2-a[N-1])

print(f(N, X))

Submission status: Runtime Error

Solution code:
# input
n,x=input().split()
n = int(n)
x = int(x)

# gen burger
burger = 'P'
earlystopping = 0
for i in range(0, n):
    burger = 'B' + burger + 'P' + burger + 'B'
    if len(burger) > x:
        earlystopping = i + 1
        break
# eat
diff = n - earlystopping
if diff > 0 and earlystopping != 0:
    x = x - diff
burger = burger[:x]
print(burger.count('P'))

Submission status: Runtime Error

Solution code:
# input
n,x=input().split()
n = int(n)
x = int(x)

# gen burger
burger = 'P'
for i in range(0,n):
    burger = 'B' + burger + 'P' + burger + 'B'

# eat
burger = burger[n-x:]
print(burger.count('P'))

Submission status: Runtime Error

Solution code:
import re

H, W = map(int, input().split())
L = {0: 'P'}
for i in range(1, H + 1):
    L[i] = 'B' + L[i - 1] + 'P' + L[i - 1] + 'B'

print(len(re.findall('P', L[H][-W:])))

Submission status: Runtime Error

Solution code:
cache = [None]*51
cache[0] = "P"


def make_burger(level):
    if cache[level]:
        return cache[level]

    l_1 = make_burger(level-1)
    cache[level] = "B" + l_1 + "P" + l_1 + "B"
    return cache[level]


n, x = map(int, raw_input().split())
# n = 50
# x = 4321098765432109

x -= 1
# print make_burger(n)
# print make_burger(n)[:-x]

print make_burger(n)[:-x].count('P')

Submission status: Runtime Error

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

burgers = ['P']
for i in range(1,N+1):
  burgers.append('B' + burgers[i-1] + 'P' + burgers[i-1] + 'B')
print(burgers[-1][:X].count('P'))

Submission status: Runtime Error

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


def burger(N):
    if N == 0:
        return "B"
    burg_pre = burger(N-1)
    return "B" + burg_pre + "P" + burg_pre + "P"

print(burger(N))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

"""
DP
"""

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

dp =[''] * (N+1)
dp[0] = 'P'

for i in range(1, N+1):
    dp[i] = 'B' + dp[i-1] + 'P' + dp[i-1] + 'B'

print(dp[N][:X].count('P'))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

"""
再帰
"""

import sys

# 再帰呼び出しの回数制限(デフォルト1000)
sys.setrecursionlimit(10 ** 9)

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

def dfs(cur, L):
    if L == N:
        return cur
    return dfs('B' + cur + 'P' + cur + 'B', L + 1)

burger = dfs('P', 0)

print(burger[:X].count('P'))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

"""
再帰
"""

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

def dfs(cur, L):
    if L == N:
        return cur
    return dfs('B' + cur + 'P' + cur + 'B', L + 1)

burger = dfs('P', 0)

print(burger[:X].count('P'))

Submission status: Runtime Error

Solution code:

from sys import stdin
yeah = stdin.readline().split(" ")
a=int(yeah[0])
b=int(yeah[1])
#a=1
#b=1
w=["P"]+[None]*(a)
for i in range(1,len(w)):
    w[i]="B"+w[i-1]+"P"+w[i-1]+"B"
#print(w)
j=0
count=0
for i in w[-1]:
    if j<b and i=="P":
        count+=1
    j+=1
print(count)

Submission status: Runtime Error

Solution code:
from sys import stdin
yeah = stdin.readline().split(" ")
a=int(yeah[0])
b=int(yeah[1])
w=["P"]+[None]*(a)
for i in range(1,len(w)):
    w[i]="B"+w[i-1]+"P"+w[i-1]+"B"
j=0
count=0
for i in w[-1]:
    if j<7 and i=="P":
        count+=1
    j+=1
print(count)

Submission status: Runtime Error

Solution code:
def cal(N, i):
    if N > 0:
        return 2 * cal(N - 1, i) + i
    elif N == 0:
        return 1
    else:
        return 0

def pCal(N ,X):
    total = cal(N, 3)
    half = total // 2
    pTmp = cal(N - 1, 1)
    if X == 1 and N == 0:
        return 1
    elif X <= N:
        return 0
    elif X < half:
        return pCal(N - 1, X - 1)
    elif X == half:
        return pTmp
    elif X == half + 1:
        return pTmp + 1
    elif total > X > half + 1:
        return (pTmp + 1) + pCal(N - 1, X - (half + 1))
    elif x == total:
        return cal(N, 1)
N, X = list(map(int, input().split()))
print(pCal(N, X))

Submission status: Runtime Error

Solution code:
def cal(N, i):
    if N > 0:
        return 2 * cal(N - 1, i) + i
    elif N == 0:
        return 1
    else:
        return 0

def pCal(N ,X):
    total = cal(N, 3)
    half = total // 2
    pTmp = cal(N - 1, 1)
    if X == 1 and N == 0:
        return 1
    elif X <= N:
        return 0
    elif X < half:
        return pCal(N - 1, X - 1)
    elif X == half:
        return pTmp
    elif X == half + 1:
        return pTmp + 1
    elif total > X > half + 1:
        return (pTmp + 1) + pCal(N - 1, X - (half + 1))
    else x == total
        return cal(N, 1)
N, X = list(map(int, input().split()))
print(pCal(N, X))

Submission status: Runtime Error

Solution code:
def make_humb(n):
    if n == 0:
        return("P")
    else:
        humb_n_1 = make_humb(n-1)
        humb = "B{}P{}B".format(humb_n_1, humb_n_1)
        return(humb)

def main():
    n, x = map(int, input().split())

    humb = make_humb(n)
    dogs_eat = humb[-x:]

    ans = 0
    for c in dogs_eat:
        if c == "P":
            ans += 1

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def f(v, i):
    s,n,m = 4 * 2 ** v - 3, 2 * 2 ** v - 1, (s + 1) / 2
    return 0 if i < 1 or i > s else [0, f(v - 1, i - 1), n / 2 + 1, f(v - 1, i - s / 2 - 1) + n / 2 + 1, n, 1][(i > 1) + (i >= m) + (i > m) + (i >= s) + (v == 0) * 3]

print f(*map(int, raw_input().split()))

Submission status: Runtime Error

Solution code:
def f(v, i):
    s,n,m = 4 * 2 ** v - 3, 2 * 2 ** v - 1, s / 2 + 1
    return 0 if i < 1 or i > s else [0, f(v - 1, i - 1), n / 2 + 1, f(v - 1, i - s / 2 - 1) + n / 2 + 1, n, 1][(i > 1) + (i >= m) + (i > m) + (i >= s) + (v == 0) * 3]

print f(*map(int, raw_input().split()))

Submission status: Runtime Error

Solution code:
N, X = map(int, raw_input().split())

def func(lv, id):
    sz = 4 * 2 ** lv - 3
    num = 2 * 2 ** lv - 1
    mid = (sz + 1) / 2
    if lv == 0:
        return 1
    if id == 1:
        return 0
    if id == mid:
        return num / 2 + 1
    if id == sz:
        return num
    if 1 < id < mid:
        return func(lv - 1, id - 1)
    if mid < id < sz:
        return func(lv - 1, id - (sz - 7) / 2) + num / 2

print func(N, X)

Submission status: Runtime Error

Solution code:
N, X = map(int, raw_input().split())

def func(lv, id):
    sz = 4 * 2 ** lv - 3
    num = 2 * 2 ** lv - 1
    mid = (sz + 1) / 2
    if lv == 0:
        return 1
    if id == 1:
        return 0
    if id == mid:
        return (num - 1) / 2 + 1
    if id == sz:
        return num
    if 1 < id < mid:
        return func(lv - 1, id - 1)
    if mid < id < sz:
        return func(lv - 1, id - (sz - 3) / 2) + (num - 1) / 2

print func(N, X)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
pat = [1 for _ in range(N+1)]
mai = [1 for _ in range(N+1)]
for i in range(1, N+1):
  mai[i] = 2 * mai[i-1] + 3
  pat[i] = 2 * pat[i-1] + 1

def cnt_pat(x, n):
  if n == 0:
    return 1
  if x == 1:
    return 0
  if x <= mai[n] // 2:
    return cnt_pat(x-1, n-1)
  if x == mai[n] // 2 + 1:
    return pat[n-1] + 1
  if x < mai[n]:
    return pat[n-1] + 1 + cnt_pat(x-mai[n]//2-1, n-1)
  if x == n:
    return pat[n]
  
print(cnt_pat(X, N))

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
pat = [1 for _ in range(N)]
mai = [1 for _ in range(N)]
for i in range(1, N+1):
  mai[i] = 2 * mai[i-1] + 3
  pat[i] = 2 * pat[i-1] + 1

def cnt_pat(x, n):
  if n == 0:
    return 1
  if x == 1:
    return 0
  if x <= mai[n] // 2:
    return cnt_pat(x-1, n-1)
  if x == mai[n] // 2 + 1:
    return pat[n-1] + 1
  if x < mai[n]:
    return pat[n-1] + 1 + cnt_pat(x-mai[n]//2-1, n-1)
  if x == n:
    return pat[n]
  
print(cnt_pat(X, N))

Submission status: Runtime Error

Solution code:
N, X = map(int, raw_input().split())

def func(lv, id):
    sz = 4 * 2 ** lv - 3
    num = 2 * 2 ** lv - 1
    mid = (sz + 1) / 2
    if lv == 0:
        return 1
    if id == 1:
        return 0
    if id == mid:
        return (num - 1) / 2 + 1
    if id == sz:
        return num
    if 1 < id < mid:
        return func(lv - 1, id - 1)
    if mid < id < sz:
        return func(lv - 1, id - (sz - 3) / 2) + num / 2

print func(N, X)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
pat = [0 for _ in range(N+1)]
mai = [0 for _ in range(N+1)]
pat[1] = 1
mai[1] = 1
for i in range(2, N+1):
  mai[i] = 2 * mai[i-1] + 3
  pat[i] = 2 * pat[i-1] + 1

def cnt_pat(x, n):
  if n == 1:
    return 1
  if x == 1:
    return 0
  if x <= mai[n] // 2:
    return cnt_pat(x-1, n-1)
  if x == mai[n] // 2 + 1:
    return pat[n-1] + 1
  if x < mai[n]:
    return pat[n-1] + 1 + cnt_pat(x-mai[n]//2-1, n-1)
  if x == n:
    return pat[n]
  
print(cnt_pat(X, N))

Submission status: Runtime Error

Solution code:
import collections
n, x = map(int, input().split())

def d(level, burger="P", i=0):
    if level is i:
        return burger
    else:
        i += 1
        burger = "B"+burger+"P"+burger+"B"
        return d(level, burger, i)

eat = list(d(n))[0:x]
print(collections.Counter(eat)['P'])

Submission status: Runtime Error

Solution code:
N,X = map(int,input().split())
a = "P"
for i in range(N):
  a = "B" + a + "P" + a + "B"
b = a[0:X]
print(b.count("P"))

Submission status: Runtime Error

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


def search(level, eat_num):
    if eat_num == 0:
        if eat_num <= 0:
            return 0
        else:
            return 1

    elif eat_num == 1:
        return 0

    elif 1 < eat_num <= pow(2, level + 1) - 2:
        return search(level - 1, eat_num - 1)

    elif eat_num == pow(2, level + 1) - 1:
        return pow(2, level)

    elif pow(2, level + 1) - 1 < eat_num <= pow(2, level + 2):
        return pow(2, level) + search(level - 1, eat_num - (pow(2, level + 1) - 1))

    elif X == pow(2, level + 2) + 1:
        return pow(2, level + 1) - 1

    else:
        print('out', X)

print(search(level=N, eat_num=X))

Submission status: Runtime Error

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


def search(level, eat_num):
    if eat_num == 0:
        return 1

    elif eat_num == 1:
        return 0

    elif 1 < eat_num <= pow(2, level + 1) - 2:
        return search(level - 1, eat_num - 1)

    elif eat_num == pow(2, level + 1) - 1:
        return pow(2, level)

    elif pow(2, level + 1) - 1 < eat_num <= pow(2, level + 2):
        return pow(2, level) + search(level - 1, eat_num - (pow(2, level + 1) - 1))

    elif X == pow(2, level + 2) + 1:
        return pow(2, level + 1) - 1

    else:
        print('out', X)


print(search(level=N, eat_num=X))

Submission status: Runtime Error

Solution code:
N,X=map(int, raw_input().split())

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

#print "p:::",p
#print "a::::",a

X-=1

ans=0
def f(N,X):
	global ans
#	print "f (",N ," , ", X ," ) " ,ans

	if N==0:
		if X>=-1:
			return 1
		else:
			return 0
	elif X>=a[N]/2:
		X-=(a[N-1]+2)
		ans+=p[N-1]+1
		f(N-1,X)
	elif X<a[N]/2:
		X-=(a[N-1]+2)
		ans+=f(N-1,X)

	X-=(a[1]+2)

f(N,X)

print ans

Submission status: Runtime Error

Solution code:
N,X = map(int,input().split())
alist = [2**(i+2)-3 for i in range(N+1)]
cntlist = [2**(i+1)-1 for i in range(N+1)]
cnt = 0
i = 0

while True:
    i += 1
    if X <= N-i+1:
        break
    elif (alist[-1] - N) <= X:
        cnt += cntlist[-i]
        break
    elif alist[-i]//2 +1 < X: # 左
        cnt += cntlist[-i]//2 +1
        X -= alist[-i]//2 +1
    elif X == alist[-i]//2 +1: # 真ん中
        cnt += cntlist[-i]//2 + 1
        break
    elif X < alist[-i]//2 +1: # 右
        X -= 1

print(cnt)

Submission status: Runtime Error

Solution code:
N,X = map(int,input().split())
alist = [2**(i+2)-3 for i in range(N+1)]
cntlist = [2**(i+1)-1 for i in range(N+1)]
cnt = 0
i = 0

while True:
    i += 1
    if X <= N-i+1:
        break
    elif (alist[-1] - N) <= X:
        cnt += cntlist[-i]
        break
    elif alist[-i]//2 +1 < X: # 左
        cnt += cntlist[-i]//2 +1
        X -= alist[-i]//2 +1
    elif X == alist[-i]//2 +1: # 真ん中
        cnt += cntlist[-i-1] + 1
        break
    elif X < alist[-i]//2 +1: # 右
        X -= 1

print(cnt)

Submission status: Runtime Error

Solution code:
def do(level,x):
    if x<=0: #1層も手を付けないとき
        return 0
    elif x>=whole[level]: #完食できるとき
        return patty[level]
    #それ以外、途中まで食べるときはlevel>=1の場合のみあり得る
    x-=1 #このとき1層目は必ずバンズ
    res=0 ＃食べたパティの総数を記録する
    if x>=whole[level-1]+1: #真ん中のパティまで到達できるとき
        x-=whole[level-1]+1
        res+=patty[level-1]+1
    return res+do(level-1,x)

n,x=map(int,input().split())
whole=[1]+[0]*50
patty=[1]+[0]*50
for i in range(1,n+1):
    whole[i]=whole[i-1]*2+3
    patty[i]=patty[i-1]*2+1
res=0
print(do(n,x))

Submission status: Runtime Error

Solution code:
def dfs (i,depth):
    if i >= depth-1:
        return 'bpppb'
    else:
        s = dfs(i+1,depth)
        return 'b' + s + 'p' + s + 'b'
n,x = map(int,input().split())
r = dfs(0,n)
print(r.count('p',0,x))

Submission status: Runtime Error

Solution code:
ef dfs (i,depth):

    if i >= depth-1:
        return 'bpppb'
    else:
        s = dfs(i+1,depth)
        return 'b' + s + 'p' + s + 'b'

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

p = 2**(n+1)-1
b = 2**(n+1)-2
a = p + b
#print(p,a,b)
pos = a / x
#print(pos)
#print(p / pos)

r = dfs(0,n)
print(r.count('p',0,x))

Submission status: Runtime Error

Solution code:
def f(n, x):
	if n==0:
		return 0 if x<= 0 else 1
	elif x<=1+a[n-1]:
		return f(n-1,x-1)
	else:
		return p[n-1]+1+f(n-1,x-2-a[n-1])


n,x=map(int, input().split())
a,p=[1],[1]
for i in range(N):
	a.append(a[i] * 2 + 3)
	p.append(p[i] * 2 + 1)
    
print(f(n, x))

Submission status: Runtime Error

Solution code:
N,X = map(int,input().split())
if N == 0:
    print(1)
    exit()
# a:バンの数  b:パティの数
a,b = [1],[1]
for i in range(N):
    a.append(2*a[i] + 3)
    b.append(2*b[i] + 1)
def f(N,X):
    if X == 1:
        return 0
    elif 1 < X <= a[N-1] + 1:
        return f(N-1,X-1)
    elif X == a[N-1] + 2:
        return b[N-1] + 1
    elif a[N-1] + 2 < X <= 2*a[N-1] + 2:
        return b[N-1] + 1 + f(N-1,X-(a[N-1]+2))
    elif X = 2*a[N-1] + 3:
        return 2*b[N-1] + 1
ans = f(N,X)
print(ans)

Submission status: Runtime Error

Solution code:
N,X = map(int,input().split())
if N == 0:
    print(1)
    exit()
# a:バンの数  b:パティの数
a,b = [1],[1]
for i in range(N):
    a.append(2*a[i] + 3)
    b.append(2*b[i] + 1)
def f(N,X):
    if X == 1:
        return 0
    elif 1 < X <= a[N-1] + 1:
        return f(N-1,X-1)
    elif X == a[N-1] + 2:
        return b[N-1] + 1
    elif a[N-1] + 2 < X <= 2*a[N-1] + 2:
        return b[N-1] + 1 + f(N-1,X-(a[N-1]+2))
    elif X = 2*a[N-1] + 3:
        return 2*b[N-1] + 1
ans = def(N,X)
print(ans)

Submission status: Runtime Error

Solution code:
def feed_runrun(answer, burger_dimension, appetite):
    if appetite == 0:
        return answer

    burger_size = 2 ** (burger_dimension + 2) - 3
    # 食べきれないなら小さくして試す
    if appetite < burger_size:
        return feed_runrun(answer, burger_dimension - 1, appetite - 1)

    amount_of_p = 2 ** (burger_dimension + 1) - 1
    if appetite == burger_size + 1:
        return answer + amount_of_p + 1
    return feed_runrun(answer + amount_of_p + 1, burger_dimension, appetite - burger_size - 1)


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

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
l=[0]
for i in range(n+1):
  l.append(2**(i+1)-1)
m=[0]
for i in range(n+1):
  m.append(2**(i+2)-3)
c=0
for i in range(n,-1,-1):
  if x<2**(i+1)-1:
    x-=1
  elif x==2**(i+1)-1:
    c+=l[i]+1
    x=0
  elif x>2**(i+1)-1:
    c+=l[i]+1
    elif x==m[i+1]:
      x-=1
    x=x-(2**(i+1)-1)
print(c)

Submission status: Runtime Error

Solution code:
def feed_runrun(answer, burger_dimension, appetite):
    if appetite == 0:
        return answer

    burger_size = 2 ** (burger_dimension + 2) - 3
    # 食べきれない
    if appetite < burger_size:
        return feed_runrun(answer, burger_dimension - 1, appetite - 1)
    # 食べきれるので, 食べる
    amount_of_p = 2 ** (burger_dimension + 1) - 1
    return feed_runrun(answer + amount_of_p + 1, burger_dimension, appetite - burger_size - 1)


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

Submission status: Runtime Error

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    # if n == 0:
    #     if x == 1:
    #         return 1
    #     else:
    #         raise "error"
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l
        bottom = 1
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return 0
    elif c == "4":
        return 1 + P[n-1] * 2
    elif c == "3":
        return 1 + P[n-1]
    elif c == "0":
        return calc_pan(n-1, x-1)
    elif c == "1":
        return P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2)





sum = calc_pan(N, X)
print(sum)

Submission status: Runtime Error

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    # if n == 0:
    #     if x == 1:
    #         return 1
    #     else:
    #         raise "error"
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return 0
    elif c == "4":
        return 1 + P[n-1] * 2
    elif c == "3":
        return 1 + P[n-1]
    elif c == "0":
        return calc_pan(n-1, x-1)
    elif c == "1":
        return P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2)





sum = calc_pan(N, X)
print(sum)

Submission status: Runtime Error

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    if n == 0:
        if x == 1:
            return 1
        else:
            raise "error"
    # if x == 1:
    #     if n == 0:
    #         return "3"
    #     else:
    #         return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x, sum):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return sum
    elif c == "4":
        return sum + 1 + P[n-1]
    elif c == "3":
        return sum + 1 
    elif c == "0":
        return sum + calc_pan(n-1, x-1, 0)
    elif c == "1":
        return sum + P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2, 0)





sum = calc_pan(N, X, 0)
print(sum)

Submission status: Runtime Error

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)

def judge(n, x):
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x, sum):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return sum
    elif c == "4":
        return sum + 1 + P[n-1]
    elif c == "3":
        return sum + 1 
    elif c == "0":
        return sum + calc_pan(n-1, x-1, 0)
    elif c == "1":
        return sum + P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2, 0)





sum = calc_pan(N, X, 0)
print(sum)

Submission status: Runtime Error

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N, X = map(int, input().split())

def calcl(n):
    l = 2 ** (n+2) - 3 
    return l
def calcp(n):
    p = 2 ** (n+1) - 1
    return p
    
L = [1]
P = [1]

for i in range(1, N+1):
    L.append( L[i-1] * 2 + 3)
    P.append( P[i-1] * 2 + 1)
# for i in range(1, N+1):
#     L.append(0)
#     P.append(0)
#     L[i] = calcl(i)
#     P[i] = calcp(i)


# print("L, P: {}, {}".format(L, P ) ) 


def judge(n, x):
    if x == 1:
        if n == 0:
            return "3"
        else:
            return "2"
    else:
        l = L[n]       # layers
        top = l-1
        bottom = 0
        mid = (l-1)//2 # index of middle
        # print("l, mid: {}, {}, {}".format(l,mid,x))

        if x == mid:
            return "3"
        elif top > x > mid:
            return "1"
        elif 0 < x < mid:
            return "0"            
        elif x == top:
            return "4"
        elif x == bottom:
            return "2"
        else:
            raise "No such pattern!"
        
    
def calc_pan(n,x, sum):
    c = judge(n,x)
    # print("calc_panK: n:{}, x:{}, c: {}".format(n,x,c))
    if  c == "2":
        return sum
    elif c == "4":
        return sum + 1 + P[n-1]
    elif c == "3":
        return sum + 1 + P[n-1]
    elif c == "0":
        return sum + calc_pan(n-1, x-1, 0)
    elif c == "1":
        return sum + P[n-1] + 1 + calc_pan(n-1, x-L[n-1]-2, 0)





sum = calc_pan(N, X, 0)
print(sum)

Submission status: Runtime Error

Solution code:
p = [1]*51
t = [1]*51
def f(n,x):
    if n==0:
        return f
    if x==1:
        return 0
    if x==t[n]:
        return p[n]
    if x==p[n]:
        return p[n-1]+1
    if x<p[n]:
        return f(n-1,x-1)
    return p[n-1]+f(n-1,x-p[n])+1

for i in range(1,51):
    p[i] = p[i-1]*2+1
    t[i] = t[i-1]*2+3
n,x = map(int,input().split())    
print(f(n,x))

Submission status: Runtime Error

Solution code:
(n, x) = list(map(int, input().split()))
num = 1
pate = 1
for i in range(n):
    num = num * 2 + 3
    pate = pate * 2 + 1
def culc(l, idx, num, pate):
    npate = (pate - 1) // 2
    nnum = (num - 3) // 2
    mid = (num + 1) // 2
    if idx == 1:
        return 0
    elif mid == idx:
        return npate + 1
    elif mid < idx:
        return npate + 1 + culc(l - 1, idx - mid, nnum, npate)
    else:
        return culc(l - 1, idx - 1, nnum, npate)
print(culc(n, x, num, pate))

Submission status: Runtime Error

Solution code:
N, X = input().split()

N = int(N)
X = int(X)

all = 2**(N+2)-3
b_all = 0
for i in range(N+1):
    if i > 0:
        b_all += 2**i

p_all = all - b_all

print(p_all)
first = 1
last = all

get = 0
for i in range(N):
    #print(int(first), int(last),"*")
    if i < N-1:
        if (first + last) / 2 == X:
            get += (p_all + 1) / 2
            break

        elif (first + last) / 2 < X:
            get += (p_all + 1) / 2
            p_all -= (p_all + 1) / 2
            first = (first + last) / 2 + 1
            last = last - 1

            if first == X:
                break
        elif (first + last) / 2 > X:
            p_all = (p_all - 1) / 2
            last = (first + last) / 2 - 1
            first = first + 1

            if last == X:
                get += p_all
                break
	else:
        if first == X:
            break
        elif last == X:
            get += 3
        else:
            get += X - first
            break

    #print(int(get),int(p_all))

print(int(get))

Submission status: Runtime Error

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

PforL = [1]
HforL = [1]
for i in range(1,51):
    PforL.append(PforL[i-1]*2+1)
    HforL.append(HforL[i-1]*2+3)

def recursive(n,x):
    if x<=n:
        return 0
    if x<=HforL[n-1]+1:
        return recursive(n-1,x-1)
    else:
        return PforL[n-1]+1+recursive(n-1,x-HforL[n-1]-2)

print(recursive(N,X))

Submission status: Runtime Error

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

a, p = [1], [1]

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

def f(N, X):
	if N == 0:
		return 0 if X <= 0 else 1
	elif X <= 1 + a[N+1]:
		return f(N-1, X-1)
	else:
		return p[N-1] + 1 + f(N-1, X-2-a[N-1])

print(f(N, X))

Submission status: Runtime Error

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

PforL = [1]
HforL = [1]
for i in range(1,51):
    PforL.append(PforL[i-1]*2+1)
    HforL.append(HforL[i-1]*2+3)

def recursive(n,x):
    if x<=n:
        return 0
    if x<=HforL[n-1]+1:
        return recursive(n-1,x-1)
    if x<HforL[n]:
        return PforL[n-1]+1+recursive(n-1,x-HforL[n-1]-2)

print(recursive(N,X))

Submission status: Runtime Error

Solution code:
def m_barg(L):
  if not L:
    return 'P'
  else:
    return 'B{0}P{0}B'.format(m_barg(L-1))
L,X = map(int,input().split())
print(m_barg(L)[-X:].count('P'))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math

al = [0, 5] + [0] * 49
bl = [0, 3] + [0] * 49


def solve2(ans, x, idx, cnt):
    if idx == 1:
        return ans + (0, 0, 1, 2, 3, 3)[x]
    if x >= al[idx - 1] + 1:
        ans += bl[idx - 1]
        x -= al[idx - 1] + 1
        if x >= 1:
            ans += 1
            x -= 1
    else:
        x -= 1
        if x <= 0:
            return ans
    return solve2(ans, x, idx - 1, cnt)


def solve():
    n, x = map(int, input().split())
    for i in range(2, n + 1):
        al[i] = al[i - 1] * 2 + 3
        bl[i] = bl[i - 1] * 2 + 1
    ans = solve2(0, x, n, 0)
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Runtime Error

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

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

deff(N, X):
        return 0 if X <= 0 else 1
    elif X <= 1 + a[N - 1]:
        return f(N - 1, X - 1)
    else:
        return p[N - 1] + 1 + f(N - 1, X - 2 - a[N - 1])

print(f(N, X))

Submission status: Runtime Error

Solution code:

import math

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

def rec(lv,ls):
    if lv == 0:
        return [1]
    else:
        ret = rec(lv-1,ls)
        for i in range(0,len(ret) -1):
            if ret[i]>0 and ret[i+1]>0:
                ret[i+1] += ret[i]
                ret[i] = 0  #deleted tag

        while 0 != ret.count(0):
            ret.remove(0)

        return [-1,*ret,1,*ret,-1]

#print( rec(n, list()) )


count = 0
for e in rec(n, list() ):
    abs_e = abs(e)

    if x == 0:
        break

    if x>=abs_e:
        x -= abs_e
        count += e if e>0 else 0
    else:
        count += x if e>0 else 0
        x = 0
    #print(x," ",count)

print(count)