Problem p02609 (python)

Fixed submission:
from collections import Counter

N = int(input())
X = input()
cnt = X.count('1')
memo = [0]*(N+1)
memo2 = [[0]*(N+1) for _ in range(2)]
memo2[0][1] = memo2[1][1] = 1

if cnt == 0:
  for _ in range(N):
    print(1)
  exit()

for i in range(1,N+1):
  if i == 1:
    if cnt > 1:
      memo2[0][i] = 1 % (cnt-1)
    memo2[1][i] = 1 % (cnt+1)
  else:
    if cnt > 1:
      memo2[0][i] = memo2[0][i-1]*2 % (cnt-1)
    memo2[1][i] = memo2[1][i-1]*2 % (cnt+1)
  t = 0
  a = i
  while a > 0:
    b = bin(a).count('1')
    a %= b
    t += 1
  memo[i] = t
  
X1 = 0
X_1 = 0
for i in range(1,N+1):
  if X[N-i] == '1':
    X1 += memo2[1][i]
    X_1 += memo2[0][i]
    X1 %= cnt+1
    if cnt > 1:
      X_1 %= cnt-1
    
for i in range(N,0,-1):
  Y = 0
  if X[N-i] == '1':
    if cnt == 1:
      print(0)
      continue
    Y = X_1 - memo2[0][i]
    if Y < 0:
      Y += cnt - 1
    print(memo[Y]+1)
  else:
    Y = X1 + memo2[1][i]
    Y %= cnt + 1
    print(memo[Y]+1)

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
X = input()
cnt = X.count('1')
memo = [0]*(N+1)
memo2 = [[0]*(N+1) for _ in range(2)]
memo2[0][1] = memo2[1][1] = 1

if cnt == 0:
  for _ in range(N):
    print(1)
  exit()

for i in range(1,N+1):
  if i == 1:
    memo2[0][i] = 1 % (cnt-1)
    memo2[1][i] = 1 % (cnt+1)
  else:
    memo2[0][i] = memo2[0][i-1]*2 % (cnt-1)
    memo2[1][i] = memo2[1][i-1]*2 % (cnt+1)
  t = 0
  a = i
  while a > 0:
    b = bin(a).count('1')
    a %= b
    t += 1
  memo[i] = t
  
X1 = 0
X_1 = 0
for i in range(1,N+1):
  if X[N-i] == '1':
    X1 += memo2[1][i]
    X_1 += memo2[0][i]
    X1 %= cnt+1
    X_1 %= cnt-1
    
for i in range(N,0,-1):
  Y = 0
  if X[N-i] == '1':
    Y = X_1 - memo2[0][i]
    if Y < 0:
      Y += cnt - 1
    print(memo[Y]+1)
  else:
    Y = X1 + memo2[1][i]
    Y %= cnt + 1
    print(memo[Y]+1)

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
X = input()
cnt = X.count('1')
memo = [0]*(N+1)
memo2 = [[0]*(N+1) for _ in range(2)]
memo2[0][1] = memo2[1][1] = 1

for i in range(1,N+1):
  if i == 1:
    memo2[0][i] = 1 % (cnt-1)
    memo2[1][i] = 1 % (cnt+1)
  else:
    memo2[0][i] = memo2[0][i-1]*2 % (cnt-1)
    memo2[1][i] = memo2[1][i-1]*2 % (cnt+1)
  t = 0
  a = i
  while a > 0:
    b = bin(a).count('1')
    a %= b
    t += 1
  memo[i] = t
  
X1 = 0
X_1 = 0
for i in range(1,N+1):
  if X[N-i] == '1':
    X1 += memo2[1][i]
    X_1 += memo2[0][i]
    X1 %= cnt+1
    X_1 %= cnt-1
    
for i in range(N,0,-1):
  Y = 0
  if X[N-i] == '1':
    Y = X_1 - memo2[0][i]
    if Y < 0:
      Y += cnt - 1
    print(memo[Y]+1)
  else:
    Y = X1 + memo2[1][i]
    Y %= cnt + 1
    print(memo[Y]+1)

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
Y = input()
X = int(Y,2)
cnt = bin(X).count('1')
for i in range(1,N+1):
  t = 0
  a = X ^ (1<<(N-i))
  if Y[i-1] == '1':
    b = cnt - 1
  else:
    b = cnt + 1
  a %= b
  t += 1
  while a > 0:
    b = bin(a).count('1')
    a %= b
    t += 1
  print(t)

Buggy submission (verdict: Runtime Error):
from collections import Counter

N = int(input())
cnt = Counter(str(X))['1']
cnt = str(bin(cnt))[2:]
X = bin(int(X, 2))