Problem p02609 (python)

Fixed submission:
N = int(input())
strX = input()

#10進数に変換
decX = int(strX,2)

#1の数をカウント
pop_c = strX.count('1')

# X % popcount+1
f_p = decX % (pop_c + 1)

# X % popcount-1(0になる時は0)
if pop_c == 1:
  f_m = 0
else:
  f_m = decX % (pop_c - 1)

for i in range(0,N):
  if strX[i] == '0':
    pop_a = (f_p + pow(2,(N-1-i),(pop_c + 1))) % (pop_c + 1) 
    Ans = 1
    
  elif pop_c == 1:
    pop_a = 0
    Ans = 0
    
  else:
    pop_a = f_m - (pow(2,(N-1-i),(pop_c - 1)))
    if pop_a < 0:
      pop_a = pop_a + pop_c - 1
    Ans = 1
  
  while pop_a > 0:
    wkpop = bin(pop_a).count('1')
    pop_a = pop_a % wkpop
    Ans += 1
  
  print(Ans)

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

#10進数に変換
decX = int(strX,2)

#1の数をカウント
pop_c = strX.count('1')

# X % popcount+1
f_p = decX % (pop_c + 1)

# X % popcount-1(0になる時は0)
if pop_c == 1:
  f_m = 0
else:
  f_m = decX % (pop_c - 1)

#(2**i)÷popcountを保存するリストを作成
f_tp = [0] * N  
f_tm = [0] * N  

for i in range(0,N):
  f_tp[i] = (2**(N-1-i)) % (pop_c + 1) 
  f_tm[i] = (2**(N-1-i)) % (pop_c - 1) 

  
for i in range(0,N):
  if strX[i] == '0':
    pop_a = (f_p + f_tp[i]) % (pop_c + 1) 
    Ans = 1
    
  elif strX[i] == '1' and pop_c == 1:
    pop_a = 0
    Ans = 0
    
  else:
    pop_a = f_m - f_tm[i]
    if pop_a < 0:
      pop_a = pop_a + pop_c - 1
    Ans = 1
  
  while pop_a > 0:
    wkpop = bin(pop_a).count('1')
    pop_a = pop_a % wkpop
    Ans += 1
  
  print(Ans)

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

#10進数に変換
decX = int(strX,2)

#1の数をカウント
pop_c = strX.count('1')

# X % popcount+1
f_p = decX % (pop_c + 1)

# X % popcount-1(0になる時は0)
if pop_c == 1:
  f_m = 0
else:
  f_m = decX % (pop_c - 1)

#(2**i)÷popcountを保存するリストを作成
f_tp = [0] * N  
f_tm = [0] * N  

for i in range(0,N):
  f_tp[i] = (2**(N-1-i)) % (pop_c + 1) 
  f_tm[i] = (2**(N-1-i)) % (pop_c - 1) 

  
for i in range(0,N):
  if strX[i] == '0':
    pop_a = (f_p + f_tp[i]) % (pop_c + 1) 
    Ans = 1
    
  elif strX[i] == '1' and pop_c == 1:
    pop_a = 0
    Ans = 0
    
  else:
    pop_a = f_m - f_tm[i]
    if pop_a < 0:
      pop_a = pop_a + pop_c + 1
    Ans = 1
  
  while pop_a > 0:
    wkpop = bin(pop_a).count('1')
    pop_a = pop_a % wkpop
    Ans += 1
  
  print(Ans)

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

decX = int(strX,2)
cnt = strX.count('1')

pop_p = decX % (cnt + 1)
pop_m = decX % (cnt - 1)

for i in range(0,N):
  if strX[i] == '0':
    pop = (2**(N-1-i) + pop_p) % (cnt + 1)
    Ans = 1
    
  elif strX[i] == '1' and cnt == 1:
    pop = 0
    Ans = 0
    
  else:
    pop = (2**(N-1-i) - pop_m) % (cnt - 1)
    Ans = 1
  
  while pop > 0:
    wkcnt = bin(pop).count('1')
    pop = pop % wkcnt
    Ans += 1
  
  print(Ans)

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

decX = int(strX,2)
cnt = strX.count('1')

pop_p = decX % (cnt + 1)
pop_m = decX % (cnt - 1)

for i in range(0,N):
  if strX[i] == '0':
    pop = (pop_p + (2**(N-1-i) % (cnt + 1))) % (cnt + 1)
    Ans = 1
    
  elif strX[i] == '1' and cnt == 1:
    pop = 0
    Ans = 0
    
  else:
    pop = (pop_m - (2**(N-1-i) % (cnt - 1))) % (cnt - 1)
    Ans = 1
  
  while pop > 0:
    wkcnt = bin(pop).count('1')
    pop = pop % wkcnt
    Ans += 1
  
  print(Ans)

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

decX = int(strX,2)
cnt = strX.count('1')

for i in range(0,N):
#  print('i=' + str(i))
  if strX[i] == '0':
    wkstr = strX[:i] + '1' + strX[i+1:N+1]
  else:
    wkstr = strX[:i] + '0' + strX[i+1:N+1]
  
  wkcnt = wkstr.count('1')
#  print(wkstr)
#  print(wkcnt)
  wkdec = int(wkstr,2)
  pop = wkdec % wkcnt
  Ans = 1
  
  while pop > 0:
    wkcnt = bin(pop).count('1')
    pop = pop % wkcnt
    Ans += 1
  
  print(Ans)