Problem p02609 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10**6)


def f(n):
    if n == 0:
        return 1
    p = n % (bin(n).count('1'))
    return f(p) + 1


def main(input, print):
    N = int(input())
    X = list(map(int, list(input().strip())))
    ans = [0] * N

    pcnt = 0
    for i in range(N):
        if X[i] == 1:
            pcnt += 1
    for is_up in range(2):
        current_pcnt = pcnt
        if is_up:
            current_pcnt -= 1
        else:
            current_pcnt += 1
        if current_pcnt <= 0:
            continue
        total = 0
        for i in range(N):
            total = (total*2) % current_pcnt
            total += X[i]
        current_mask = 1
        for i in reversed(range(N)):
            if(X[i] == is_up):
                current_total = total
                if is_up:
                    current_total = (current_total-current_mask+current_pcnt) % current_pcnt
                else:
                    current_total = (current_total+current_mask) % current_pcnt
                ans[i] = f(current_total)
            current_mask = current_mask * 2 % current_pcnt

    for i in range(N):
        print(ans[i])


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)


def f(n):
    if n == 0:
        return 1
    p = n % (bin(n).count('1'))
    return f(p) + 1


def main(input, print):
    N = int(input())
    X = list(map(int, list(input())))
    ans = [0] * N

    pcnt = 0
    for i in range(N):
        if X[i] == 1:
            pcnt += 1
    for is_up in range(2):
        current_pcnt = pcnt
        if is_up:
            current_pcnt -= 1
        else:
            current_pcnt += 1
        if current_pcnt <= 0:
            continue
        total = 0
        for i in range(N):
            total = (total*2) % current_pcnt
            total += X[i]
        current_mask = 1
        for i in reversed(range(N)):
            if(X[i] == is_up):
                current_total = total
                if is_up:
                    current_total = (current_total-current_mask+current_pcnt) % current_pcnt
                else:
                    current_total = (current_total+current_mask) % current_pcnt
                ans[i] = f(current_total)
            current_mask = current_mask * 2 % current_pcnt

    for i in range(N):
        print(ans[i])


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)


def f(n):
    p = n % (bin(n).count('1'))
    if p == 0:
        return 1
    else:
        return f(p) + 1


def main(input, print):
    N = int(input())
    X = list(input())
    for i in range(N):
        a = X[:]
        a[i] = "1" if a[i] == "0" else "0"
        b = int(''.join(a), 2)
        print(f(b))


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)


def f(n):
    p = n % (bin(n).count('1'))
    if p == 0:
        return 1
    else:
        return f(p) + 1


def main(input, print):
    N = int(input())
    X = int(input(), 2)
    for i in reversed(range(N)):
        b = 1 << i
        a = X ^ b
        print(f(a))


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**6)


def f(n):
    p = n % popcnt(n)
    if p == 0:
        return 1
    else:
        return f(p) + 1


def popcnt(n):
    c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555)
    c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333)
    c = (c & 0x0f0f0f0f0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f0f0f0f0f)
    c = (c & 0x00ff00ff00ff00ff) + ((c >> 8) & 0x00ff00ff00ff00ff)
    c = (c & 0x0000ffff0000ffff) + ((c >> 16) & 0x0000ffff0000ffff)
    c = (c & 0x00000000ffffffff) + ((c >> 32) & 0x00000000ffffffff)
    return c


def main(input, print):
    N = int(input())
    X = int(input(), 2)
    for i in reversed(range(N)):
        b = 1 << i
        a = X ^ b
        print(f(a))


if __name__ == '__main__':
    main(sys.stdin.readline, print)