Problem p02609 (python)

Fixed submission:
# -*- coding: utf-8 -*-
N = int(input())
X = input()
X_10 = int(X, 2)
popcount_X = X.count("1")
popcount_X_plus = popcount_X + 1
popcount_X_minus = popcount_X - 1
f_plus = X_10 % popcount_X_plus
f_minus = X_10 % popcount_X_minus if popcount_X_minus != 0 else 0

def popcount(n):
  return bin(n).count("1")

def f(n):
  if n == 0:
    return 0
  return n % popcount(n)

for i in range(N):
  j = N - (i + 1)
  n = 0
  ans = 1
  
  if X[i] == '0':
#    n = (f_plus + (2**j % popcount_X_plus)) % popcount_X_plus
    n = (f_plus + pow(2, j, popcount_X_plus)) % popcount_X_plus
  else:
    if popcount_X_minus == 0:
      print(0)
      continue
    else:
#      n = (f_minus - (2**j % popcount_X_minus) + popcount_X_minus) % popcount_X_minus
      n = (f_minus - pow(2, j, popcount_X_minus) + popcount_X_minus) % popcount_X_minus
  
  while n > 0:
    n = f(n)
    ans += 1
  print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N = int(input())
X = input()
X_10 = 0
popcount_X = 0

def popcount(n):
  return bin(n).count("1")

def f_func(n):
  popcnt = popcount(n)
  if popcnt == 0:
    return 0
  return n % popcnt

for i in range(N):
  j = N - (i + 1)
  X_10 += int(X[i]) * 2 ** j
  if X[i] == '1':
    popcount_X += 1

popcount_X_plus = popcount_X + 1
popcount_X_minus = popcount_X - 1
if popcount_X_plus == 0:
  fX_plus = 0
else:
  fX_plus = X_10 % popcount_X_plus

fX_minus = X_10 % popcount_X_minus


for i in range(N):
  j = N - (i + 1)
  
  Xi = 0
  f = 0
  ans = 1
  
  if X[i] == '0':
    Xi = X_10 + 2**j
    f = (fX_plus + 2**j % popcount_X_plus) % popcount_X_plus
    
  elif X[i] == '1':
    Xi = X_10 - 2**j
    if popcount_X_minus == 0:
      f = 0
    else:
      f = (fX_minus - 2**j % popcount_X_minus) % popcount_X_minus
      
  
  while f > 0:
    f = f_func(f)
    ans += 1
  print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N = int(input())
X = input()
X_10 = 0
popcount_X = 0

def popcount(n):
  return bin(n).count("1")

def f_func(n):
  popcnt = popcount(n)
  if popcnt == 0:
    return 0
  return n % popcnt

for i in range(N):
  j = N - (i + 1)
  X_10 += int(X[i]) * 2 ** j
  if X[i] == '1':
    popcount_X += 1

popcount_X_plus = popcount_X + 1
popcount_X_minus = popcount_X - 1
fX_plus = X_10 % popcount_X_plus
fX_minus = X_10 % popcount_X_minus

for i in range(N):
  j = N - (i + 1)
  
  Xi = 0
  f = 0
  ans = 1
  
  if X[i] == '0':
    Xi = X_10 + 2**j
    if popcount_X_plus == 0:
      f = 0
    else:
      f = (fX_plus + 2**j % popcount_X_plus) % popcount_X_plus
    
  elif X[i] == '1':
    Xi = X_10 - 2**j
    if popcount_X_minus == 0:
      f = 0
    else:
      f = (fX_minus - 2**j % popcount_X_minus) % popcount_X_minus
      
  
  while f > 0:
    f = f_func(f)
    ans += 1
  print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N = int(input())
X = input()
X_10 = 0
popcount_X = 0

def popcount(n):
  return bin(n).count("1")

def f_func(n):
  if n == 0:
    return 0
  return n % popcount(n)

for i in range(N):
  j = N - (i + 1)
  X_10 += int(X[i]) * 2 ** j
  if X[i] == '1':
    popcount_X += 1

popcount_X_plus = popcount_X + 1
popcount_X_minus = popcount_X - 1
fX_plus = X_10 % popcount_X_plus
fX_minus = X_10 % popcount_X_minus

for i in range(N):
  j = N - (i + 1)
  
  Xi = 0
  f = 0
  ans = 1
  
  if X[i] == '0':
    Xi = X_10 + 2**j
    if popcount_X_plus == 0:
      f = 0
    else:
      f = (fX_plus + 2**j % popcount_X_plus) % popcount_X_plus
    
  elif X[i] == '1':
    Xi = X_10 - 2**j
    if popcount_X_minus == 0:
      f = 0
    else:
      f = (fX_minus - 2**j % popcount_X_minus) % popcount_X_minus
      
  
  while f > 0:
    f = f_func(f)
    ans += 1
  print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N = int(input())
X = input()
X_10 = 0
popcount_X = 0

def popcount(n):
  return bin(n).count("1")

def f_func(n):
  if n == 0:
    return 0
  return n % popcount(n)

for i in range(N):
  j = N - (i + 1)
  X_10 += int(X[i]) * 2 ** j
  if X[i] == '1':
    popcount_X += 1

popcount_X_plus = popcount_X + 1
popcount_X_minus = popcount_X - 1
fX_plus = X_10 % popcount_X_plus
fX_minus = X_10 % popcount_X_minus

for i in range(N):
  j = N - (i + 1)
  
  Xi = 0
  f = 0
  popcnt = 0
  ans = 1
  
  if X[i] == '0':
    Xi = X_10 + 2**j
    popcnt = popcount_X + 1
    f = (fX_plus + 2**j % popcount_X_plus) % popcount_X_plus
    
  elif X[i] == '1':
    Xi = X_10 - 2**j
    popcnt = popcount_X - 1
    f = (fX_minus - 2**j % popcount_X_minus) % popcount_X_minus
  
  while f > 0:
    f = f_func(f)
    ans += 1
  print(ans)

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
N = int(input())
X = input()
X_10 = 0
popcount_X = 0

def popcount(n):
  return bin(n).count("1")

def f_func(n):
  return n % popcount(n)

for i in range(N):
  j = N - (i + 1)
  X_10 += int(X[i]) * 2 ** j
  if X[i] == '1':
    popcount_X += 1

popcount_X_plus = popcount_X + 1
popcount_X_minus = popcount_X - 1
fX_plus = X_10 % popcount_X_plus
fX_minus = X_10 % popcount_X_minus

for i in range(N):
  j = N - (i + 1)
  
  Xi = 0
  f = 0
  popcnt = 0
  ans = 1
  
  if X[i] == '0':
    Xi = X_10 + 2**j
    popcnt = popcount_X + 1
    f = (fX_plus + 2**j % popcount_X_plus) % popcount_X_plus
    
  elif X[i] == '1':
    Xi = X_10 - 2**j
    popcnt = popcount_X - 1
    f = (fX_minus - 2**j % popcount_X_minus) % popcount_X_minus
  
  while f > 0:
    f = f_func(f)
    ans += 1
  print(ans)