Problem p02609 (python)

Fixed submission:
def resolve():
  N = int(input())
  X = input()

  if N == 1:
    if X.count("1"):
      print(0)
    else:
      print(1)
    return True

  base_1_count = X.count("1")
  if base_1_count == 0:
    for _ in range(N):
      print(1)
    return True

  X_int = int(X, 2)
  X_int_p = X_int%(base_1_count + 1)

  # RE 対策
  if base_1_count == 1:
    for i in range(N):
      if X[i] == "1":
        print(0)
      else:
        if i == N - 1:
          Xi = X_int_p + 1
        else:
          Xi = X_int_p
        count = 1
        while Xi != 0:
          Xi %= bin(Xi).count("1")
          count += 1
        print(count)
    return True

  X_int_m = X_int%(base_1_count - 1)
  for i in range(N):
    # 初期値計算を高速にやる
    if X[i] == "1":
      temp_1_count = base_1_count-1
      pow_2 = pow(2, N-i-1, base_1_count-1)
      if X_int_m <= pow_2:
        Xi = X_int_m - pow_2 + base_1_count-1
      else:
        Xi = X_int_m - pow_2
    else:
      temp_1_count = base_1_count+1
      Xi = X_int_p + pow(2, N-i-1, base_1_count+1)
      while Xi >= base_1_count+1:
        Xi %= base_1_count+1

    # print("1_c={0}".format(temp_1_count))
    if temp_1_count == Xi:
      print(1)
      continue

    count = 1
    while Xi != 0:
      num_of_1 = bin(Xi).count("1")
      Xi %= num_of_1
      count += 1
    print(count)

if __name__ == "__main__":
    resolve()

Buggy submission (verdict: Runtime Error):
N = int(input())
X = input()

if X == "1":
  print(1)
  return True

if X == "0":
  print(0)
  return True

base_1_count = X.count("1")
if base_1_count == 0:
  for _ in range(N):
    print(1)
  return True

X_int = int(X, 2)
X_int_p = X_int%(base_1_count + 1)

# RE 対策
if base_1_count == 1:
  for i in range(N):
    if X[i] == "1":
      print(0)
    else:
      Xi = X_int_p
      count = 1
      while Xi != 0:
        Xi %= bin(Xi).count("1")
        count += 1
      print(count)
  return True

X_int_m = X_int%(base_1_count - 1)
for i in range(N):
  # 初期値計算を高速にやる
  if X[i] == "1":
    temp_1_count = base_1_count-1
    pow_2 = 2**(N-i-1)%(base_1_count-1)
    if X_int_m < pow_2:
      Xi = (X_int-2**(N-i-1))%(base_1_count-1)
    else:
      Xi = X_int_m - pow_2
  else:
    temp_1_count = base_1_count+1
    Xi = X_int_p + 2**(N-i-1)%(base_1_count+1)

  # print("1_c={0}".format(temp_1_count))
  if temp_1_count == Xi:
    print(1)
    continue

  count = 1
  while Xi != 0:
    num_of_1 = bin(Xi).count("1")
    # print("Xi={0}".format(Xi))
    Xi %= num_of_1
    count += 1
  print(count)

Buggy submission (verdict: Runtime Error):
def resolve():
  N = int(input())
  X = input()
  base_1_count = X.count("1")
  X_int = int(X, 2)
  X_int_p = X_int%(base_1_count + 1)
  X_int_m = X_int%(base_1_count - 1)

# 高速化バージョン
  for i in range(N):
    if base_1_count == 1 and X[i] == "1":
      print(0)
      continue

    if X[i] == "1":
      Xi = X_int_m - 2**(N-i-1)%(base_1_count-1)
    else:
      Xi = X_int_p + 2**(N-i-1)%(base_1_count+1)
    count = 1
    while Xi != 0:
      # print(Xi)
      num_of_1 = bin(Xi).count("1")
      if num_of_1 != 0:
        Xi %= num_of_1
        count += 1
      else:
        break
    # print("~~~~~~~~~~~~~~~")
    print(count)

# 低速バージョン
  # for i in range(N):
  #   if X[i] == "1":
  #     Xi = X_int - 2**(N-i-1)
  #   else:
  #     Xi = X_int + 2**(N-i-1)
  #   count = 0
  #   while Xi != 0:
  #     # print(Xi)
  #     num_of_1 = bin(Xi).count("1")
  #     Xi %= num_of_1
  #     count += 1
  #   # print("~~~~~~~~~~~~~~~")
  #   print(count)

if __name__ == "__main__":
    resolve()

Buggy submission (verdict: Runtime Error):
def resolve():
  N = int(input())
  X = input()
  base_1_count = X.count("1")
  X_int = int(X, 2)
  X_int_p = X_int%(base_1_count + 1)
  X_int_m = X_int%(base_1_count - 1)

# 高速化バージョン
  for i in range(N):
    if X[i] == "1":
      Xi = X_int_m - 2**(N-i-1)%(base_1_count-1)
    else:
      Xi = X_int_p + 2**(N-i-1)%(base_1_count+1)
    count = 1
    while Xi != 0:
      # print(Xi)
      num_of_1 = bin(Xi).count("1")
      if num_of_1 != 0:
        Xi %= num_of_1
        count += 1
      else:
        break
    # print("~~~~~~~~~~~~~~~")
    print(count)

# 低速バージョン
  # for i in range(N):
  #   if X[i] == "1":
  #     Xi = X_int - 2**(N-i-1)
  #   else:
  #     Xi = X_int + 2**(N-i-1)
  #   count = 0
  #   while Xi != 0:
  #     # print(Xi)
  #     num_of_1 = bin(Xi).count("1")
  #     Xi %= num_of_1
  #     count += 1
  #   # print("~~~~~~~~~~~~~~~")
  #   print(count)

if __name__ == "__main__":
    resolve()

Buggy submission (verdict: Runtime Error):
def resolve():
  N = int(input())
  X = input()
  base_1_count = X.count("1")
  X_int = int(X, 2)
  X_int_p = X_int%(base_1_count + 1)
  X_int_m = X_int%(base_1_count - 1)

# 高速化バージョン
  print()
  for i in range(N):
    if X[i] == "1":
      Xi = X_int_m - 2**(N-i-1)%(base_1_count-1)
    else:
      Xi = X_int_p + 2**(N-i-1)%(base_1_count+1)
    count = 1
    while Xi != 0:
      # print(Xi)
      num_of_1 = bin(Xi).count("1")
      Xi %= num_of_1
      count += 1
    # print("~~~~~~~~~~~~~~~")
    print(count)

# 低速バージョン
  # for i in range(N):
  #   if X[i] == "1":
  #     Xi = X_int - 2**(N-i-1)
  #   else:
  #     Xi = X_int + 2**(N-i-1)
  #   count = 0
  #   while Xi != 0:
  #     # print(Xi)
  #     num_of_1 = bin(Xi).count("1")
  #     Xi %= num_of_1
  #     count += 1
  #   # print("~~~~~~~~~~~~~~~")
  #   print(count)

if __name__ == "__main__":
    resolve()