Problem p02609 (python)

Fixed submission:
import numpy as np

N = int(input())
X = str(input())
num_one = X.count("1")

dp = [-1] * N
dp[0] = 0
def dfs(n):
    if dp[n] == -1:
        dp[n] = 1 + dfs(n % bin(n).count('1'))
    return dp[n]

num_one = X.count("1")
bool_arr = np.array([True if X[N-i-1] == "1" else False for i in range(N)])
zero_ver = np.array([pow(2,  i, num_one + 1) for i in range(N)])
zero_ver_sum = sum(zero_ver[bool_arr])

one_ver = -1
one_ver_sum = 0
flag = False
if num_one != 1:
    one_ver = np.array([pow(2,  i, num_one - 1) for i in range(N)])
    one_ver_sum = sum(one_ver[bool_arr])
else:
    flag = True
for i in range(1,N+1):
    start = 0
    if bool_arr[N-i] == False:
        start = (zero_ver_sum + pow(2, N - i, num_one + 1)) % (num_one + 1)
        print(dfs(start)+1)
    else:
        if flag:
            print(0)
        else:
            start = (one_ver_sum - pow(2, N - i, num_one - 1))  %  (num_one - 1)
            print(dfs(start)+1)

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
X = str(input())
num_one = X.count("1")

dp = [-1] * N
dp[0] = 0
def dfs(n):
    if dp[n] == -1:
        dp[n] = 1 + dfs(n % bin(n).count('1'))
    return dp[n]

num_one = X.count("1")
bool_arr = np.array([True if X[N-i-1] == "1" else False for i in range(N)])
zero_ver = np.array([pow(2,  i, num_one + 1) for i in range(N)])
zero_ver_sum = sum(zero_ver[bool_arr])

one_ver = -1
flag = False
if num_one != 1:
    one_ver = np.array([pow(2,  i, num_one - 1) for i in range(N)])
else:
    flag = True
one_ver_sum = sum(one_ver[bool_arr])
for i in range(1,N+1):
    start = 0
    if bool_arr[N-i] == False:
        start = (zero_ver_sum + pow(2, N - i, num_one + 1)) % (num_one + 1)
        print(dfs(start)+1)
    else:
        if flag:
            print(0)
        else:
            start = (one_ver_sum - pow(2, N - i, num_one - 1))  %  (num_one - 1)
            print(dfs(start)+1)

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
X = str(input())

def func(x):
    if a[x] != -1:
        return a[x]
    else:
        counter = 0
        x_tmp = x
        while x > 0:
            if x % 2 == 1:
                counter += 1
            x = x >> 1
        a[x_tmp] = func(x_tmp % counter) + 1
        return func(x_tmp % counter) + 1
if N <= 10:
    print(func(int(X, 2)))
    
else:
    a = [-1] * (2 * 10 ** 5)
    a[0] = 0
    num_one = X.count("1")
    zero_ver = np.array([pow(2,  i, num_one + 1) for i in range(N)])
    one_ver = np.array([pow(2,  i, num_one - 1) for i in range(N)])
    bool_arr = np.array([True if X[N-i-1] == "1" else False for i in range(N)])
    for i in range(1,N+1):
        tmp_bool = bool_arr.copy()
        start = 0
        if tmp_bool[N-i] == False:
            tmp_bool[N-i] = True
            start = sum(zero_ver[tmp_bool]) % (num_one + 1)
        else:
            tmp_bool[N-i] = False
            start = sum(one_ver[tmp_bool]) %  (num_one - 1)
        print(func(start)+1)

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
X = str(input())
num_one = X.count("1")
zero_ver = np.array([pow(2,  i, num_one + 1) for i in range(N)])
one_ver = np.array([pow(2,  i, num_one - 1) for i in range(N)])
bool_arr = np.array([True if X[N-i-1] == "1" else False for i in range(N)])


def func(x):
    if x == 0:
        return 0
    else:
        counter = 0
        x_tmp = x
        while x > 0:
            if x % 2 == 1:
                counter += 1
            x = x >> 1
        return func(x_tmp % counter) + 1

for i in range(1,N+1):
    tmp_bool = bool_arr.copy()
    start = 0
    if tmp_bool[N-i] == False:
        tmp_bool[N-i] = True
        start = sum(zero_ver[tmp_bool]) % (num_one + 1)
    else:
        tmp_bool[N-i] = False
        start = sum(one_ver[tmp_bool]) %  (num_one - 1)
    print(func(start)+1)