Problem p02609 (python)

Fixed submission:
n = int(input())
x = list(input())

count = x.count('1')
one_count = count - 1
zero_count = count + 1

one_mod = 0
zero_mod = 0
for b in x:
    if one_count:
        one_mod = (one_mod * 2 + int(b)) % one_count
    zero_mod = (zero_mod * 2 + int(b)) % zero_count

f = [0] * 2000001
pop_count = [0] * 200001
for i in range(1, 200001):
    pop_count[i] = pop_count[i//2] + i % 2
    f[i] = f[i % pop_count[i]] + 1

for i in range(n):
    if x[i] == '1':
        if one_count:
            nxt = one_mod
            nxt -= pow(2, n-i-1, one_count)
            nxt %= one_count
            print(f[nxt] + 1)
        else:
            print(0)
    if x[i] == '0':
        nxt = zero_mod
        nxt += pow(2, n-i-1, zero_count)
        nxt %= zero_count
        print(f[nxt] + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = list(input())

count = x.count('1')
one_count = count - 1
zero_count = count + 1

one_mod = 0
zero_mod = 0
for b in x:
    if one_count:
        one_mod = (one_mod * 2 + int(b)) % one_count
    zero_mod = (zero_mod * 2 + int(b)) % zero_count

f = [0] * 22000
pop_count = [0] * 22000
for i in range(1, 22000):
    pop_count[i] = pop_count[i//2] + i % 2
    f[i] = f[i % pop_count[i]] + 1

for i in range(n):
    if x[i] == '1':
        if one_count:
            nxt = one_mod
            nxt -= pow(2, n-i-1, one_count)
            nxt %= one_count
            print(f[nxt] + 1)
        else:
            print(0)
    if x[i] == '0':
        nxt = zero_mod
        nxt += pow(2, n-i-1, zero_count)
        nxt %= zero_count
        print(f[nxt] + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = list(input())

count = x.count('1')
one_count = count - 1
zero_count = count + 1

one_mod = 0
zero_mod = 0
for b in x:
    if one_count:
        one_mod = (one_mod * 2 + int(b)) % one_count
    zero_mod = (zero_mod * 2 + int(b)) % zero_count

f = [0] * 20001
pop_count = [0] * 20001
for i in range(1, 20001):
    pop_count[i] = pop_count[i//2] + i % 2
    f[i] = f[i % pop_count[i]] + 1

for i in range(n):
    if x[i] == '1':
        if one_count:
            nxt = one_mod
            nxt -= pow(2, n-i-1, one_count)
            nxt %= one_count
            print(f[nxt] + 1)
        else:
            print(0)
    if x[i] == '0':
        nxt = zero_mod
        nxt += pow(2, n-i-1, zero_count)
        nxt %= zero_count
        print(f[nxt] + 1)

Buggy submission (verdict: Runtime Error):
def power(x, n):
    if n == 0: return 1
    value = power(x, int(n / 2))
    value *= value
    if n % 2 == 1: value *= x
    return value

n = int(input())
x = list(input())

number = int(''.join(x), 2)
count = x.count('1')

def f(num, cnt):
    if num == 0:
        return cnt
    popcount = format(num, 'b').count('1')
    return f(num % popcount, cnt+1)

for i in range(n):
    if x[i] == '0':
        m = number + power(2, n-i-1)
        if m:
            print(f(m % (count+1), 1))
        else:
            print(0)
    if x[i] == '1':
        m = number + power(2, n-i-1)
        if m:
            print(f(m % (count-1), 1))
        else:
            print(0)

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

def power(x, n):
    if n == 0: return 1
    value = power(x, int(n / 2))
    value *= value
    if n % 2 == 1: value *= x
    return value

n = int(input())
x = list(input())

counter = sum(list(map(int, x)))
number = sum([power(2, n-i-1) for i in range(n) if x[i] == '1'])

def f(num, cnt):
    if num == 0:
        return cnt
    popcount = sum(list(map(int, format(num, 'b'))))
    return f(num % popcount, cnt+1)

for i in range(n):
    if x[i] == '0':
        m = number + power(2, n-i-1)
        print(f(m % (counter+1), 1))
    if x[i] == '1':
        m = number - power(2, n-i-1)
        print(f(m % (counter-1), 1))

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

def power(x, n):
    if n == 0: return 1
    value = pow1(x, int(n / 2))
    value *= value
    if n % 2 == 1: value *= x
    return value

n = int(input())
x = list(input())

counter = sum(list(map(int, x)))
number = sum([power(2, n-i-1) for i in range(n) if x[i] == '1'])

def f(num, cnt):
    if num == 0:
        return cnt
    popcount = sum(list(map(int, format(num, 'b'))))
    return f(num % popcount, cnt+1)

for i in range(n):    
    if x[i] == '0':
        m = number + power(2, n-i-1)
        print(f(m % (counter+1), 1))
    if x[i] == '1':
        m = number - power(2, n-i-1)
        print(f(m % (counter-1), 1))

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

n = int(input())
x = list(input())

counter = sum(list(map(int, x)))
number = sum([pow(2, n-i-1) for i in range(n) if x[i] == '1'])

def f(num, cnt):
    if num == 0:
        return cnt
    popcount = sum(list(map(int, format(num, 'b'))))
    return f(num % popcount, cnt+1)

for i in range(n):    
    if x[i] == '0':
        m = number + pow(2, n-i-1)
        print(f(m % (counter+1), 1))
    if x[i] == '1':
        m = number - pow(2, n-i-1)
        print(f(m % (counter-1), 1))

Buggy submission (verdict: Runtime Error):
n = int(input())
x = list(input())

counter = sum(list(map(int, x)))
number = int(''.join(x), 2)

def f(num, cnt):
    if num == 0:
        return cnt
    popcount = sum(list(map(int, format(num, 'b'))))
    return f(num % popcount, cnt+1)

for i in range(n):    
    if x[i] == '0':
        m = number + pow(2, n-i-1)
        print(f(m % (counter+1), 1))
    if x[i] == '1':
        m = number - pow(2, n-i-1)
        print(f(m % (counter-1), 1))

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

def pow_k(x, n):
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

n = int(input())
x = list(input())

counter = x.count('1')
number = int(''.join(x), 2)

def f(num, cnt):
    if num == 0:
        return cnt
    popcount = format(num, 'b').count('1')
    return f(num % popcount, cnt+1)

for i in range(n):    
    if x[i] == '0':
        m = number + pow_k(2, n-i-1)
        print(f(m % (counter+1), 1))
    if x[i] == '1':
        m = number - pow_k(2, n-i-1)
        print(f(m % (counter-1), 1))

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

def pow_k(x, n):
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

n = int(input())
x = list(input())

popcount = [0] * (10**6)
for i in range(10**6):
    popcount[i] = format(i, 'b').count('1')

counter = x.count('1')
number = int(''.join(x), 2)

def f(num, cnt):
    if num == 0:
        return cnt
    return f(num % popcount[num], cnt+1)

for i in range(n):    
    if x[i] == '0':
        m = number + pow_k(2, n-i-1)
        print(f(m % (counter+1), 1))
    if x[i] == '1':
        m = number - pow_k(2, n-i-1)
        print(f(m % (counter-1), 1))

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

def pow_k(x, n):
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

n = int(input())
x = list(input())

popcount = [0] * (10**5 + 1)
for i in range(10**5+1):
    popcount[i] = format(i, 'b').count('1')

counter = x.count('1')
number = int(''.join(x), 2)

def f(num, cnt):
    if num == 0:
        return cnt
    return f(num % popcount[num], cnt+1)

for i in range(n):    
    if x[i] == '0':
        m = number + pow_k(2, n-i-1)
        print(f(m % (counter+1), 1))
    if x[i] == '1':
        m = number - pow_k(2, n-i-1)
        print(f(m % (counter-1), 1))