Problem p02609 (python)

Fixed submission:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
X = input()

def popcount(n):
	if n == 0:
		return 0
	else:
		cnt = 0
		while 0 < n:
			cnt += 1
			p = "{:b}".format(n).count("1")
			n %= p
		return cnt

n = X.count("1")

n1 = n-1
n2 = n+1

q_1 = [0]*N
q_2 = [0]*N
for i in range(N):
	if n1 != 0:
		q_1[i] = pow(2, i, n1)
	if n2 != 0:
		q_2[i] = pow(2, i, n2)

mod_sum_1 = 0
mod_sum_2 = 0

for i in range(N):
	if X[-i-1] == "1":
		mod_sum_1 += q_1[i]
		mod_sum_2 += q_2[i]

q_1 = q_1[::-1]
q_2 = q_2[::-1]


if n1 == 0:
	for i in range(N):
		if X[i] == "1":
			print(0)
		else:
			m = n2
			mod_sum = mod_sum_2 + q_2[i]
			print(popcount(mod_sum%m)+1)
else:
	for i in range(N):
		if X[i] == "1":
			m = n1
			mod_sum = mod_sum_1 - q_1[i]
		else:
			m = n2
			mod_sum = mod_sum_2 + q_2[i]

		print(popcount(mod_sum%m)+1)

Buggy submission (verdict: Runtime Error):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
X = input()

def popcount(n):
	cnt = 0
	while 0 < n:
		cnt += 1
		p = "{:b}".format(n).count("1")
		n %= p
	return cnt

n = X.count("1")

n1 = n-1
n2 = n+1

q_1 = [0]*N
q_2 = [0]*N
for i in range(N):
	q_1[i] = pow(2, i, n1)
	q_2[i] = pow(2, i, n2)

mod_sum_1 = 0
mod_sum_2 = 0

for i in range(N):
	if X[-i-1] == "1":
		mod_sum_1 += q_1[i]
		mod_sum_2 += q_2[i]

q_1 = q_1[::-1]
q_2 = q_2[::-1]

for i in range(N):
	if X[i] == "1":
		m = n1
		mod_sum = mod_sum_1 - q_1[i]
	else:
		m = n2
		mod_sum = mod_sum_2 + q_2[i]
	print(popcount(mod_sum%m)+1)