Problem p02609 (python)

Fixed submission:
import sys
input = sys.stdin.readline

def p_count(n):
    return bin(n)[2:].count('1')


n = int(input())
x = input().rstrip()
x_dec = int(x,base = 2)

pop_cnt = x.count('1')
pop_cnt_0 = (pop_cnt + 1)
pop_cnt_1 = (pop_cnt - 1)
x_0 = pow(x_dec,1,pop_cnt_0)
x_1 = pow(x_dec,1,pop_cnt_1) if pop_cnt_1 > 0 else 0

x_bit = [0]*n

for i in range(n-1,-1,-1):
    if x[i] == '0':
        x_bit[i] = (x_0 + pow(2,n-1-i,pop_cnt_0))%pop_cnt_0
        #x_bit[i] = (x_dec + pow(2,n-1-i))%pop_cnt_0
        
    else:
        if pop_cnt_1 > 0:
            x_bit[i] = (x_1 - pow(2,n-1-i,pop_cnt_1))
            x_bit[i] = x_bit[i] if x_bit[i] >= 0 else x_bit[i] + pop_cnt_1
        else:
            x_bit[i] = -1

anslist = []
for i in range(n):
    if x_bit[i] == -1:
        anslist.append(0)
        continue
    ans = 1 
    now = x_bit[i]
    while True:
        if now == 0:
            break
        now = now%p_count(now)
        
        ans += 1
    anslist.append(ans)


for ans in anslist:
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

def p_count(n):
    return bin(n).count('1')


n = int(input())
x = input().rstrip()
x_dec = int(x,base = 2)

pop_cnt = x.count('1')
pop_cnt_0 = pop_cnt + 1
pop_cnt_1 = pop_cnt - 1

x_bit = [0]*n

for i in range(n-1,-1,-1):
    if x[i] == '0':
        x_bit[i] = (x_dec + pow(2,n-1-i))%pop_cnt_0
        
    else:
        x_bit[i] = (x_dec - pow(2,n-1-i))%pop_cnt_1

anslist = []
for i in range(n):
    ans = 1
    now = x_bit[i]
    while True:
        if now == 0:
            break
        now = now%p_count(now)
        
        ans += 1
    anslist.append(ans)


for ans in anslist:
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n = int(input())
x = int(input().rstrip(),2)
#print(x)
anslist = []
pop = [0]*(int('1'*n,2)+1)

def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f
def toggle_nth_bit(num: int, n: int):
    return num ^ (1 << n)

for i in range(1,len(pop)):
    pop[i] = popcount(i)

for i in range(n):
    '''
    b = list(x)
    if b[n-i] == '1':
        b[n-i] = '0'
    else:
        b[n-i] = '1'
    p = int(''.join(b),2)
    '''
    p = toggle_nth_bit(x,i)
    #print(p)
    ans = 0
    while True:
        if p == 0:
            break
        p = p%pop[p]
        ans += 1
    anslist.append(ans)

for i in range(len(anslist)-1,-1,-1):
    print(anslist[i])

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
n = int(input())
x = int(input().rstrip(),2)
#print(x)
anslist = []

def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f
def toggle_nth_bit(num: int, n: int):
    return num ^ (1 << n)


for i in range(n):
    '''
    b = list(x)
    if b[n-i] == '1':
        b[n-i] = '0'
    else:
        b[n-i] = '1'
    p = int(''.join(b),2)
    '''
    p = toggle_nth_bit(x,i)
    #print(p)
    ans = 0
    while True:
        if p == 0:
            break
        p = p%popcount(p)
        ans += 1
    anslist.append(ans)

for i in range(len(anslist)-1,-1,-1):
    print(anslist[i])