Problem p02609 (python)

Fixed submission:
n = int(input())
s = input()
x = int(s, 2)
pcx = s.count("1")
pc1 = x % (pcx - 1) if pcx > 1 else 0
pc2 = x % (pcx + 1)

def f(j):
    cnt = 0
    while j:
        j %= bin(j).count("1")
        cnt += 1
    return cnt

for i, y in enumerate(s):
    k = n - 1 - i
    if y == "0":
        print(f((pc2 + pow(2, k, pcx + 1)) % (pcx + 1)) + 1)
    elif pcx > 1:
        print(f((pc1 - pow(2, k, pcx - 1)) % (pcx - 1)) + 1)
    else:
        print(0)

Buggy submission (verdict: Runtime Error):
n = int(input())
x_s = input()
x = int(x_s, 2)

def popcount(n):
    return bin(n).count("1")

pops = popcount(x)
x_pop_p = x%(pops + 1)
try:
    x_pop_m = x%(pops - 1)
except:
    x_pop_m = 0
for i in reversed(range(0,n)):
    if x_s[i] == '1':
        tmp = (x_pop_m - pow(2,i,(pops - 1)))%(pops - 1)
    else:
        tmp = (x_pop_p + pow(2,i,(pops + 1))) % (pops + 1)
    if tmp == 0:
        print(1)
        continue
    cnt = 2
    tmp %= popcount(tmp)
    while tmp > 0:
        cnt += 1
        if tmp == 0:
            print(cnt-1)
            continue
        tmp %= popcount(tmp)
    print(cnt)

Buggy submission (verdict: Runtime Error):
n = input()
x = int(input(),2)

def popcount(n):
    return n.count("1")

def popcount2(n):
    ans = 0
    while n:
        ans += n % 2
        n //= 2
    return ans

pops = popcount(x)
x_pop_p = x%(pops + 1)
try:
    x_pop_m = x%(pops - 1)
except:
    x_pop_m = 0
for i in reversed(range(0,n)):
    bit = x^(1<<i)
    if bit == 0:
        print(0)
        continue
    if x&(1<<i):
        tmp = (x_pop_m - pow(2,i,(pops - 1)))%(pops - 1)
    else:
        tmp = (x_pop_p + pow(2,i,(pops + 1))) % (pops + 1)
    if tmp == 0:
        print(1)
        continue
    cnt = 2
    tmp %= popcount(tmp)
    while tmp > 0:
        cnt += 1
        if tmp == 0:
            print(cnt-1)
            continue
        tmp %= popcount(tmp)
    print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = int(input(),2)

def popcount(n):
    return bin(n).count("1")

x_pop = popcount(x)
for i in reversed(range(0,n)):
    bit = x&(1<<i)
    if bit:
        tmp_pop = x_pop - 1
    else:
        tmp_pop = x_pop + 1
    tmp = bit % tmp_pop
    if tmp == 0:
        print(0)
        continue
    if tmp < 0:
        print(1)
        continue
    cnt = 2
    tmp = tmp % popcount(tmp)
    while tmp > 0:
        cnt += 1
        tmp %= popcount(tmp)
    print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = int(input(),2)

def popcount(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f
x_pop = popcount(x)
for i in reversed(range(0,n)):
    if x&(1<<i):
        tmp_pop = x_pop - 1
    else:
        tmp_pop = x_pop + 1
    tmp = (x^(1<<i)) % tmp_pop
    if tmp <= 0:
        print(1)
        continue
    cnt = 2
    tmp2 = tmp % popcount(tmp)
    while tmp2 > 0:
        tmp %= popcount(tmp)
        cnt += 1
        tmp2 %= popcount(tmp2)
    print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = int(input(),2)

def popcount(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f
x_pop = popcount(x)
for i in reversed(range(0,n)):
    if x&(1<<i):
        tmp_pop = x_pop - 1
    else:
        tmp_pop = x_pop + 1
    tmp2 = (x^(1<<i)) % tmp_pop
    cnt = 1
    while tmp2 > 0:
        tmp = tmp2
        tmp2 = tmp % popcount(tmp)
        cnt += 1
    print(cnt)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = int(input(),2)

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
for i in reversed(range(0,n)):
    tmp = x^(1<<i)
    tmp2 = tmp%popcount(tmp)
    cnt = 1
    while tmp2 > 0:
        tmp = tmp2
        tmp2 = tmp % popcount(tmp)
        cnt += 1
    print(cnt)