Problem p02609 (python)

Fixed submission:
N = int(input())
x = input()

cnt_x = x.count("1")
px10 = 0
nx10 = 0
ans = [0] * (N+1)

for i in range(1, N+1): #Nまで先に出しておく
    popcnt = format(i,"b").count("1")
    fx = i % popcnt
    ans[i] = ans[fx] + 1

for i in range(N):
    px10 += (int(x[i]) * pow(2,N-1-i, cnt_x+1)) % (cnt_x+1)#0⇒1となった場合の合計
    if cnt_x != 0 and cnt_x != 1:
        nx10 += int(x[i])*pow(2,N-1-i,cnt_x-1) % (cnt_x-1)#1⇒0となった場合の合計
#print(px10)
#print(nx10)
for i in range(N):
    if x[i] == "0":
        xi10 = (px10+pow(2,N-1-i,cnt_x+1))%(cnt_x+1)
        print(ans[xi10]+1)
    else:
        if cnt_x != 1:
            xi10 = (nx10-pow(2,N-1-i,cnt_x-1))%(cnt_x-1)
            print(ans[xi10]+1)
        else:
            print(0)

Buggy submission (verdict: Runtime Error):
import sys
input = lambda:sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)

def solve(k, cnt, Ans):
    S = str(bin(k)).count("1") 
    L = k % S
    if L < len(Ans)+1:
        return Ans[L]
    elif L == 0:
        return 1
    cnt += solve(L,cnt, Ans)
    return cnt 

N = int(input())
P = input()
I = P.count("1")
Df = int(P,2)
Ans = [1]

for i in range(1, I+1):
    cnt = 1
    Ans.append(solve(i, cnt, Ans)+1)
#print(Ans)

for i in range(N-1):
    if P[i] == "1":
        tmp = Df - 2**(N-i-1)
        cur = I - 1
    else:
        tmp = Df + 2**(N-i-1)
        cur = I + 1 
    print(Ans[tmp%cur])
    #print(solve(tmp,cnt))

if P[N-1] == "1":
    tmp = Df - 1
else:
    tmp = Df + 1
cur = str(bin(tmp)).count("1")
print(Ans[tmp%cur])
#print(solve(N,cnt))

Buggy submission (verdict: Runtime Error):
import sys
input = lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
sys.setrecursionlimit(10**9)

def solve(k, cnt):  
    S = str(bin(k)).count("1") 
    L = k % S
    if L == 0:
        return 1
    cnt += solve(L,cnt)
    return cnt 

N = int(input())
P = input()
Df = int(P,2)
for i in range(N-1):
    cnt = 1
    if P[i] == "1":
        tmp = Df - 2**(N-i-1)
    else:
        tmp = Df + 2**(N-i-1)    
    #print(tmp, bin(tmp))
    print(solve(tmp,cnt))
    
cnt = 1
if P[-1] == "1":
    tmp = Df - 1
else:
    tmp = Df + 1
print(solve(tmp,cnt))
#print(solve(N,cnt))

Buggy submission (verdict: Runtime Error):
import sys
input = lambda:sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**7)

def solve(k, cnt, Ans):
    S = str(bin(k)).count("1") 
    L = k % S
    if L < len(Ans)-1:
        return Ans[L]
    elif L == 0:
        return 1
    cnt += solve(L,cnt)
    return cnt 

N = int(input())
P = input()
I = P.count("1")
Df = int(P,2)
Ans = [1]

for i in range(1, I+1):
    cnt = 1
    Ans.append(solve(i, cnt, Ans)+1)
#print(Ans)

for i in range(N-1):
    if P[i] == "1":
        tmp = Df - 2**(N-i-1)
        cur = I-1
    else:
        tmp = Df + 2**(N-i-1)
        cur = I + 1 
    print(Ans[tmp%cur])
    #print(solve(tmp,cnt))

if P[N-1] == "1":
    tmp = Df - 1
else:
    tmp = Df + 1
cur = str(bin(tmp)).count("1")
print(Ans[tmp%cur])
#print(solve(N,cnt))

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


def solve(k, cnt):
    S = str(bin(k)).count("1") 
    L = k % S
    if L == 0:
        return 1
    cnt += solve(L,cnt)
    return cnt 

N = int(input())
P = input()
I = P.count("1")
Df = int(P,2)
Ans = [1]
for i in range(1, I+1):
    cnt = 1
    Ans.append(solve(i, cnt)+1)
#print(Ans)

for i in range(N-1):
    if P[i] == "1":
        tmp = Df - 2**(N-i-1)
    else:
        tmp = Df + 2**(N-i-1)    
    cur = str(bin(tmp)).count("1")    
    print(Ans[tmp%cur])
    #print(solve(tmp,cnt))

if P[N-1] == "1":
    tmp = Df - 1
else:
    tmp = Df
cur = str(bin(tmp)).count("1")
print(Ans[tmp%cur]+1)
#print(solve(N,cnt))

Buggy submission (verdict: Runtime Error):
import sys
input = lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
sys.setrecursionlimit(10**7)

def solve(k, cnt):  
    S = str(bin(k)).count("1") 
    L = k % S
    if L == 0:
        return 1
    cnt += solve(L,cnt)
    return cnt 

N = int(input())
P = input()
Df = int(P,2)
for i in range(N-1):
    cnt = 1
    if P[i] == "1":
        tmp = Df - 2**(N-i-1)
    else:
        tmp = Df + 2**(N-i-1)    
    #print(tmp, bin(tmp))
    print(solve(tmp,cnt))
    
cnt = 1
if P[-1] == "1":
    tmp = Df - 1
else:
    tmp = Df + 1
print(solve(tmp,cnt))
#print(solve(N,cnt))

Buggy submission (verdict: Runtime Error):
import sys
input = lambda:sys.stdin.readline().rstrip()
from collections import defaultdict

sys.setrecursionlimit(10**7)
def solve(k, cnt):
    S = str(bin(k))[2:].zfill(k).count("1") 
    L = k % S
    if L == 0:
        return 1
    cnt += solve(L,cnt)
    return cnt 

N = int(input())
P = input()
for i in range(N):
    cnt = 1
    if P[i] == "1":
        tmp = int(P,2) - 2**(N-i-1)
    else:
        tmp = int(P,2) + 2**(N-i-1)    
    #print(tmp, bin(tmp))
    print(solve(tmp,cnt))
    
#print(solve(N,cnt))