Problem: 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?

Submission attempts (in order):
import bisect
n,x = map(int,input().split())


a = [1, 5, 13, 29, 61, 125, 253, 509, 1021, 2045, 4093, 8189, 16381, 32765, 65533, 131069, 262141, 524285, 1048573, 2097149, 4194301, 8388605, 16777213, 33554429, 67108861, 134217725, 268435453, 536870909, 1073741821, 2147483645, 4294967293, 8589934589, 17179869181, 34359738365, 68719476733, 137438953469, 274877906941, 549755813885, 1099511627773, 2199023255549, 4398046511101, 8796093022205, 17592186044413, 35184372088829, 70368744177661, 140737488355325, 281474976710653, 562949953421309, 1125899906842621, 2251799813685245, 4503599627370493]

b = [1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823, 2147483647, 4294967295, 8589934591, 17179869183, 34359738367, 68719476735, 137438953471, 274877906943, 549755813887, 1099511627775, 2199023255551, 4398046511103, 8796093022207, 17592186044415, 35184372088831, 70368744177663, 140737488355327, 281474976710655, 562949953421311, 1125899906842623, 2251799813685247]



def f(k,x,a,b):
    if x == 0:
        return 0
    elif k == 0:
        return 1
    elif k== 2:
        if x <= 4:
            return x-1
        else:
            return 3
    else:
        if x == (a[k]-1)//2+1:
            return b[k-1] + 1
        elif x < (a[k]-1)//2+1:
            if x <= k:
                return 0
            else:
                return f(k-1,x-1,a,b)
        elif x >= a[k]-k:
            return b[k]
        else:
            return b[k-1] + f(k-1,x-(a[k]-1)//2-1,a,b) + 1

print(f(n,x,a,b))
        


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


a = [1, 5, 13, 29, 61, 125, 253, 509, 1021, 2045, 4093, 8189, 16381, 32765, 65533, 131069, 262141, 524285, 1048573, 2097149, 4194301, 8388605, 16777213, 33554429, 67108861, 134217725, 268435453, 536870909, 1073741821, 2147483645, 4294967293, 8589934589, 17179869181, 34359738365, 68719476733, 137438953469, 274877906941, 549755813885, 1099511627773, 2199023255549, 4398046511101, 8796093022205, 17592186044413, 35184372088829, 70368744177661, 140737488355325, 281474976710653, 562949953421309, 1125899906842621, 2251799813685245, 4503599627370493]

b = [1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777215, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823, 2147483647, 4294967295, 8589934591, 17179869183, 34359738367, 68719476735, 137438953471, 274877906943, 549755813887, 1099511627775, 2199023255551, 4398046511103, 8796093022207, 17592186044415, 35184372088831, 70368744177663, 140737488355327, 281474976710655, 562949953421311, 1125899906842623, 2251799813685247]



def f(k,x,a,b):
    if x == 0:
        return 0
    elif k == 0:
        return 1
    elif k== 1:
        if x <= 4:
            return x-1
        else:
            return 3
    else:
        if x == (a[k]-1)//2+1:
            return b[k-1] + 1
        elif x < (a[k]-1)//2+1:
            if x <= k:
                return 0
            else:
                return f(k-1,x-1,a,b)
        elif x >= a[k]-k:
            return b[k]
        else:
            return b[k-1] + f(k-1,x-(a[k]-1)//2-1,a,b) + 1

print(f(n,x,a,b))
        


Verdicts: Wrong Answer

Accepted