Problem p02609 (python)

Fixed submission:
def popcnt_int(n): # n:int
    return bin(n).count('1')

def popcnt_bin(s): # s:str
    return s.count('1')

def poptimes(n, cnt=1): #:int
    if n==0:
        return cnt
    else:
        n %= popcnt_int(n)
        cnt += 1
        return poptimes(n, cnt)

n = int(input())
s = input()

s_int = int(s,2)
pc = popcnt_bin(s)
pc_plus = s_int % (pc+1)
if  pc != 1:
    pc_minus = s_int % (pc-1)
#print(n,s,s_int,pc,pc_plus,pc_minus)

for i in range(n):
    if s[i] == '1':
        if pc != 1:
            tmp = pc_minus - pow(2, n-i-1, pc-1)
            print(poptimes(tmp % (pc-1)))
        else:
            print(0)
            continue
    
    else:
        tmp = pc_plus + pow(2, n-i-1, pc+1)
        print(poptimes(tmp % (pc+1)))

Buggy submission (verdict: Runtime Error):
def popcnt_int(n): # n:int
    return bin(n).count('1')

def popcnt_bin(s): # s:str
    return s.count('1')

def poptimes(n, cnt=1): #:int
    if n==0:
        return cnt
    else:
        n %= popcnt_int(n)
        cnt += 1
        return poptimes(n, cnt)

n = int(input())
s = input()

s_int = int(s,2)
pc = popcnt_bin(s)
pc_plus = s_int % (pc+1)
pc_minus = s_int % (pc-1)
#print(n,s,s_int,pc,pc_plus,pc_minus)

for i in range(n):
    if s[i] == '1':
        if pc != 1:
            tmp = pc_minus - pow(2, n-i-1, pc-1)
            print(poptimes(tmp % (pc-1)))
        else:
            print(0)
            continue
    
    else:
        tmp = pc_plus + pow(2, n-i-1, pc+1)
        print(poptimes(tmp % (pc+1)))

Buggy submission (verdict: Runtime Error):
def popcnt_int(n): # n:int
    return bin(n).count('1')

def popcnt_bin(s): # s:str
    return s.count('1')

def poptimes(n, cnt=1): #:int
    if n==0:
        return cnt
    else:
        n %= popcnt_int(n)
        cnt += 1
        return poptimes(n, cnt)

n = int(input())
s = input()

s_int = int(s,2)
pc = popcnt_bin(s)
pc_plus = pc+1
pc_minus = pc-1
#print(n,s,s_int,pc,pc_plus,pc_minus)

l_plus = [2**(n-i-1) % (pc+1) for i in range(n)]
l_minus = [2**(n-i-1) % (pc-1) for i in range(n)]
#print(l_plus)
#print(l_minus)

for i in range(n):
    if s[i] == '1':
        if pc != 1:
            tmp = (s_int % pc_minus) - (l_minus[i] % pc_minus)
            print(poptimes(tmp))
        else:
            print(0)
            continue
    
    else:
        tmp = (s_int % pc_plus) + (l_plus[i] % pc_plus)
        print(poptimes(tmp))

Buggy submission (verdict: Runtime Error):
def popcnt_int(n): # n:int
    return bin(n).count('1')

def popcnt_bin(s): # s:str
    return s.count('1')

def poptimes(n, cnt=1): #:int
    if n==0:
        return cnt
    else:
        n %= popcnt_int(n)
        cnt += 1
        return poptimes(n, cnt)

n = int(input())
s = input()

s_int = int(s,2)
pc = popcnt_bin(s)
pc_plus = s_int % (pc+1)
pc_minus = s_int % (pc-1)
#print(n,s,s_int,pc,pc_plus,pc_minus)

l_plus = [2**(n-i-1) % (pc+1) for i in range(n)]
l_minus = [2**(n-i-1) % (pc-1) for i in range(n)]
#print(l_plus)
#print(l_minus)

for i in range(n):
    if s[i] == '1':
        if pc != 1:
            tmp = (pc_minus - l_minus[i]) % (pc-1)
            print(poptimes(tmp))
        else:
            print(0)
            continue
    
    else:
        tmp = (pc_plus + l_plus[i]) % (pc+1)
        print(poptimes(tmp))

Buggy submission (verdict: Runtime Error):
def popcnt_int(n): # n:int
    return bin(n).count('1')

def popcnt_bin(s): # s:str
    return s.count('1')

def poptimes(n, cnt=1): #:int
    if n==0:
        return cnt
    else:
        n %= popcnt_int(n)
        cnt += 1
        return poptimes(n, cnt)

n = int(input())
s = input()

s_int = int(s,2)
pc = popcnt_bin(s)
pc_plus = pc+1
pc_minus = pc-1
#print(n,s,s_int,pc,pc_plus,pc_minus)

l_plus = [2**(n-i-1) % (pc_plus) for i in range(n)]
l_minus = [2**(n-i-1) % (pc_minus) for i in range(n)]
#print(l_plus)
#print(l_minus)

for i in range(n):
    if s[i] == '1':
        if pc != 1:
            tmp = (s_int - l_minus[i]) % (pc_minus)
            print(poptimes(tmp))
        else:
            print(0)
            continue
    
    else:
        tmp = (s_int + l_plus[i]) % (pc_plus)
        print(poptimes(tmp))