Problem p02609 (python)

Fixed submission:
N = int(input())
x2 = input()
x10 = int(x2, 2)
popc =  x2.count("1")
#print("x10", x10)
#print("popc", popc)
#popc: Nを2進数で表したときの1の個数

#以下で1つフリップさせた時の入力に対する余りを事前に計算
if popc <= 1:
    xp = x10%(popc+1)
    xm = 0
else:
    xp = x10%(popc+1)
    xm = x10%(popc-1)
#print("xp", xp)
#print("xm", xm)

#以下でフリップさせた場所のみを計算
for i in range(N):
    if x2[i] == "1":
        if popc == 1:
            print(0)
            continue
        tmp = (xm - pow(2, N-i-1, popc-1))%(popc-1)
    else:
        tmp = (xp + pow(2, N-i-1, popc+1))%(popc+1)
        
    #print("tmp", tmp)
    ans = 1
    while tmp > 0:
        tmp %= bin(tmp).count("1")
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = str(input())
X10 = int(X,2)

one_num = X.count("1")
X10_pl = X10%(one_num+1)
X10_mi = X10%(one_num-1)

for i in range(N):
    target = int(X[i])
    
    if target == 0:
        ones = one_num + 1
        plus = pow(2,N-i-1,ones)
        Xnow = (X10_pl + plus)%ones
        
    else:
        if one_num == 1:
            print(0)
            continue
        ones = one_num-1
        plus = -pow(2,N-i-1,ones)
        Xnow = (X10_mi + plus)%ones

    count=1
    
    while Xnow > 0:
        Xnow2 = bin(Xnow)
        ones = Xnow2.count("1")
        Xnow = Xnow%ones
        count+=1
        
    print(count)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = str(input())
X10 = int(X,2)
one_num = X.count("1")



for i in range(N):
    target = int(X[i])
    if target == 0:
        ones = one_num + 1
        plus = pow(2,N-i-1,ones)
        
    else:
        ones = one_num-1
        plus = -pow(2,N-i-1,ones)
        
    Xnow = X10+plus
    count = 0
    if Xnow == 0:
        print(count)
        continue
    else:
        Xnow = Xnow%ones
        count+=1
        while Xnow > 0:
            Xnow2 = bin(Xnow)
            ones = Xnow2.count("1")
            Xnow = Xnow%ones
            count+=1
        print(count)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = str(input())
X10 = int(X,2)

one_num = X.count("1")
xp = X10 % (one_num+1)
xm = X10 % (one_num-1)

for i in range(N):
    target = int(X[i])
    count=0
    if target == 0:
        plus = pow(2,N-i-1,xp)
    else:
        plus = -pow(2,N-i-1,xm)
    if X10+plus == 0:
        print(count)
        continue
    if target == 0:
        ones = one_num+1
        Xnow = (xp + plus)%ones
    else:
        ones = one_num-1
        Xnow = (xm + plus)%ones
    count += 1
    while Xnow > 0:
        Xnow2 = bin(Xnow)
        ones = Xnow2.count("1")
        Xnow = Xnow%ones
        count+=1
    print(count)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = str(input())
X10 = int(X,2)

one_num = X.count("1")
xp = X10 % (one_num+1)
xm = X10 % (one_num-1)

for i in range(N):
    target = int(X[i])
    count=0
    if target == 0:
        plus = pow(2,N-i-1)
    else:
        plus = -pow(2,N-i-1)
    if X10+plus == 0:
        print(count)
        continue
    if target == 0:
        ones = one_num+1
        Xnow = (xp + plus)%ones
    else:
        ones = one_num-1
        Xnow = (xm + plus)%ones
    count += 1
    while Xnow > 0:
        Xnow2 = bin(Xnow)
        ones = Xnow2.count("1")
        Xnow = Xnow%ones
        count+=1
    print(count)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = str(input())
X10 = int(X,2)

one_num = X.count("1")
one_mod = one_nom

for i in range(N):
    target = int(X[i])
    if target == 0:
        plus = pow(2,N-i-1)
        ones = one_num + 1
    else:
        plus = -pow(2,N-i-1)
        ones = one_num-1
    Xnow = X10+plus
    count = 0
    if Xnow == 0:
        print(count)
        continue
    Xnow = Xnow%ones
    count+=1
    for j in range(1000):
        if Xnow == 0:
            break
        Xnow2 = bin(Xnow)
        ones = Xnow2.count("1")
        Xnow = Xnow%ones
        count+=1
    print(count)

Buggy submission (verdict: Runtime Error):
N = int(input())
X = str(input())
X10 = int(X,2)

one_num = X.count("1")
answer = []
for i in range(N):
    target = int(X[i])
    if target == 0:
        plus = pow(2,N-i-1)
        ones = one_num + 1
    else:
        plus = -pow(2,N-i-1)
        ones = one_num-1
    Xnow = X10+plus
    count = 0
    if Xnow == 0:
        answer.append(count)
        continue
    else:
        Xnow = Xnow%ones
        count+=1
        while Xnow > 0:
            Xnow2 = bin(Xnow)
            ones = Xnow2.count("1")
            Xnow = Xnow%ones
            count+=1
        answer.append(count)
    print(*count,sep="\n")

Buggy submission (verdict: Runtime Error):
from collections import Counter
N = int(input())
X = str(input())
X10 = int(X,2)

one_num = Counter(X)["1"]

for i in range(N):
    target = int(X[i])
    if target == 0:
        plus = pow(2,N-i-1)
        ones = one_num + 1
    else:
        plus = -pow(2,N-i-1)
        ones = one_num-1
    
    Xnow = X10+plus
    count = 0
    if Xnow == 0:
        print(count)
        continue
    else:
        Xnow = Xnow%ones
        count+=1
        while Xnow > 0:
            Xnow2 = format(Xnow, 'b')
            ones = 0
            for j in range(len(Xnow2)):
                if Xnow2[j]==1:
                    ones+=1
            Xnow = Xnow%ones
            count+=1
        print(count)