Problem p02609 (python)

Fixed submission:
# @author 

import sys

class DAnythingGoesToZero:
    def solve(self, tc=0):
        n = int(input())
        # n = 10 ** 5
        x = input().strip()
        cache = [-1] * (n + 1)
        cache[0] = 0
        for nb in range(n + 1):
            v = 0
            i = nb
            while cache[i] == -1:
                i %= bin(i).count('1')
                v += 1

            cache[nb] = v + cache[i]

        # print(cache[8191])

        def brem(N, D):
            n = len(N)
            R = 0
            for i in range(n - 1, -1, -1):
                R <<= 1
                R |= int(N[n - 1 - i])
                if R >= D:
                    R -= D
            return R

        x = list(x)
        ones = x.count('1')
        r1 = brem(x, ones + 1)
        r2 = brem(x, ones - 1) if ones > 1 else 0
        for i in range(n):
            if x[i] == '0':
                # print((r + pow(2, n - 1 - i, ones + 1)) % (ones + 1), '*')
                print(cache[(r1 + pow(2, n - 1 - i, ones + 1)) % (ones + 1)] + 1)
            else:
                if ones == 1:
                    print(0)
                    continue
                print(cache[(r2 - pow(2, n - 1 - i, ones - 1)) % (ones - 1)] + 1)
            # x[i] = str(int(x[i]) ^ 1)
            # print(cache[brem(x, x.count('1'))] + 1)
            # x[i] = str(int(x[i]) ^ 1)

solver = DAnythingGoesToZero()
input = sys.stdin.readline

solver.solve()

Buggy submission (verdict: Runtime Error):
# @author 

import sys

class DAnythingGoesToZero:
    def solve(self, tc=0):
        n = int(input())
        # n = 10 ** 5
        x = input().strip()
        cache = [-1] * (n + 1)
        cache[0] = 0
        for nb in range(n + 1):
            v = 0
            i = nb
            while cache[i] == -1:
                i %= bin(i).count('1')
                v += 1

            cache[nb] = v + cache[i]

        # print(cache[8191])

        def brem(N, D):
            n = len(N)
            R = 0
            for i in range(n - 1, -1, -1):
                R <<= 1
                R |= int(N[n - 1 - i])
                if R >= D:
                    R -= D
            return R

        x = list(x)
        ones = x.count('1')
        r1 = brem(x, ones + 1)
        r2 = brem(x, ones - 1) if ones != 0 else 0
        for i in range(n):
            if x[i] == '0':
                # print((r + pow(2, n - 1 - i, ones + 1)) % (ones + 1), '*')
                print(cache[(r1 + pow(2, n - 1 - i, ones + 1)) % (ones + 1)] + 1)
            else:
                print(cache[(r2 - pow(2, n - 1 - i, ones - 1)) % (ones - 1)] + 1)
            # x[i] = str(int(x[i]) ^ 1)
            # print(cache[brem(x, x.count('1'))] + 1)
            # x[i] = str(int(x[i]) ^ 1)

solver = DAnythingGoesToZero()
input = sys.stdin.readline

solver.solve()

Buggy submission (verdict: Runtime Error):
# @author 

import sys

class DAnythingGoesToZero:
    def solve(self, tc=0):
        n = int(input())
        # n = 10 ** 5
        x = input().strip()
        cache = [-1] * (n + 1)
        cache[0] = 0
        for nb in range(n + 1):
            v = 0
            i = nb
            while cache[i] == -1:
                i %= bin(i).count('1')
                v += 1

            cache[nb] = v + cache[i]

        # print(cache[8191])

        def brem(N, D):
            n = len(N)
            R = 0
            for i in range(n - 1, -1, -1):
                R <<= 1
                R |= int(N[n - 1 - i])
                if R >= D:
                    R -= D
            return R

        x = list(x)
        ones = x.count('1')
        r1 = brem(x, ones + 1)
        r2 = brem(x, ones - 1)
        for i in range(n):
            if x[i] == '0':
                # print((r + pow(2, n - 1 - i, ones + 1)) % (ones + 1), '*')
                print(cache[(r1 + pow(2, n - 1 - i, ones + 1)) % (ones + 1)] + 1)
            else:
                print(cache[(r2 - pow(2, n - 1 - i, ones - 1)) % (ones - 1)] + 1)
            # x[i] = str(int(x[i]) ^ 1)
            # print(cache[brem(x, x.count('1'))] + 1)
            # x[i] = str(int(x[i]) ^ 1)

solver = DAnythingGoesToZero()
input = sys.stdin.readline

solver.solve()