Buggy python submission (verdict: Time Limit Exceeded):
memo = {0:0, 1:1, 2:1}
def calc(x):
  global memo
  if x in memo:
    return memo[x]
  c = bin(x).count('1')
  y = x % c
  res = calc(y) + 1
  memo[x] = res
  return res

for i in range(3, 2*10**5+1):
  calc(i)

N = int(input())
X = input()
c = X.count('1')
X_int = int(X, 2)
Xmodp = X_int % (c+1)
Xmodm = X_int % (c-1) if c > 1 else 0

ans = []
digit = 1 << (N - 1)
for x in X:
  if x == '0':
    ans.append(
      memo[(Xmodp + digit%(c+1)) % (c+1)] + 1
    )
  else:
    if c == 1:
      ans.append(0)
    else:
      ans.append(
        memo[(Xmodm - digit%(c-1)) % (c-1)] + 1
      )  
  digit >>= 1

print(*ans, sep='\n')

Fixed submission:
N = int(input())
X = input()
c = X.count('1')
if c == 0:
  for i in range(N):
    print(1)
  quit()
if c == 1:
  if int(X[-1]):
    for i in range(N-1):
      print(2)
    print(0)
  else:
    for i in range(N-1):
      if int(X[i]):
        print(0)
      else:
        print(1)
    print(2)
  quit()

memo = {0:0, 1:1, 2:1}
def calc(x):
  global memo
  if x in memo:
    return memo[x]
  c = bin(x).count('1')
  y = x % c
  res = calc(y) + 1
  memo[x] = res

for i in range(3, 2*10**5+1):
  calc(i)

Xmodp = Xmodm = 0
digit_p = digit_m = 1
digit = {}
for i in range(N):
  if int(X[-i-1]):
    Xmodp += digit_p
    Xmodp %= c + 1
    Xmodm += digit_m
    Xmodm %= c - 1
  digit[i] = [digit_p, digit_m]
  digit_p = (digit_p * 2) % (c+1)
  digit_m = (digit_m * 2) % (c-1)

ans = []
for i in range(N):
  x = int(X[-i-1])
  if x:
    ans.append(memo[(Xmodm - digit[i][1]) % (c-1)] + 1)
  else:
    ans.append(memo[(Xmodp + digit[i][0]) % (c+1)] + 1)

ans = ans[::-1]
print(*ans, sep='\n')

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)

memo = {0:0, 1:1, 2:1}
def calc(x):
  global memo
  if x in memo:
    return memo[x]
  c = bin(x).count('1')
  y = x % c
  res = calc(y) + 1
  memo[x] = res
  return res

for i in range(3, 2*10**5+1):
  calc(i)

N = int(input())
X = input()
c = X.count('1')
X_int = int(X, 2)
Xmodp = X_int % (c+1)
Xmodm = X_int % (c-1) if c > 1 else 0

ans = []
digit = 1 << (N - 1)
for x in X:
  if x == '0':
    ans.append(
      calc((Xmodp + digit%(c+1)) % (c+1)) + 1
    )
  else:
    if c == 1:
      ans.append(0)
    else:
      ans.append(
        calc((Xmodm - digit%(c-1)) % (c-1)) + 1
      )  
  digit >>= 1

print(*ans, sep='\n')

Fixed submission:
N = int(input())
X = input()
c = X.count('1')
if c == 0:
  for i in range(N):
    print(1)
  quit()
if c == 1:
  if int(X[-1]):
    for i in range(N-1):
      print(2)
    print(0)
  else:
    for i in range(N-1):
      if int(X[i]):
        print(0)
      else:
        print(1)
    print(2)
  quit()

memo = {0:0, 1:1, 2:1}
def calc(x):
  global memo
  if x in memo:
    return memo[x]
  c = bin(x).count('1')
  y = x % c
  res = calc(y) + 1
  memo[x] = res

for i in range(3, 2*10**5+1):
  calc(i)

Xmodp = Xmodm = 0
digit_p = digit_m = 1
digit = {}
for i in range(N):
  if int(X[-i-1]):
    Xmodp += digit_p
    Xmodp %= c + 1
    Xmodm += digit_m
    Xmodm %= c - 1
  digit[i] = [digit_p, digit_m]
  digit_p = (digit_p * 2) % (c+1)
  digit_m = (digit_m * 2) % (c-1)

ans = []
for i in range(N):
  x = int(X[-i-1])
  if x:
    ans.append(memo[(Xmodm - digit[i][1]) % (c-1)] + 1)
  else:
    ans.append(memo[(Xmodp + digit[i][0]) % (c+1)] + 1)

ans = ans[::-1]
print(*ans, sep='\n')

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**9)

memo = {0:0, 1:1, 2:1}
def calc(x):
  global memo
  if x in memo:
    return memo[x]
  c = bin(x).count('1')
  y = x % c
  res = calc(y) + 1
  memo[x] = res
  return res

for i in range(3, 2*10**5 + 1):
  calc(i)

N = int(input())
X = input()
X_int = int(X, 2)
c = X.count('1')
X_list = list(X)
ans = []
digit = 1 << (N - 1)
for i, x in enumerate(X_list):
  if x == '0':
    ans.append(calc((X_int+digit)%(c+1))+1)
  else:
    ans.append(calc((X_int-digit)%(c-1))+1)
  
  digit >>= 1

print(*ans, sep='\n')

Fixed submission:
N = int(input())
X = input()
c = X.count('1')
if c == 0:
  for i in range(N):
    print(1)
  quit()
if c == 1:
  if int(X[-1]):
    for i in range(N-1):
      print(2)
    print(0)
  else:
    for i in range(N-1):
      if int(X[i]):
        print(0)
      else:
        print(1)
    print(2)
  quit()

memo = {0:0, 1:1, 2:1}
def calc(x):
  global memo
  if x in memo:
    return memo[x]
  c = bin(x).count('1')
  y = x % c
  res = calc(y) + 1
  memo[x] = res

for i in range(3, 2*10**5+1):
  calc(i)

Xmodp = Xmodm = 0
digit_p = digit_m = 1
digit = {}
for i in range(N):
  if int(X[-i-1]):
    Xmodp += digit_p
    Xmodp %= c + 1
    Xmodm += digit_m
    Xmodm %= c - 1
  digit[i] = [digit_p, digit_m]
  digit_p = (digit_p * 2) % (c+1)
  digit_m = (digit_m * 2) % (c-1)

ans = []
for i in range(N):
  x = int(X[-i-1])
  if x:
    ans.append(memo[(Xmodm - digit[i][1]) % (c-1)] + 1)
  else:
    ans.append(memo[(Xmodp + digit[i][0]) % (c+1)] + 1)

ans = ans[::-1]
print(*ans, sep='\n')