Problem p02609 (python)

Fixed submission:
n = int(input())
x = list(input())

def popcount(n):
    bin_n = bin(n)[2:]
    count = 0
    for i in bin_n:
        count += int(i)
    return count

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

lst = [0 for i in range(n+1)]
for i in range(1, n+1):
    num = i%popcount(i)
    lst[i] = lst[num] + 1

plus = [0 for i in range(n)]
minus = [0 for i in range(n)]
plus[0] = 1
if cnt != 1:
    minus[0] = 1
for i in range(1, n):
    plus[i] = (plus[i-1]*2) % (cnt+1)
    if cnt != 1:
        minus[i] = (minus[i-1]*2) % (cnt-1)

origin = int(''.join(x), base=2)
amariplus = origin % (cnt+1)
if cnt != 1:
    amariminus = origin % (cnt-1)

for i in range(n):
    if x[i] == '0':
        amari = (amariplus + plus[n-i-1]) % (cnt+1)
        print(lst[amari] + 1)
    else:
        if cnt != 1:
            amari = (amariminus - minus[n-i-1]) % (cnt-1)
            print(lst[amari] + 1)
        else:
            print(0)

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

def popcount(n):
    bin_n = bin(n)[2:]
    count = 0
    for i in bin_n:
        count += int(i)
    return count

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

lst = [0 for i in range(n+1)]
for i in range(1, n+1):
    num = i%popcount(i)
    lst[i] = lst[num] + 1

plus = [0 for i in range(n)]
minus = [0 for i in range(n)]
plus[0] = 1
if cnt != 1:
    minus[0] = 1
for i in range(1, n):
    plus[i] = (plus[i-1]*2) % (cnt+1)
    if cnt != 1:
        minus[i] = (minus[i-1]*2) % (cnt-1)

origin = int(''.join(x), base=2)
amariplus = origin % (cnt+1)
amariminus = origin % (cnt-1)

for i in range(n):
    if x[i] == '0':
        amari = (amariplus + plus[n-i-1]) % (cnt+1)
        print(lst[amari] + 1)
    else:
        if cnt != 1:
            amari = (amariminus - minus[n-i-1]) % (cnt-1)
            print(lst[amari] + 1)
        else:
            print(0)

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

def popcount(n):
    bin_n = bin(n)[2:]
    count = 0
    for i in bin_n:
        count += int(i)
    return count

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

lst = [0 for i in range(n+1)]
for i in range(1, n+1):
    num = i%popcount(i)
    lst[i] = lst[num] + 1

plus = [0 for i in range(n)]
minus = [0 for i in range(n)]
plus[0] = 1
minus[0] = 1
for i in range(1, n):
    plus[i] = (plus[i-1]*2) % (cnt+1)
    minus[i] = (minus[i-1]*2) % (cnt-1)

origin = int(''.join(x), base=2)
amariplus = origin % (cnt+1)
amariminus = origin % (cnt-1)

for i in range(n):
    if x[i] == '0':
        amari = (amariplus + plus[n-i-1]) % (cnt+1)
        print(lst[amari] + 1)
    else:
        amari = (amariminus - minus[n-i-1]) % (cnt-1)
        print(lst[amari] + 1)

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

def popcount(nn):
    bin_n = bin(nn)[2:]
    count = 0
    for i in bin_n:
        count += int(i)
    return count

cnt = popcount(int(''.join(x), base=2))

dic = {}
for i in range(n):
    temp = x[:]
    if temp[i] == '0':
        temp[i] = '1'
        cnt_temp = cnt + 1
    else:
        temp[i] = '0'
        cnt_temp = cnt - 1
    temp = ''.join(temp)
    num = int(temp, base=2)
    num = num % cnt_temp
    lst = [num]
    ans = 1
    while num != 0:
        if num in dic:
            ans += dic[num]
            for i in range(len(lst)):
                dic[lst.pop()] = dic[num] + i
            break
        ans += 1
        popcnt = popcount(num)
        num %= popcnt
        lst.append(num)
    for i in range(len(lst)):
        dic[lst.pop()] = i
    print(ans)

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

def popcount(n):
    bin_n = bin(n)[2:]
    count = 0
    for i in bin_n:
        count += int(i)
    return count

cnt = popcount(int(''.join(x), base=2))

dic = {}
for i in range(n):
    temp = x[:]
    if temp[i] == '0':
        temp[i] = '1'
        cnt_temp = cnt + 1
    else:
        temp[i] = '0'
        cnt_temp = cnt - 1
    temp = ''.join(temp)
    num = int(temp, base=2)
    num = num % cnt_temp
    lst = [num]
    ans = 1
    while num != 0:
        if num in dic:
            ans += dic[num]
            for i in range(len(lst)):
                dic[lst.pop()] = dic[num] + i
            break
        ans += 1
        num = num%popcount(num)
        lst.append(num)
    for i in range(len(lst)):
        dic[lst.pop()] = i
    print(ans)