Buggy python submission (verdict: Runtime Error):
N = int(input())
X = input()
flag1 = 0
flag0 = 0


def popcount(n: int):
    bin_n = bin(n)
    return str(bin_n).count('1')


popcount_X = popcount(int(X, 2))

for i in range(N):
    count = 0
    if X[i] == '1':
        tmp = int(X)
        tmp -= 10 ** (N - i - 1)
        flag1 += 1
    else:
        tmp = int(X)
        tmp += 10 ** (N - i - 1)
        flag0 += 1
    tmp = int(str(tmp), 2)

    while True:
        if tmp == 0:
            print(count)
            break
        elif flag1:
            count += 1
            tmp = tmp % (popcount_X-1)
            flag1 = 0
        elif flag0:
            count += 1
            tmp = tmp % (popcount_X+1)
            flag0 = 0
        else:
            count += 1
            tmp = tmp % popcount(tmp)


Fixed submission:
N = int(input())
S = input()
X = int(S, 2)
pcx = S.count('1')
# ビット反転で1が0になる場合
pc1 = X % (pcx - 1) if pcx > 1 else 0
# ビット反転で0が1になる場合
pc2 = X % (pcx + 1)


def f(j):
    cnt = 0
    while j:
        j %= bin(j).count('1')
        cnt += 1
    return cnt


for i, y in enumerate(S):
    k = N - i - 1
    if y == "0":  # modを分配するときは、分配してからもう１回modを作用させれば良い
        print(f((pc2 + pow(2, k, pcx + 1)) % (pcx + 1)) + 1)
    elif pcx > 1:
        print(f((pc1 - pow(2, k, pcx - 1)) % (pcx - 1)) + 1)
    else:
        print(0)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
X = input()

dp = [-1] * 200001
dp[0] = 0
dp[1] = 1
dp[2] = 1
dp[3] = 2
dp[4] = 1
dp[5] = 2
dp[6] = 1
dp[7] = 2
dp[8] = 1
dp[9] = 2
dp[10] = 1


def popcount(n: int):
    bin_n = bin(n)
    return str(bin_n).count('1')


for i in range(N):
    count = 0
    if X[i] == '1':
        tmp = int(X)
        tmp -= 10 ** (N - i - 1)
    else:
        tmp = int(X)
        tmp += 10 ** (N - i - 1)
    tmp = int(str(tmp), 2)

    while True:
        if tmp == 0:
            print(count)
            break
        else:
            count += 1
            tmp = tmp % popcount(tmp)
            origin = tmp
            if dp[tmp] != -1:
                if tmp != origin:
                    dp[origin] = count + dp[tmp]
                print(count + dp[tmp])
                break
            else:
                count += 1
                tmp = tmp % popcount(tmp)


Fixed submission:
N = int(input())
S = input()
X = int(S, 2)
pcx = S.count('1')
# ビット反転で1が0になる場合
pc1 = X % (pcx - 1) if pcx > 1 else 0
# ビット反転で0が1になる場合
pc2 = X % (pcx + 1)


def f(j):
    cnt = 0
    while j:
        j %= bin(j).count('1')
        cnt += 1
    return cnt


for i, y in enumerate(S):
    k = N - i - 1
    if y == "0":  # modを分配するときは、分配してからもう１回modを作用させれば良い
        print(f((pc2 + pow(2, k, pcx + 1)) % (pcx + 1)) + 1)
    elif pcx > 1:
        print(f((pc1 - pow(2, k, pcx - 1)) % (pcx - 1)) + 1)
    else:
        print(0)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
X = input()

dp = [-1] * 200001
dp[0] = 0
dp[1] = 1
dp[2] = 1
dp[3] = 2
dp[4] = 1
dp[5] = 2
dp[6] = 1
dp[7] = 2
dp[8] = 1
dp[9] = 2
dp[10] = 1


def popcount(n: int):
    bin_n = bin(n)
    return str(bin_n).count('1')


for i in range(N):
    count = 0
    if X[i] == '1':
        tmp = int(X)
        tmp -= 10 ** (N - i - 1)
    else:
        tmp = int(X)
        tmp += 10 ** (N - i - 1)
    tmp = int(str(tmp), 2)

    while True:
        if tmp == 0:
            print(count)
            break
        else:
            count += 1
            tmp = tmp % popcount(tmp)
            origin = tmp
            if dp[tmp] != -1:
                print(count + dp[tmp])
                break
            else:
                count += 1
                tmp = tmp % popcount(tmp)


Fixed submission:
N = int(input())
S = input()
X = int(S, 2)
pcx = S.count('1')
# ビット反転で1が0になる場合
pc1 = X % (pcx - 1) if pcx > 1 else 0
# ビット反転で0が1になる場合
pc2 = X % (pcx + 1)


def f(j):
    cnt = 0
    while j:
        j %= bin(j).count('1')
        cnt += 1
    return cnt


for i, y in enumerate(S):
    k = N - i - 1
    if y == "0":  # modを分配するときは、分配してからもう１回modを作用させれば良い
        print(f((pc2 + pow(2, k, pcx + 1)) % (pcx + 1)) + 1)
    elif pcx > 1:
        print(f((pc1 - pow(2, k, pcx - 1)) % (pcx - 1)) + 1)
    else:
        print(0)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
X = input()

dp = [0] * 200001


def popcount(n: int):
    bin_n = bin(n)
    return str(bin_n).count('1')


for i in range(N):
    count = 0
    if X[i] == '1':
        tmp = int(X)
        tmp -= 10 ** (N - i - 1)
    else:
        tmp = int(X)
        tmp += 10 ** (N - i - 1)

    tmp = int(str(tmp), 2)

    while True:
        if tmp == 0:
            print(count)
            break
        else:
            count += 1
            tmp = tmp % popcount(tmp)
            origin = tmp
            if tmp == 0:
                dp[origin] = count
                print(count)
                break
            elif tmp == 1:
                dp[origin] = count + 1
                print(count + 1)
                break
            elif tmp == 2:
                dp[origin] = count + 1
                print(count + 1)
                break
            elif dp[tmp] != 0:
                dp[origin] = count + dp[tmp]
                print(count + dp[tmp])
                break
            else:
                count += 1
                tmp = tmp % popcount(tmp)


Fixed submission:
N = int(input())
S = input()
X = int(S, 2)
pcx = S.count('1')
# ビット反転で1が0になる場合
pc1 = X % (pcx - 1) if pcx > 1 else 0
# ビット反転で0が1になる場合
pc2 = X % (pcx + 1)


def f(j):
    cnt = 0
    while j:
        j %= bin(j).count('1')
        cnt += 1
    return cnt


for i, y in enumerate(S):
    k = N - i - 1
    if y == "0":  # modを分配するときは、分配してからもう１回modを作用させれば良い
        print(f((pc2 + pow(2, k, pcx + 1)) % (pcx + 1)) + 1)
    elif pcx > 1:
        print(f((pc1 - pow(2, k, pcx - 1)) % (pcx - 1)) + 1)
    else:
        print(0)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
X = input()


def popcount(n: int):
    bin_n = bin(n)
    return str(bin_n).count('1')


for i in range(N):
    count = 0
    if X[i] == '1':
        tmp = int(X)
        tmp -= 10 ** (N - i - 1)
    else:
        tmp = int(X)
        tmp += 10 ** (N - i - 1)

    tmp = int(str(tmp), 2)
    origin = tmp

    while True:
        if tmp == 0:
            print(count)
            break
        elif tmp == 1:
            print(count + 1)
            break
        elif tmp == 2:
            print(count + 1)
            break
        else:
            count += 1
            tmp = tmp % popcount(tmp)


Fixed submission:
N = int(input())
S = input()
X = int(S, 2)
pcx = S.count('1')
# ビット反転で1が0になる場合
pc1 = X % (pcx - 1) if pcx > 1 else 0
# ビット反転で0が1になる場合
pc2 = X % (pcx + 1)


def f(j):
    cnt = 0
    while j:
        j %= bin(j).count('1')
        cnt += 1
    return cnt


for i, y in enumerate(S):
    k = N - i - 1
    if y == "0":  # modを分配するときは、分配してからもう１回modを作用させれば良い
        print(f((pc2 + pow(2, k, pcx + 1)) % (pcx + 1)) + 1)
    elif pcx > 1:
        print(f((pc1 - pow(2, k, pcx - 1)) % (pcx - 1)) + 1)
    else:
        print(0)

Buggy python submission (verdict: Runtime Error):
N = int(input())
X = input()

dp = [0] * (2 ** N + 1)


def popcount(n: int):
    bin_n = bin(n)
    return str(bin_n).count('1')


for i in range(N):
    count = 0
    if X[i] == '1':
        tmp = int(X)
        tmp -= 10 ** (N - i - 1)
    else:
        tmp = int(X)
        tmp += 10 ** (N - i - 1)

    tmp = int(str(tmp), 2)
    origin = tmp

    while True:
        if tmp == 0:
            dp[origin] = count
            print(count)
            break
        elif tmp == 1:
            dp[origin] = count + 1
            print(count + 1)
            break
        elif tmp == 2:
            dp[origin] = count + 1
            print(count + 1)
            break
        elif dp[tmp] != 0:
            dp[origin] = count + dp[tmp]
            print(count + dp[tmp])
            break
        else:
            count += 1
            tmp = tmp % popcount(tmp)


Fixed submission:
N = int(input())
S = input()
X = int(S, 2)
pcx = S.count('1')
# ビット反転で1が0になる場合
pc1 = X % (pcx - 1) if pcx > 1 else 0
# ビット反転で0が1になる場合
pc2 = X % (pcx + 1)


def f(j):
    cnt = 0
    while j:
        j %= bin(j).count('1')
        cnt += 1
    return cnt


for i, y in enumerate(S):
    k = N - i - 1
    if y == "0":  # modを分配するときは、分配してからもう１回modを作用させれば良い
        print(f((pc2 + pow(2, k, pcx + 1)) % (pcx + 1)) + 1)
    elif pcx > 1:
        print(f((pc1 - pow(2, k, pcx - 1)) % (pcx - 1)) + 1)
    else:
        print(0)