Problem p02609 (python)

Fixed submission:
N=int(input())
X=input()
Y=int(X,2)
Z=Y
C=X.count('1')
if C==1:
  for i in range(N):
    if X[i]=='1':
      print(0)
    elif i==N-1:
      print(2)
    else:
      print(1)
  exit()
Y,Z=Y%(C+1),Z%(C-1)
def f(x):
  if x==0:
    return 0
  return 1+f(x%bin(x).count('1'))
 
for i in range(N):
  if X[i]=='1':
    print(1+f((Z-pow(2,N-i-1,C-1))%(C-1)))
  else:
    print(1+f((Y+pow(2,N-i-1,C+1))%(C+1)))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


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 = I()
x = S()
cnt = sum([int(i) for i in x])
ret1 = 1
ret2 = 1
rem1 = 0
rem2 = 0
for i in range(n - 1, -1, -1):
    if x[i] == "1":
        rem1 = (rem1 + ret1) % (cnt + 1)
        rem2 = (rem2 + ret2) % (cnt - 1)
    ret1 = ret1 * 2 % (cnt + 1)
    ret2 = ret2 * 2 % (cnt - 1)

for j in range(n):
    ans = 1
    if x[j] == "1":
        if cnt == 1:
            print(0)
            continue
        r = (rem2 - pow(2, n - 1 - j, cnt - 1)) % (cnt - 1)
        while r:
            r = r % popcount(r)
            ans += 1
        print(ans)
    else:
        r = (rem1 + pow(2, n - 1 - j, cnt + 1)) % (cnt + 1)
        while r:
            r = r % popcount(r)
            ans += 1
        print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


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 = I()
x = S()
cnt = sum([int(i) for i in x])
ret1 = 1
ret2 = 1
rem1 = 0
rem2 = 0
for i in range(n - 1, -1, -1):
    if x[i] == "1":
        rem1 = (rem1 + ret1) % (cnt + 1)
        rem2 = (rem2 + ret2) % (cnt - 1)
    ret1 = ret1 * 2 % (cnt + 1)
    ret2 = ret2 * 2 % (cnt - 1)


for j in range(n):
    ans = 1
    if x[j] == "1":
        r = (rem2 - pow(2, n - 1 - j, cnt - 1)) % (cnt - 1)
        while r:
            r = r % popcount(r)
            ans += 1
        print(ans)
    else:
        r = (rem1 + pow(2, n - 1 - j, cnt + 1)) % (cnt + 1)
        while r:
            r = r % popcount(r)
            ans += 1
        print(ans)