Problem p02609 (python)

Fixed submission:
def pc(x):
    return format(x, 'b').count('1')


N = int(input())
X = input()
Xo = int(X, 2)

count = 0
ans = 0
ori = pc(Xo)
X = list(X)

Xp = Xo%(ori + 1)
if ori == 1:
    Xm = 0
else:
    Xm = Xo % (ori - 1)

# for i in range(N-1, -1, -1):
for i in range(N):
    if ori == 1 and X[i] == '1':
        print(0)
        continue
    else:
        if X[i] == '1':
            # ans = Xm - pow(2, i, ori - 1)
            ans = Xm - pow(2, N-1-i, ori - 1)
            ans %= ori - 1
        else:
            # ans = Xp - pow(2, i, ori + 1)
            ans = Xp + pow(2, N-1-i, ori + 1)
            ans %= ori + 1
    count = 1
    while ans > 0:
        # count += 1
        # if ans == 1:
        #     break
        ans %= pc(ans)
        count += 1

   
    print(count)
    count = 1

Buggy submission (verdict: Runtime Error):
def toggle_nth_bit(num: int, n: int):
    return num ^ (1 << n)


N = int(input())
X = input()
f = 0
count = 1
ans = 0
fi = 0
ori = X.count("1")
result = 0
xc0 = int(X,2) % (ori + 1)
xc1 = int(X,2) % (ori - 1) if ori > 1 else 0
for i in range(N-1, -1, -1):
    # print(i)
    fi = toggle_nth_bit(int(X, 2), i)
    # f = bin(toggle_nth_bit(int(X, 2), i))
    if X[i] == "1":
        result = ori - 1
        ans = (xc1 - pow(2, i,result)) % result
    else:
        result = ori + 1
        ans = (xc0 - pow(2, i,result)) % result

    # f = toggle_nth_bit(int(X, 2), i)
    if result == 0:
        # ans = fi % result
    # else:
        print(0)
        continue
    while ans > 0:
        # f = bin(fi)
        count += 1
        if ans == 1:
            break
        # ans = ans % popcount(ans)
        ans = ans % bin(ans).count("1")

        # print(f)
        # print(fi)
        # count += 1
        # print(count)
    # c.append(count)
    print(count)
    count = 1

# ans = str(bin(toggle_nth_bit(int(X,2), 0)))
# for i in c:
#     print(i)

Buggy submission (verdict: Runtime Error):
def toggle_nth_bit(num: int, n: int):
    return num ^ (1 << n)


N = int(input())
X = input()
f = 0
count = 1
ans = 0
fi = 0
for i in range(N-1, -1, -1):
    fi = toggle_nth_bit(int(X, 2), i)
    f = bin(toggle_nth_bit(int(X, 2), i))
    ans = fi % f.count("1")
    while ans != 0:
        count += 1
        if ans == 1:
            break
        ans = ans % bin(ans).count("1")

    print(count)
    count = 1

Buggy submission (verdict: Runtime Error):
def toggle_nth_bit(num: int, n: int):
    return num ^ (1 << n)

def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f

N = int(input())
X = input()
f = 0
count = 1
for i in range(N-1, -1, -1):
    f = toggle_nth_bit(int(X, 2), i)
    while f % popcount(f) != 0:
        f = f % popcount(f)
        count += 1
    print(count)
    count = 1

Buggy submission (verdict: Runtime Error):

def toggle_nth_bit(num: int, n: int):
    return num ^ (1 << n)

N = int(input())
X = input()
f = 0
count = 1
c = []
fi = 0
for i in range(N-1, -1, -1):
    # print(i)
    fi = toggle_nth_bit(int(X, 2), i)
    f = str(bin(toggle_nth_bit(int(X, 2), i)))
    while fi % f.count("1") != 0:
        fi = fi % f.count("1")
        f = str(bin(fi))
        # print(f)
        # print(fi)
        count += 1
        # print(count)
    c.append(count)
    count = 1

# ans = str(bin(toggle_nth_bit(int(X,2), 0)))
for i in c:
    print(i)