Problem p02609 (python)

Fixed submission:
def popcount(x):
    return bin(x).count("1")

N, X = open(0).read().split()
N = int(N)
XS = list(X)
X = int(X, 2)

p = popcount(X)
xi = X % (p + 1)
if p == 1:
    xd = 0
else:
    xd = X % (p - 1)

for i in range(N):
    if XS[i] == "1":
        if p == 1:
            print(0)
            continue
        else:
            k = pow(2, N - 1 - i, p - 1)
            x = (xd - k) % (p - 1)
    else:
        k = pow(2, N - 1 - i, p + 1)
        x = (xi + k) % (p + 1)
    ans = 1
    while x != 0:
        x %= popcount(x)
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    return bin(x).count("1")

N,X = open(0).read().split()
N = int(N)
X = int(X, 2)

p = popcount(X)
if p == 0:
    xi = xd = 0
elif p == 1:
    xi = X % 2
    xd = 0
else:
    xi = X % (p + 1)
    xd = X % (p - 1)

for i in range(0, N):
    k = 1 << (N - 1 - i)
    if X & k:
        x = (xd - k) % (p - 1)
    else:
        x = (xi + k) % (p + 1)
    ans = 1
    while x != 0:
        x %= popcount(x)
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    return bin(x).count("1")

N,X = open(0).read().split()
N = int(N)
X = int(X, 2)

if X == 0:
    for i in range(0, N):
        k = 1 << (N - 1 - i)
        x = X ^ k
        ans = 0
        while x != 0:
            x %= popcount(x)
            ans += 1
        print(ans)
else:
    p = popcount(X)
    xi = X % (p + 1)
    xd = X % (p - 1)
    for i in range(0, N):
        k = 1 << (N - 1 - i)
        x = X ^ k
        if x == 0:
            print(0)
            continue

        if x > X:
            pi = p + 1
            x = (xi + (k % pi)) % pi
        else:
            pd = p - 1
            x = (xd - (k % pd)) % pd
        ans = 1
        while x != 0:
            x %= popcount(x)
            ans += 1
        print(ans)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    return bin(x).count("1")

N,X = open(0).read().split()
N = int(N)
X = int(X, 2)

if X == 0:
    for i in range(0, N):
        k = 1 << (N - 1 - i)
        x = X ^ k
        ans = 0
        while x != 0:
            x %= popcount(x)
            ans += 1
        print(ans)
else:
    p = popcount(X)
    xi = X % (p + 1)
    xd = X % (p - 1)
    for i in range(0, N):
        k = 1 << (N - 1 - i)
        x = X ^ k
        if x == 0:
            print(0)
            continue

        if x > X:
            pi = p + 1
            x = (xi + (k % pi)) % pi
        else:
            pd = p - 1
            x = (xd - (k % pd) % pd)
        ans = 1
        while x != 0:
            x %= popcount(x)
            ans += 1
        print(ans)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    return bin(x).count("1")

N,X = open(0).read().split()
N = int(N)
X = int(X, 2)

p = popcount(X)
if p == 0:
    xi = X
    xd = X
else:
    xi = X % (p + 1)
    xd = X % (p - 1)

for i in range(0, N):
    k = 1 << (N - 1 - i)
    x = X ^ k
    if x == 0:
        print(0)
        continue

    if x > X:
        x = xi + (k % (p + 1))
    else:
        x = xd - (k % (p - 1))
    ans = 1
    while x != 0:
        x %= popcount(x)
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    return bin(x).count("1")

N,X = open(0).read().split()
N = int(N)
X = int(X, 2)

p = popcount(X)
xp = X % p

for i in range(0, N):
    k = 1 << (N - 1 - i)
    x = X ^ k
    if x == 0:
        print(0)
        continue

    if x > X:
        x %= xp + (k % (p + 1))
    else:
        x %= xp + (k % (p - 1))
    ans = 1
    while x != 0:
        x %= popcount(x)
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f

N,X = open(0).read().split()
N = int(N)
X = int(X,2)

for i in range(0, N):
    x = X
    x = x ^ (1 << (N - 1 - i))
    ans = 0
    while x != 0:
        x = x % popcount(x)
        ans += 1
    print(ans)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    x = x - ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
    x = x + (x >> 8)
    x = x + (x >> 16)
    x = x + (x >> 32)
    return x & 0x0000007f

N,X = open(0).read().split()
N = int(N)
X = list(X)

for i in range(0, N):
    xs = X[:]
    xs[i] = "1" if xs[i] == "0" else "0"
    x = int("".join(xs), 2)
    ans = 0
    while x != 0:
        x = x % popcount(x)
        ans += 1
    print(ans)