Problem p02609 (python)

Fixed submission:
import math
# from collections import defaultdict
import time
def steps(n,p):
  c = 0
  while n:
    c += 1
    n = n%p
    p = bin(n).count('1')
  return c

# n = 200000
# x = "1"*n
n = int(input())
x = input()
p = x.count('1')
ans = []
num = int(x,2)
if p==1:
  for i in range(n-1,-1,-1):
    if x[i]=='1':
      ans.append(0)
    else:
      if i==n-1 or x[n-1]=='1':
        ans.append(2)
      else:
        ans.append(1)
else:
  incs = 1
  decs = 1
  inc = num%(p+1)
  dec = num%(p-1)
  start = time.time()
  k = -1
  for i in range(n-1,-1,-1):
    cp = p
    if x[i] == '0':
      cp += 1
      new = (inc+incs)%cp
    else:
      cp -= 1
      new = (dec-decs)%cp
    k -= 1
    incs = (incs*2)%(p+1)
    decs = (decs*2)%(p-1)
    sol = steps(new,cp)
    if sol==0:
      sol=1
    ans.append(sol)
    # steps(new,cp)
  # print(time.time()-start)
ans = ans[::-1]
for x in ans:
  print(x)

Buggy submission (verdict: Runtime Error):
import math
# from collections import defaultdict
import time
def steps(n,p):
  c = 0
  while n:
    c += 1
    n = n%p
    p = bin(n).count('1')
  return c

# n = 200000
# x = "1"*n
n = int(input())
x = input()
incs = 1
decs = 1
p = x.count('1')
ans = []
num = int(x,2)
inc = num%(p+1)
dec = num
if p > 1:
  dec = num%(p-1)
start = time.time()
k = -1
for i in range(n-1,-1,-1):
  cp = p
  if x[i] == '0':
    cp += 1
    new = (inc+incs)%cp
  else:
    cp -= 1
    if cp > 1:
      new = (dec-decs)%cp
    else:
      new = dec-decs
  k -= 1
  incs = (incs*2)%(p+1)
  if p > 1:
    decs = (decs*2)%(p-1)
  sol = steps(new,cp)
  if sol==0:
    sol=1
  ans.append(sol)
  # steps(new,cp)
# print(time.time()-start)
ans = ans[::-1]
for x in ans:
  print(x)

Buggy submission (verdict: Runtime Error):
import math
# from collections import defaultdict
import time
def steps(n,p):
  c = 0
  while n:
    c += 1
    n = n%p
    p = bin(n).count('1')
  return c

# n = 200000
# x = "1"*n
n = int(input())
x = input()
incs = 1
decs = 1
p = x.count('1')
ans = []
num = int(x,2)
inc = num%(p+1)
dec = num%(p-1)
start = time.time()
k = -1
for i in range(n-1,-1,-1):
  cp = p
  if x[i] == '0':
    cp += 1
    new = (inc+incs)%cp
  else:
    cp -= 1
    new = (dec-decs)%cp
  k -= 1
  incs = (incs*2)%(p+1)
  decs = (decs*2)%(p-1)
  sol = steps(new,cp)
  if sol==0:
    sol=1
  ans.append(sol)
  # steps(new,cp)
# print(time.time()-start)
ans = ans[::-1]
for x in ans:
  print(x)

Buggy submission (verdict: Runtime Error):
import math
# from collections import defaultdict
import time
def steps(n,p):
  c = 0
  while n:
    c += 1
    n = n%p
    p = bin(n).count('1')
  return c

# n = 200000
# x = "1"*n
n = int(input())
x = input()
incs = [1]
decs = [1]
p = x.count('1')
for i in range(1,n):
  incs.append((incs[-1]*2)%(p+1))
  decs.append((decs[-1]*2)%(p-1))
num = int(x,2)
inc = num%(p+1)
dec = num%(p-1)
start = time.time()
k = -1
for i in range(n):
  cp = p
  if x[i] == '0':
    cp += 1
    new = (inc+incs[k])%cp
  else:
    cp -= 1
    new = (dec-decs[k])%cp
  k -= 1
  ans = steps(new,cp)
  if ans==0:
    ans=1
  print(ans)
  # steps(new,cp)
# print(time.time()-start)

Buggy submission (verdict: Runtime Error):
import math
# from collections import defaultdict
# import time
def steps(n,p):
  c = 0
  while n:
    c += 1
    n = n%p
    p = bin(n).count('1')
  return c

n = int(input())
x = input()
incs = [1]
decs = [1]
p = x.count('1')
for i in range(1,n):
  incs.append((incs[-1]*2)%(p+1))
  decs.append((decs[-1]*2)%(p-1))
num = int(x,2)
inc = num%(p+1)
dec = num%(p-1)
# start = time.time()
for i in range(n):
  cp = p
  if x[i] == '0':
    cp += 1
    new = (inc+incs[n-i-1])%cp
  else:
    cp -= 1
    new = (dec-decs[n-i-1])%cp
  ans = steps(new,cp)
  if ans==0:
    ans=1
  print(ans)
  # steps(new,cp)
# print(time.time()-start)