Problem p02609 (python)

Fixed submission:
def popcount(n):
    bit_count = bin(n).count('1')
    n = n % bit_count 
    return n

if __name__ == "__main__":
    import sys 

    readline = sys.stdin.readline 

    N = int(input())
    X = readline()
    bit_count = X.count('1')
    mod1 = int(X, 2) % (bit_count + 1)
    mod2 = int(X, 2) % (bit_count - 1) if (bit_count - 1) != 0 else 0

    for i in range(N):
        if X[i] == '0':
            X_rest = (mod1 + pow(2, N - 1 - i, bit_count + 1)) % (bit_count + 1)
        else:
            if bit_count - 1 != 0:
                X_rest = (mod2 - pow(2, N - 1 - i, bit_count - 1)) % (bit_count - 1)
            else:
                print(0)
                continue

        counter = 1
        while X_rest != 0:
            X_rest = popcount(X_rest)
            counter += 1
        print(counter)

Buggy submission (verdict: Runtime Error):
def popcount(n):
    bit_count = bin(n).count('1')
    n = n % bit_count 
    return n

if __name__ == "__main__":
    import sys 

    readline = sys.stdin.readline 

    N = int(input())
    X = readline()
    bit_count = X.count('1')
    mod1 = int(X, 2) % (bit_count + 1)
    mod2 = int(X, 2) % (bit_count - 1)



    for i in range(N):
        if X[i] == '0':
            X_rest = (mod1 + pow(2, N - 1 - i, bit_count + 1)) % (bit_count + 1)
        else:
            if bit_count - 1 != 0:
                X_rest = (mod2 - pow(2, N - 1 - i, bit_count - 1)) % (bit_count - 1)
            else:
                print(0)
                continue

        counter = 1
        while X_rest != 0:
            X_rest = popcount(X_rest)
            counter += 1
        print(counter)

Buggy submission (verdict: Runtime Error):
def popcount(n):
    bit_count = bin(n).count('1')
    n = n % bit_count 
    return n

if __name__ == "__main__":
    import sys 

    readline = sys.stdin.readline 

    N = int(input())
    X = readline()
    bit_count = X.count('1')
    mod1 = int(X, 2) % (bit_count + 1)
    mod2 = int(X, 2) % (bit_count - 1)



    for i in range(N):
        X_rest = 0
        if X[i] == '0':
            X_rest = (mod1 + pow(2, N - 1 - i, bit_count + 1)) % (bit_count + 1)
        else:
            if bit_count - 1 != 0:
                X_rest = (mod2 - pow(2, N - 1 - i, bit_count - 1)) % (bit_count - 1)
            else:
                print(0)
                continue

        counter = 1
        while X_rest != 0:
            X_rest = popcount(X_rest)
            counter += 1
        print(counter)

Buggy submission (verdict: Runtime Error):
def popcount(n):
    bit_count = bin(n).count('1')
    n = n % bit_count 
    return n

if __name__ == "__main__":
    import sys 

    readline = sys.stdin.readline 

    N = int(input())
    X = readline()
    bit_count = X.count('1')
    X_int = int(X, 2)


    for i in range(N):
        # ビットの反転
        X_int_copy = X_int
        bit_count_n = bit_count
        if X[i] == '0':
            bit_count_n += 1
            X_int_copy += pow(2, N - 1 - i)
        else:
            bit_count_n -= 1
            X_int_copy -= pow(2, N - 1 - i)

        # print("反転後{}".format(X_int_copy))
        # ビット反転後に0になった場合
        if X_int_copy == 0:
            print(0)

        # 1回操作しておく
        popcount_X = X_int_copy % bit_count_n 
        counter = 1
        while popcount_X != 0:
            popcount_X = popcount(popcount_X)
            counter += 1

        print(counter)

Buggy submission (verdict: Runtime Error):
def popcount(n):
    bit_count = bin(n).count('1')
    n = n % bit_count 
    return n

if __name__ == "__main__":
    import sys 

    readline = sys.stdin.readline 

    N = int(input())
    X_str= readline()
    X = list(X_str)
    bit_count = X_str.count('1')
    X_int = int(X_str, 2)


    for i in range(N):
        # ビットの反転
        x_copy = X.copy()
        X_int_copy = X_int
        bit_count_n = bit_count
        if x_copy[i] == '0':
            x_copy[i] = '1'
            bit_count_n += 1
            X_int_copy += pow(2, N - 1 - i)
        else:
            x_copy[i] = '0'
            bit_count_n -= 1
            X_int_copy -= pow(2, N - 1 - i)

        # print("反転後{}".format(X_int_copy))
        # ビット反転後に0になった場合
        if X_int_copy == 0:
            print(0)

        # 1回操作しておく
        popcount_X = X_int_copy % bit_count_n 
        counter = 1
        while popcount_X != 0:
            popcount_X = popcount(popcount_X)
            counter += 1

        print(counter)

Buggy submission (verdict: Runtime Error):
import sys 

readline = sys.stdin.readline 

N = int(input())
X_str= input()
X = list(X_str)
bit_count = X_str.count('1')

def popcount(number, bit_counter):
    num = number
    counter = 0
    while num != 0:
        bit_con = bit_counter if counter == 0 else bin(num)[2:].count('1')
        num = num % bit_con
        counter += 1

    return counter

x_copy = X.copy()
for i in range(N):
    # ビットの反転
    # x_copy = X.copy()
    bit_count_n = bit_count
    if x_copy[i] == '0':
        x_copy[i] = '1'
        bit_count_n += 1
    else:
        x_copy[i] = '0'
        bit_count_n -= 1

    bit_int = int("".join(x_copy), 2)

    print(popcount(bit_int, bit_count_n))