Problem p02609 (python)

Fixed submission:
n = int(input())
x = input()
a = x.count('1')
b = int(x,2)
# print (b)

ary = [None for i in range(200010)]
ary[0] = 0

def f(n):
  if ary[n] is not None:
    return ary[n]
  b = bin(n).count('1')
  r = 1 + f(n%b)
  ary[n] = r
  return ary[n]


a1 = a + 1
a0 = a - 1
for i in range(200010):
  f(i)


one = [1] * 200010
zero = [1] * 200010
for i in range(1, 200010):
    if a0 != 0:
        one[i] = one[i-1] * 2 % a0
    zero[i] = zero[i-1] * 2 % a1


ans = [0 for i in range(n)]
c1 = b%a1
if a0 != 0:
  c0 = b%a0
for i in range(n-1, -1, -1):
  if x[n-1-i] == '0':
    y = c1 + zero[i]
    # y = x %a1
    y %= a1
    print (ary[y]+1)
  else:
    if a0 != 0:
      y = c0 - one[i]
      y %= a0
      print (ary[y]+1)
    else:
      print (0)
# ans.reverse()
# for i in ans:
#   print (i)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = input()
a = x.count('1')
b = int(x,2)
# print (b)

ary = [None for i in range(200010)]
ary[0] = 0

def f(n):
  if ary[n] is not None:
    return ary[n]
  b = bin(n).count('1')
  r = 1 + f(n%b)
  ary[n] = r
  return ary[n]


a1 = a + 1
a0 = a - 1
for i in range(200010):
  f(i)


one = [1] * 200010
zero = [1] * 200010
for i in range(1, 200010):
    if a0 != 0:
        one[i] = one[i-1] * 2 % a0
    zero[i] = zero[i-1] * 2 % a1


ans = [0 for i in range(n)]
c1 = b%a1
c0 = b%a0
for i in range(n-1, -1, -1):
  if x[n-1-i] == '0':
    y = c1 + zero[i]
    # y = x %a1
    y %= a1
    print (ary[y]+1)
  else:
    if a0 != 0:
      y = c0 - one[i]
      y %= a0
      print (ary[y]+1)
    else:
      print (0)
# ans.reverse()
# for i in ans:
#   print (i)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = input()
a = x.count('1')
b = int(x,2)
# print (b)

ary = [None for i in range(200010)]
ary[0] = 0

def f(n):
  if ary[n] is not None:
    return ary[n]
  b = bin(n).count('1')
  r = 1 + f(n%b)
  ary[n] = r
  return ary[n]


for i in range(200010):
  f(i)

a1 = a + 1
a0 = a - 1

ans = [0 for i in range(n)]
for i, j in enumerate(reversed(list(x))):
  if j == '0':
    x = b + pow(2,i,a1)
    # y = x %a1
    x %= a1
    ans[i] = ary[x]+1
  else:
    x = b - pow(2,i,a0)
    # y = x %a0
    x %= a0
    ans[i] = ary[x]+1
ans.reverse()
for i in ans:
  print (i)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = input()
a = x.count('1')
b = int(x,2)
# print (b)

ary = [None for i in range(200010)]
ary[0] = 0

def f(n):
  if ary[n] is not None:
    return ary[n]
  b = bin(n).count('1')
  r = 1 + f(n%b)
  ary[n] = r
  return ary[n]


for i in range(200010):
  f(i)

a1 = a + 1
a0 = a - 1

ans = [0 for i in range(n)]
for i, j in enumerate(reversed(list(x))):
  if j == '0':
    x = b + pow(2,i)
    y = x %a1
    ans[i] = ary[y]+1
  else:
    x = b - pow(2,i)
    y = x %a0
    ans[i] = ary[y]+1
ans.reverse()
for i in ans:
  print (i)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = input()
# print (int(x,2))
a = x.count('1')
# print (a)


for i in range(n):
  if x[i] == '0':
    b = '1'
    cnt = a + 1
  else:
    b = '0'
    cnt = a - 1
  d = x[:i] + b + x[i+1:]
  # print (d, int(d,2))
  v = int(d,2)
  out = 0
  # while v != 0:
  v %= cnt
  cnt = bin(v).count('1')
  if cnt == 0:
    out = 1
  elif cnt < 3:
    out = 2
  else:
    out = 3
  print (out)