Buggy python submission (verdict: Runtime Error):
def popcnt(n):
    return bin(n).count("1")

def f(n):
    cnt = 0
    while n>0:
        n = n%popcnt(n)
        cnt += 1
    return cnt

def solve(X):
    Y = int(X, 2)
    c = X.count("1")
    if c == 0:
        return 0
    m, p = c-1, c+1
    Ym, Yp = Y%m, Y%p
    for i in range(N):
        if X[i] == "1":
            Y = (Ym - pow(2,N-i-1,m))%m
        else:
            Y = (Yp + pow(2,N-i-1,p))%p
        print(f(Y) + 1)

N = int(input())
solve(input())



Fixed submission:
def popcnt(n):
    return bin(n).count("1")

def f(n):
    cnt = 0
    while n>0:
        n = n%popcnt(n)
        cnt += 1
    return cnt

def solve(X):
    Y = int(X, 2)
    c = X.count("1")
    m, p = c-1, c+1
    Ym, Yp = Y%m if m else -1, Y%p
    for i in range(N):
        if X[i] == "1":
            Y = (Ym - pow(2,N-i-1,m))%m if Ym!=-1 else -1
        else:
            Y = (Yp + pow(2,N-i-1,p))%p
        print(f(Y) + 1 if Y!=-1 else 0)

N = int(input())
solve(input())

Buggy python submission (verdict: Time Limit Exceeded):


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

def f(n):
    k = n
    cnt = 0
    while k>0:
        k = k%popcnt(k)
        cnt+= 1
    return cnt

def solve(X):
    for i in reversed(range(N)):
        if P[N-1-i] == 0:
            print(0)
        else:
            K = X ^ (1 << i)
            K2 = K%P[N-1-i]
            print(f(K2) + 1)




N = int(input())
X = input()
X = X.zfill(N)
count_1 = X.count("1")

P = [0]*N
for i in range(N):
    if X[i] == "1":
        P[i] = count_1 - 1
    else:
        P[i] = count_1 + 1
Y = int(X,2)

solve(Y)

Fixed submission:
def popcnt(n):
    return bin(n).count("1")

def f(n):
    cnt = 0
    while n>0:
        n = n%popcnt(n)
        cnt += 1
    return cnt

def solve(X):
    Y = int(X, 2)
    c = X.count("1")
    m, p = c-1, c+1
    Ym, Yp = Y%m if m else -1, Y%p
    for i in range(N):
        if X[i] == "1":
            Y = (Ym - pow(2,N-i-1,m))%m if Ym!=-1 else -1
        else:
            Y = (Yp + pow(2,N-i-1,p))%p
        print(f(Y) + 1 if Y!=-1 else 0)

N = int(input())
solve(input())

Buggy python submission (verdict: Runtime Error):


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

def f(n):
    k = n
    cnt = 0
    while k>0:
        k = k%popcnt(k)
        cnt+= 1
    return cnt

def solve(X):
    for i in reversed(range(N)):
        if [N-1-i] == 0:
            print(0)
        else:
            K = X ^ (1 << i)
            K2 = K%P[N-1-i]
            print(f(K2) + 1)




N = int(input())
X = input()
X = X.zfill(N)
count_1 = X.count("1")

P = [0]*N
for i in range(N):
    if X[i] == "1":
        P[i] = count_1 - 1
    else:
        P[i] = count_1 + 1
Y = int(X,2)

solve(Y)

Fixed submission:
def popcnt(n):
    return bin(n).count("1")

def f(n):
    cnt = 0
    while n>0:
        n = n%popcnt(n)
        cnt += 1
    return cnt

def solve(X):
    Y = int(X, 2)
    c = X.count("1")
    m, p = c-1, c+1
    Ym, Yp = Y%m if m else -1, Y%p
    for i in range(N):
        if X[i] == "1":
            Y = (Ym - pow(2,N-i-1,m))%m if Ym!=-1 else -1
        else:
            Y = (Yp + pow(2,N-i-1,p))%p
        print(f(Y) + 1 if Y!=-1 else 0)

N = int(input())
solve(input())

Buggy python submission (verdict: Runtime Error):


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

def f(n):
    k = n
    cnt = 0
    while k>0:
        k = k%popcnt(k)
        cnt+= 1
    return cnt

def solve(X):
    for i in reversed(range(N)):
        K = X ^ (1 << i)
        K2 = K%P[N-1-i]
        print(f(K2) + 1)




N = int(input())
X = input()
X = X.zfill(N)
count_1 = X.count("1")

P = [0]*N
for i in range(N):
    if X[i] == "1":
        P[i] = count_1 - 1
    else:
        P[i] = count_1 + 1
Y = int(X,2)

solve(Y)

Fixed submission:
def popcnt(n):
    return bin(n).count("1")

def f(n):
    cnt = 0
    while n>0:
        n = n%popcnt(n)
        cnt += 1
    return cnt

def solve(X):
    Y = int(X, 2)
    c = X.count("1")
    m, p = c-1, c+1
    Ym, Yp = Y%m if m else -1, Y%p
    for i in range(N):
        if X[i] == "1":
            Y = (Ym - pow(2,N-i-1,m))%m if Ym!=-1 else -1
        else:
            Y = (Yp + pow(2,N-i-1,p))%p
        print(f(Y) + 1 if Y!=-1 else 0)

N = int(input())
solve(input())