Problem p02609 (python)

Fixed submission:
import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

def popcount(num):
    cnt = 0
    while num > 0:
        num %= bin(num).count('1')
        cnt += 1
    return cnt

def Main():
    n = read_int()
    x = read_str()
    x_int = int(x, 2)

    cnt = x.count('1')
    minus_cnt = cnt - 1
    plus_cnt = cnt + 1
    
    minus_mod = x_int % minus_cnt if minus_cnt != 0 else 0
    plus_mod = x_int % plus_cnt

    for i in range(n):
        if x[i] == '0':
            print(popcount((plus_mod + pow(2, n - 1 - i, plus_cnt)) % plus_cnt) + 1)
        elif minus_cnt != 0:
            print(popcount((minus_mod - pow(2, n - 1 - i, minus_cnt)) % minus_cnt) + 1)
        else:
            print(0)

if __name__ == '__main__':
    Main()

Buggy submission (verdict: Runtime Error):
def popcount(x):
    bitx = bin(x)
    cnt = bitx.count('1')
    return cnt

n = int(input())
x = list(input())
original_cnt = x.count('1')
stock = [1] * n

for i in range(1, n):
    stock[i] = stock[i - 1] * 2
stock.reverse()
original_num = 0
for i in range(n):
    original_num += int(x[i]) * stock[i]
cnt_one = original_cnt + 1
cnt_zero = original_cnt - 1

for i in range(n):
    tmp = original_num
    tmp2 = popcount
    if x[i] == '1':
        tmp -= stock[i]
        tmp2 = cnt_zero
    else:
        tmp += stock[i]
        tmp2 =cnt_one
    ans = 1
    num = tmp % tmp2
    while num != 0:
        ans += 1
        num %= popcount(num)
    print(ans)

Buggy submission (verdict: Runtime Error):
# def popcount(x):
#     bitx = bin(x)
#     cnt = bitx.count('1')
#     return cnt

n = int(input())
x = list(input())
popcount = x.count('1')
stock = [1] * (n + 1)

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


for i in range(n):
    tmp = x.copy()
    tmp2 = popcount
    if tmp[i] == '1':
        tmp[i] = '0'
        tmp2 -= 1
    else:
        tmp[i] = '1'
        tmp2 += 1
    num = 0
    if tmp[i] == '0':
        for i in range(n):
            num += tmp[i] * stock[i]
    
    if tmp2 == 0:
        print(0)
    elif num>0 and num % tmp2 == 0:
        print(1)
    elif tmp[n - 1] == '1' and tmp2 % 2 == 0 and tmp2 > 2:
        print(3)
    else:
        print(2)

Buggy submission (verdict: Runtime Error):
# def popcount(x):
#     bitx = bin(x)
#     cnt = bitx.count('1')
#     return cnt

n = int(input())
x = list(input())
popcount = x.count('1')
stock = [1] * (n + 1)

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


for i in range(n):
    tmp = x.copy()
    tmp2 = popcount
    if tmp[i] == '1':
        tmp[i] = '0'
        tmp2 -= 1
    else:
        tmp[i] = '1'
        tmp2 += 1
    num = 0
    if tmp[i] == '0':
        for i in range(n):
            num += tmp[i] * stock[i]
    
    if tmp2 == 0:
        print(0)
    elif num>0 andnum % tmp2 == 0:
        print(1)
    elif tmp[n - 1] == '1' and tmp2 % 2 == 0 and tmp2 > 2:
        print(3)
    else:
        print(2)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    bitx = bin(x)
    cnt = bitx.count('1')
    return cnt

n = int(input())
x = list(input())
for i in range(n):
    tmp = x.copy()
    if tmp[i] == '1':
        tmp[i] = '0'
    else:
        tmp[i] = '1'
    tmp = int(''.join(tmp), 2)
    tmp2 = popcount(tmp)
    if tmp % tmp2 == 0:
        print(1)
    elif tmp % 2 != 0 and tmp2 % 2 == 0:
        print(3)
    else:
        print(2)