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:

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)

Submission status: Runtime Error

Solution code:
IN = input().split()
N = int(IN[0])
X = int(IN[1])

BP = 'P'
for i in range(N):
    if len(BP) < X:
        BP = 'B' + BP + 'P' + BP + 'B'
    else:
        BP = BP +'B'

BP = BP[-X:]
print(BP.count("P"))

Submission status: Runtime Error

Solution code:

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]

 
count = 0
for e in rec(n, list() ):
    abs_e = abs(e)
    if x>=abs_e:
        x -= abs_e
        count += e if e>0 else 0
    else:
        count += e if e>0 else 0


print(count)

Submission status: Runtime Error

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

def b(z):
  if z != 0:
  	return b(z-1)*2+1
  else:
    return 1
def f(x):
  while x<=3:
    
  
def c(y):
  a = 0
  y -= 2
  while y<=5:
    y -= 2
    y -= 3
    a += 3
  return a
  a = b(n)
  
print(c(x))

Submission status: Runtime Error

Solution code:
IN = input().split()
N = int(IN[0])
X = int(IN[1])

BP = 'P'
for i in range(N):
    BP = 'B' + BP + 'P' + BP + 'B'
    if len(BP) > X:
        break


BP = BP[-X:]
print(BP.count("P"))

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
X=[0,1,2,3,3]
a=0
print
if n==1:
  print(0)
if n >1:
  if x %2 ==0:
    tmp=int(x/2**(n-1))
    if tmp>=len(X):
      print((X[tmp-1]+1)*(2**(n-1))-1)
    else:
      print(X[tmp]*(2**(n-1))-1)
  else:
    tmp=int(x/2**(n-1))
    print(X[tmp]*(2**(n-1))+1)

Submission status: Runtime Error

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

done = [[False] * (x+1) for _ in range(n+1)]
memo = [[0] * (x+1) for _ in range(n+1)]
for i in range(0, n+1):
	done[i][0] = True
memo[0][1] = 1
done[0][1] = True

def cnt(n, x):
	if not done[n][x]:
		done [n][x] = True
		if x<2**(n+1)-1:
			memo[n][x] = cnt(n-1, x-1)
		elif 2**(n+1)-1 <= x < 2**(n+2)-3:
			memo[n][x] = cnt(n-1, 2**(n+1)-3) + 1 + cnt(n-1, x-2**(n+1)+1)
		else:
			memo[n][x] = cnt(n-1, 2**(n+1)-3) + 1 + cnt(n-1, x-2**(n+1))
	return memo[n][x]

print(cnt(n, x))

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[:x:]
print(b.count('P'))

Submission status: Runtime Error

Solution code:
n, x = map(int, input().split())
def get_burger(base, n):
    if n==0:
        return base
    return get_burger('B' + base + 'P' + base + 'B', n-1)
print(get_burger('P', n)[:x].count('P'))

Submission status: Runtime Error

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

for i in range(n):
    ans = 'b{}p{}b'.format(ans, ans)

print(ans[-x:].count('p'))

Submission status: Runtime Error

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

def burg(n):
    st = "P"
    while n>0:
        st = "B{0}P{1}B". format(st,st)
        n -= 1
    return st

B = list(burg(N))
count = 0
for i in range(X):
    if B[i]=="P":
        count+=1
        
print(count)

Submission status: Runtime Error

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

def burg(n):
    st = "P"
    while n>0:
        st = "B{0}P{1}B". format(st,st)
        n -= 1
    return st

B = list(burg(N))
print(B)
count = 0
for i in range(X):
    if B[i]=="P":
        count+=1
        
print(count)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
b = list()
b.append('P')
for i in range(1, n+1):
	b.append('B' + b[i - 1] + 'P' + b[i-1] + 'B')
eat = b[n][:k]
print(eat.count('P'))

Submission status: Runtime Error

Solution code:
IN = input().split()
N = int(IN[0])
X = int(IN[1])

def baga(N):
    b = "P"
    if N == 0:
        return b
    for i in range(N):
        b = "B" + b + "P" + b + "B"
    return b

tonkatu = list(baga(N))
runrun = tonkatu[-X:]
mojiretu = ''.join(tonkatu)
#print(mojiretu)
print(runrun.count("P"))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000000)

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

num = [0] * (n + 1)
num_p = [0] * (n + 1)
num_h = [0] * (n + 1)
num[0] = 1
num_p[0] = 1

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

for i in range(n + 1):
    num_h[i] = num[i] // 2 + 1

# idx 番目までに含むPの数を返す
def func(idx, n):
    if idx <= n:
        ret = 0

    if idx == num_h[n]:
        ret = num_p[n - 1] + 1

    if idx > num_h[n]:
        ret = num_p[n] - func(num[n] - idx, n)

    if idx < num_h[n - 1]:
        ret = func(x, n)

    return ret

print(func(x, n))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
b = list()
b.append('P')
for i in range(1, n+1):
	b.append('B' + b(i - 1) + 'P' + b(i-1) + 'B')
eat = b[n][:k]
print(eat.count('P'))

Submission status: Runtime Error

Solution code:
N,X = list(map(int,input().split()))
patinum = [1]
Llength=[1,1]
for i in range(N+10):
    patinum.append(patinum[i]*2+1)
    Llength.append(Llength[i]*2+3)
ans=0
while X>=1 or N>1:
    if X==Llength[N]:
        N=0
        ans+=patinum[N]
    elif X>Llength[N]:
        if X>(Llength[N]+2):
            ans+= patinum[N]
            ans+=1
            X=X-Llength[N]-2
            N-=1
        elif X==Llength[N]+1:
            ans+=patinum[N]
            X=X-Llength[N]-1
            N=0

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000000)

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

num = [0] * (n + 1)
num_p = [0] * (n + 1)
num_h = [0] * (n + 1)
num[0] = 1
num_p[0] = 1

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

for i in range(n + 1):
    num_h[i] = num[i] // 2 + 1

def memoize(f): #メモ化関数
    table = {}
    def func(*args):
        if not args in table:
            table[args] = f(*args)
        return table[args]
    return func

# idx 番目までに含むPの数を返す
@memoize
def func(idx, n):
    if idx <= n:
        ret = 0

    if idx == num_h[n]:
        ret = num_p[n - 1] + 1

    if idx > num_h[n]:
        ret = num_p[n] - func(num[n] - idx, n)

    if idx < num_h[n - 1]:
        ret = func(x, n)

    return ret

print(func(x, n))

Submission status: Runtime Error

Solution code:
from functools import lru_cache
#@lru_cache(maxsize=3000)
@memoize
def burger(str_b,level):
  if(level == 0):
    return str_b + 'p'
  str_b += 'B'
  str_b = burger(str_b,level-1)
  str_b += 'p'
  str_b = burger(str_b,level-1)
  str_b += 'B'
  return str_b


str_b = ''
N,X = map(int,input().split())
str_b = burger(str_b,N)
b_list = list(str_b)
eat_list = b_list[::-1]
eat_list = b_list[0:X]
print(eat_list.count('p'))

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
def calc(L, X):
    if L == 0 and X == 1:
        return 1
    if X == 1:
        return 0
    l = 2**(L + 1) - 3
    if 2 <= X <= l + 1:
        return solve(L - 1, X - 1)
    elif X == l + 2:
        return solve(L - 1, l) + 1
    elif l + 2 <= X <= 2 * l + 2:
        return solve(L - 1, l) + solve(L - 1, X - l - 2) + 1
    else:
        return 2 * solve(L - 1, l) + 1
print(calc(N, X))

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
b = list()
b.append('P')
for i in range(1, n+1):
	b.append('B' + b(i - 1) + 'P' + b(i-1) + 'B')
eat = b[n][:k]
eat.count('P')

Submission status: Runtime Error

Solution code:
IN = input().split()
N = int(IN[0])
X = int(IN[1])

BP = 'P'
for i in range(N):
    BP = 'B' + BP + 'P' + BP + 'B'

BP = BP[-X:]
print(BP.count("P"))

Submission status: Runtime Error

Solution code:
def bu(n):
  if n == 0:
    return 'p'
  else:
    return 'b' + bu(n-1) + 'p' + bu(n-1) + 'b'


def bun(n, a = 'p'):
  if n == 0:
    return a
  else:
    return bun(n-1, 'b' + a + 'p' + a + 'b' )

n, x = map(int, input().split())
#li = list(bu(n))
#print(li)

count = 0
for i in range(x):
  if bun(n)[i] == 'p':
    count += 1

print(count)

#print(bun(n))

Submission status: Runtime Error

Solution code:
n,x = map(int,input().split())
a = '131'
for i in range(n-1):
    a = str(int(a[0])+1) + a[1::] + '1' + a[:-1:] + str(int(a[-1])+1)
c = 0
d = 0
i = 0
while d < k:
    d += int(a[i])
    if i % 2 ==0:
        c += int(a[i])
    i += 1
print(c)

Submission status: Runtime Error

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

table_l = [-1 for _ in range(50)]
table_p = [-1 for _ in range(50)]
table_l[0] = 1
table_p[0] = 1

for i in range(1, 50):
    table_l[i] = table_l[i-1] * 2 + 3
    table_p[i] = table_p[i-1] * 2 + 1


def f(level, x):
    if level == 1:
        if x == 0 or x == 1:
            return 0
        if x == 2:
            return 1
        if x == 3:
            return 2
        else:
            return 3

    if table_l[level] == x:
        return table_p[level]

    if x - 1 <= table_l[level - 1]:
        return f(level - 1, x - 1)

    if x - 2 == table_l[level - 1]:
        return table_p[level - 1] + 1
    if x - 2 > table_l[level - 1]:
        xx = x - 2 - table_l[level - 1]
        return table_p[level - 1] + 1 + f(level - 1, xx)


print(f(n, x))

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'
a = a[:x:]
print(a.count('P'))

Submission status: Runtime Error

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

def burg(n, st="P"):
    if n<1:
        return(st)
    else:
        return burg(n-1 ,"B{0}P{1}B". format(st,st))

B = list(burg(N))
count = 0
for i in range(X):
    if B[i]=="P":
        count+=1
        
print(count)

Submission status: Runtime Error

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

def burg(n, st="P"):
    if n<1:
        return(st)
    else:
        return burg(n-1 ,"B{0}P{1}B". format(st,st))

B = list(burg(N))
print(B)
count = 0
for i in range(X):
    if B[i]=="P":
        count+=1
        
print(count)

Submission status: Runtime Error

Solution code:
from sys import stdin
N,X=[int(x) for x in stdin.readline().rstrip().split()]
burger_set=["P"]
for i in range(N):
    new_burger="B"+burger_set[0]+"P"+burger_set[0]+"B"
    burger_set[0]=(new_burger)
ans=burger_set[0][0:X].count('P')
print(ans)

Submission status: Runtime Error

Solution code:
n,x = map(int,input().split())
s = "p"
for k in range(n):
    s = "b" + s + "p" + s + "b"
i = 1
p = 0
while i < len(s)+1:
   
    if i == x:
        if s[i-1] == "p":
            print(p+1)
        else:
            print(p)
        break
    if s[i-1] == "p":
        p += 1
    i += 1

Submission status: Runtime Error

Solution code:
import numpy as np
def niku(level, num):
	maisu = 2**(level+2)-3
	if(num <= level):
		return 0
	elif(num < (maisu+1)/2):
		return niku(level-1, num-1)
	elif(num == (maisu+1)/2):
		return 2**(level+1-1)-1+1
	else:
		return 2**(level+1-1)-1+1+niku(level-1, num-((maisu+1)/2))

def main():
	
	buf = input().split()
	n = int(buf[0])
	x = int(buf[1])
	print(niku(n, x))
		
main()

Submission status: Runtime Error

Solution code:
import re

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

burger = [None] * 51
burger[0] = "P"

for i in range(1, N + 1):
    burger[i] = "B" + burger[i - 1] + "P" + burger[i - 1] + "B"
    print(burger[i])

burger_x = burger[N][:X]
ans = len(re.findall("P", burger_x))
print(ans)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
bager = []
bager.append("P")

for i in range(1, N+1):
    bagernow = "B" + bager[i-1] + "P" + bager[i-1] + "B"
    bager.append(bagernow)

    if len(bagernow) >= X:
        break

bagerN = str(bager[i])

runbager = bagerN[:X]

Ans = runbager.count("P")

print(Ans)

Submission status: Runtime Error

Solution code:
def get_next_burger(current_burger) :
	return "B" + current_burger + "P" + current_burger + "B"

N,X = [int(i) for i in input().split()]

current = "P"

for i in range(N) :
	current = get_next_burger(current)

target = current[:X]
print(target.count("P"))

Submission status: Runtime Error

Solution code:
n,x = map(int, input().split())
bp = ["p"]
for i in range(n):
    bp.append("b" + bp[i] + "p" + bp[i] + "b")
print(bp[-1][-x:].count("p"))

Submission status: Runtime Error

Solution code:
# AtCoder Beginner Contest 115 - D.py
anss = [0, 0, 0, 1, 2, 3, 3]


def phi(i):
    return (1 << (i + 1)) - 1


def getlen(i):
    return (1 << (i + 2)) - 2


def f(x, n):
    if n == 1:
        return anss[x]
    if x > getlen(n)/2:
        return phi(n) - f(getlen(n)-x, n)
    else:
        return f(x-1, n-1)


arg = input()
N = int(arg.split()[0])
X = int(arg.split()[1])
print(f(X+1, N))

Submission status: Runtime Error

Solution code:
a, b = (int(i) for i in input().split()) 

pan = "P"

for i in range(a):
    pan = "B" + pan + "P" + pan + "B"

count = 0

for i in range(b):
    if "P" == pan[i]:
        count += 1

print(count)

Submission status: Runtime Error

Solution code:
N,X = map(int,input().split())
def lb(N):
    if N != 0:
        return 2*lb(N-1)+3
    else:
        return 1
def pn(N):
    if N != 0:
        return 2*pn(N-1)+1
    else:
        return 1
def B(N,X):
    if X == 0:
        return 0
    elif lb(N-1)+1 >= X:
        return B(N-1,X-1)
    else:
        return pn(N-1)+1+B(N-1,X-lb(N-1)-2)
print(B(N,X))

Submission status: Runtime Error

Solution code:
level,eat = map(int, input().split())
burger = 'BPPPB'
for i in range(1, level):
  burger = 'B' + burger + 'P' + burger + 'B'
count=0
eat -=1
for i in range(len(burger)):
  if i > eat:
    break
  if burger[i] == 'P':
    count+=1
print(count)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from sys import stdin
N,X=[int(x) for x in stdin.readline().rstrip().split()]
burger_set=["P"]
for i in range(N):
    new_burger="B"+burger_set[0]+"P"+burger_set[0]+"B"
    burger_set.append(new_burger)
    del burger_set[0]
print(burger_set[0].count('P'))

Submission status: Runtime Error

Solution code:
n,x = map(int, input().split())
bp = ["p"]
for i in range(n):
    bp.append("b" + bp[i] + "p" + bp[i] + "b")
print(bp)
print(bp[-1][-x:].count("p"))

Submission status: Runtime Error

Solution code:
level,eat = map(int, input().split())
burger = 'BPPPB'
for i in range(1, level):
  burger = 'B' + burger + 'P' + burger + 'B'
print(burger)
count=0
eat -=1
for i in range(len(burger)):
  if i > eat:
    break
  if burger[i] == 'P':
    count+=1
print(count)

Submission status: Runtime Error

Solution code:
N,X = map(int,input().split())
burgur = "P"
b = "B"
p = "P"
s = 0
for i in range(N):
    burgur = b + burgur + p + burgur + b
for i in range(X):
    if burgur[i] == p:
        s += 1
print(s)

Submission status: Runtime Error

Solution code:
N, X = map(float, input().split())
N = int(N)
bager = []
bager.append("P")

for i in range(1, N+1):
    bagernow = "B" + bager[i-1] + "P" + bager[i-1] + "B"
    bager.append(bagernow)

print("a")
bagerN = str(bager[N])

runbager = bagerN[:X]

Ans = runbager.count("P")

print(Ans)

Submission status: Runtime Error

Solution code:
def func(b, lv):
    if lv == 0:
        return b
    else:
        return func('B{}P{}B'.format(b, b), lv-1)


N, K = map(int, input().split())
L0 = 'P'
s = func(L0, N)
ans = s[:K].count('P')
print(ans)

Submission status: Runtime Error

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

# (i)XがLバーガーの半分より上かどうか？を計算
# (ii-i)上の場合: sumに下半分のパティの数を加算。下半分を切り捨てて、L-1バーガに対して、
#        再度X = (X-(Lバーガーの半分の枚数))で(i)から計算
# (ii-ii)下の場合: 上半分を切り捨てて、L-1バーガに対して再度X = (X-(Lバーガーの半分の枚数))で(i)から計算

patty_num_map = {}
size_map = {}
patty_num_map[0] = 1
size_map[0] = 1
for i in range(1, 51):
    patty_num_map[i] = 1+patty_num_map[i-1]*2
    size_map[i] = 3+size_map[i-1]*2

L = N
patty_sum = 0
L1_map = {}
L1_map[0] = 0
L1_map[1] = 0
L1_map[2] = 1
L1_map[3] = 2
L1_map[4] = 3
L1_map[5] = 3
L1_map[6] = 3
while True:
    half = size_map[L]//2+1
    if L < 2:
        patty_sum += L1_map[X]
        break
    if X > half:
        patty_sum += patty_num_map[L-1]+1  # 真ん中のpattyの分
        # X -= half+1  # 一番上のバティの分
        X -= half
        L -= 1
    elif X < half:
        X -= 1  # 一番下のバンの分
        L -= 1
    else:  # ちょうど半分
        patty_sum += patty_num_map[L-1]+1
        break
print(patty_sum)

Submission status: Runtime Error

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

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, 3)[x] + [0, 1][cnt >= 1]
    if x == (al[idx] // 2) + 1:
        return ans + bl[idx - 1] + 1 + [0, 1][cnt >= 1]
    elif x > (al[idx] // 2) + 1:
        ans += bl[idx - 1]
        x -= (al[idx] // 2)
        cnt += 1
    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:
from sys import stdin
N,X=[int(x) for x in stdin.readline().rstrip().split()]
burger_set=["P"]
for i in range(N):
    new_burger="B"+burger_set[i]+"P"+burger_set[i]+"B"
    burger_set.append(new_burger)
print(burger_set[N][0:X].count('P'))

Submission status: Runtime Error

Solution code:
n,x = map(int,input().split())
l1='BPPPB'
for i in range(n-1):
    l2 = 'B'+l1+'P'+l1+'B'
    l1 = l2
y = l1[:x].count('P')
print(y)

Submission status: Runtime Error

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

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


def solve2(ans, x, idx, cnt):
    print(ans, x, idx)
    if idx == 1:
        return ans + (0, 0, 1, 2, 3, 3, 3)[x] + [0, 1][cnt >= 1]
    if x == (al[idx] // 2) + 1:
        return ans + bl[idx - 1] + 1 + [0, 1][cnt >= 1]
    elif x > (al[idx] // 2) + 1:
        ans += bl[idx - 1]
        x -= (al[idx] // 2)
        cnt += 1
    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())

# (i)XがLバーガーの半分より上かどうか？を計算
# (ii-i)上の場合: sumに下半分のパティの数を加算。下半分を切り捨てて、L-1バーガに対して、
#        再度X = (X-(Lバーガーの半分の枚数))で(i)から計算
# (ii-ii)下の場合: 上半分を切り捨てて、L-1バーガに対して再度X = (X-(Lバーガーの半分の枚数))で(i)から計算

patty_num_map = {}
size_map = {}
patty_num_map[0] = 1
size_map[0] = 1
for i in range(1, 51):
    patty_num_map[i] = 1+patty_num_map[i-1]*2
    size_map[i] = 3+size_map[i-1]*2

L = N
patty_sum = 0
L1_map = {}
L1_map[0] = 0
L1_map[1] = 0
L1_map[2] = 1
L1_map[3] = 2
L1_map[4] = 3
L1_map[5] = 3
while True:
    half = size_map[L]//2+1
    if L < 2:
        patty_sum += L1_map[X]
        break
    if X > half:
        patty_sum += patty_num_map[L-1]+1  # 真ん中のpattyの分
        # X -= half+1  # 一番上のバティの分
        X -= half
        L -= 1
    elif X < half:
        X -= 1  # 一番下のバンの分
        L -= 1
    else:  # ちょうど半分
        patty_sum += patty_num_map[L-1]+1
        break
print(patty_sum)

Submission status: Runtime Error

Solution code:
n,x = map(int,input().split())
l1='BPPPB'
for i in range(n-1):
    l2 = 'B'+l1+'P'+l1+'B'
    l1 = l2
print(l1[:x])
y = l1[:x].count('P')
print(y)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
bg = 'P'
for i in range(1,N+1):
    bg = 'B'+bg+'P'+bg+'B'
print(bg[-X:].count('P'))

Submission status: Runtime Error

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

def makeBurger(n):
    if n==1:
        return [1,3,1]
    else:
        burger=makeBurger(n-1)
        newBurger=[*burger,1,*burger]
        newBurger[0]+=1
        newBurger[-1]+=1
        # print(newBurger)
        return newBurger

burger=makeBurger(n)
size=0
index=0
cnt=0
while size<=x:
    layerSize=burger[index]
    if index%2:
        if size+layerSize>x:
            cnt+=x-size
            break
        else:
            cnt+=layerSize
    size+=layerSize
    index+=1
print(cnt)

Submission status: Runtime Error

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

def makeBurger(n):
    if n==1:
        return [1,3,1]
    else:
        burger=makeBurger(n-1)
        newBurger=[*burger,1,*burger]
        newBurger[0]+=1
        newBurger[-1]+=1
        print(newBurger)
        return newBurger

burger=makeBurger(n)
size=0
index=0
cnt=0
while size<=x:
    layerSize=burger[index]
    if index%2:
        if size+layerSize>x:
            cnt+=x-size
            break
        else:
            cnt+=layerSize
    size+=layerSize
    index+=1
print(cnt)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

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

burger = 'P'
for i in range(N):
    burger = 'B' + burger + 'P' + burger + 'B'

for s in range(X):
    if burger[s] == 'P':
        ans += 1

print(ans)

Submission status: Runtime Error

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

hamberger = "P"

for i in range(N):
    hamberger = "B"+ hamberger+"P"+hamberger+"B"
number = hamberger[0:X].count("P")
print(number)

Submission status: Runtime Error

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

hamberger = "P"

for i in range(N):
    hamberger = "B"+ hamberger+"P"+hamberger+"B"
print(hamberger)
number = hamberger[0:X].count("P")
print(number)

Submission status: Runtime Error

Solution code:
a,b= map(int,input().split())

l = 1

c = [1]
d = [1]

for i in range(50):
	c.append(2*c[-1]+3)
	d.append(2*d[-1]+1)
k = 0
while True:
	if b == 1 && a == 0:
		k += 1
		break
	elif b==1:
		break
	elif c[a] // 2 == b:
		k += d[a-1]
		break
	elif c[a] // 2 > b:
		a -= 1
		b -= 1
	elif c[a] // 2 + 1 == b:
		a -= 1
		k += 1 + d[a]
		break
	elif c[a] == b:
		k = d[a]
		break
	elif a == 0:
		k += 1
		break
	else:
		a -= 1
		k += d[a]+1
		b -= c[a] + 2


print(k)

Submission status: Runtime Error

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

def get_next_burger(current_burger) :
	return "B" + current_burger + "P" + current_burger + "B"

current = "P"

for i in range(N) :
	current = get_next_burger(current)

target = current[-X:]
print(target.count("P"))

Submission status: Runtime Error

Solution code:
temp = input().rstrip().split(" ")
N = int(temp[0])
X = int(temp[1])

BGR = "P"
list = [BGR]
for i in range(N):
    temp1 = "B"
    temp2 = "P"
    temp3 = "B"

    BGR = temp1 + BGR + temp2 + BGR + temp3
    list.append(BGR)

def call(B):
    s = B[-X:]
    return s.count("P")
answer = call(BGR)
print(answer)
print(list)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
burger = []
burger.append("P")
for i in range(1, N+1):
  burger.append("B" + burger[i-1] + "P" + burger[i-1] + "B")
count = 0
burger[N] = list(burger[N])
for i in range(X):
  if(burger[N][i] == "P"):
    count += 1
print(count)

Submission status: Runtime Error

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

def make_barger(str):
    return "0" + str + "1" + str + "0"
str = "1"
for i in range(n):
    str = make_barger(str)
cnt = 0
for s in str[-k:]:
    cnt += int(s)
print(cnt)

Submission status: Runtime Error

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

def burger(n):
    if n < 0:
        return False
    
    elif n == 0:
        return 'P'
    
    tmp = burger(n - 1)
    return 'B' + tmp + 'P' + tmp + 'B'


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

Submission status: Runtime Error

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

burger = 'P'
for i in range(N):
    burger = 'B' + burger + 'P' + burger + 'B'

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

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
# レベルNバーガー作る、 下からX層食べる


def next_burger(former_burger):
    return "0"+former_burger+"1"+former_burger+"0"

#レベルNバーガーの下からNまいはB.
#下半分を求めれば対照.
# P:1, B:0
burger = "1"
for i in range(N):
    burger = next_burger(burger)
    
score=0
for gu in burger[-X:]:
    if gu == "1":
        score+=1
print(score)

Submission status: Runtime Error

Solution code:



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 f[n] <= remain:
        total += g[n]
        remain -= f[n]
        return
    if f[n] > remain:
        remain -=1
        if f[n-1] > remain:
            eat(n-1)
        else:
            total += g[n-1]
            remain -= f[n-1]
            total += 1
            remain -= 1
            eat(n-1)

eat(n)
print(total)

Submission status: Runtime Error

Solution code:
def memoize(f):
    table = {}
    def func(x):
        if not X in table:
            table[x] = f(x)
        return table[x]
    return func

@memoize
def burger(l):
    if l == 0:
        return "P"
    else:
        b = burger(l - 1)
        return "B" + b + "P" + b + "B"

N, X = map(int, input().split())
bg = burger(N)
eat = bg[0:X]
print(eat.count("P"))

Submission status: Runtime Error

Solution code:
n, x = map(int, input().split())
hum = 'P'
a = 'B'
b = 'P'
ans = 0
eat = ''
if n == 1:
    print(ans)
else:
    for i in range(n):
        hum = a + hum + b + hum + a
    for j in range(x):
        eat += hum[j]
    ans = eat.count('P')
    print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
burger = []
for i in range(n + 1):
    if not i:
        burger.append('P')
    else:
        burger.append('B' + burger[i - 1] + 'P' + burger[i - 1] + 'B')

ans = 0
for i in range(k):
    if burger[n][i] == 'P':
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
A = 'P'
for i in range(1, N+1):
    A = 'B' + A + 'P' + A + 'B'
print(A[0:min(X,len(A))].count('P'))

Submission status: Runtime Error

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

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

bg = "p"
l = 0
while(len(bg) > X):
    bg = "b" + bg + "p" + bg + "b"
    l += 1

bg = bg[:X]

for n in range(N-l):
    bg = "b" + bg + "p" + bg + "b"
    bg = bg[:X]

count = 0
for x in range(X):
    if bg[x] == "p":
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
N, X = map(int, input().split())
bager = []
bager.append("P")

for i in range(1, N+1):
    bagernow = "B" + bager[i-1] + "P" + bager[i-1] + "B"
    bager.append(bagernow)

bagerN = str(bager[N])

runbager = bagerN[:(X)]

Ans = runbager.count("P")

print(Ans)

Submission status: Runtime Error

Solution code:
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 == 1:
        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))

Submission status: Runtime Error

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

def makeBurger(n):
    if n==1:
        return 'bpppb'
    else:
        burger=makeBurger(n-1)
        print(n)
        return 'b'+burger+'p'+burger+'b'

burger=makeBurger(n)
cnt=0
for i in range(x):
    if burger[i]=='p':
        cnt+=1
print(cnt)

Submission status: Runtime Error

Solution code:
def main():
    n,x = map(int, input().split())
    a = "P"
    for i in range(n):
        a = "B" + a + "P" + a + "B"
    c = 0
    for s in a[:x]:
        if s == "P":
            c+=1
    print(c)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n , k=map(int,input().split())
l="p"
b=0
for o in range(0,n):
 l="b"+l+"p"+l+"b"
p=list(l)
for t in range(0,k):
 if p[t]=="p":
  b+=1
print(b)

Submission status: Runtime Error

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

def make_hmg(l1, cnt):
    if not cnt:
        return l1
    return make_hmg('B' + l1 + 'P' + l1 + 'B', cnt-1)

hmg = make_hmg('P', N)
cnt = hmg[0:X].count('P')
print(cnt)

Submission status: Runtime Error

Solution code:
n,x=map(int,input().split())
burgar='p'
for i in range(n):
    burgar='b'+burgar+'p'+burgar+'b'
a=list(burgar)
ans=0
for j in a[:x]:
    if j=='p':
        ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
n , k=map(int,input().split())
l="p"
b=0
for o in range(0,n):
 l="b"+l+"p"+l+"b"
p=list(l)
for t in range(0,k):
 if p[t]==b:
  pass
 elif p[t]==p:
  b+=1
print(b)

Submission status: Runtime Error

Solution code:
pb_list = []
p_count = 0

def pb(n):
    if n == 0:
        pb_list.append('p')
        p_count = p_count + 1
        return
    elif n >= 0:
        pb_list.append('b')
        pb(n - 1)
        pb_list.append('p')
        p_count = p_count + 1
        pb(n - 1)
        pb_list.append('b')
    else:
        return

n, x = input().split(' ')
n = int(n)
x = int(x)
pb(n)

#for i in range(x):
#    if pb_list[i] == 'p':
#        p_count = p_count + 1

print(p_count)

Submission status: Runtime Error

Solution code:
l,x=map(int,input().split())
a='BPPPB'
for i in range(l):
    if i==0:
        pass
    else:
        a='P'+a+'B'+a+'P'
print(a[:x].count('P'))