Problem p02609 (python)

Fixed submission:
def main():
    n_ansl = [0]*(2*(10**5))
    for i in range(1,2*(10**5)):
    # for i in range(1,20):
        cnt = 0
        one_cnt = bin(i).count("1")
        rem = i
        while rem != 0:
            one_cnt = bin(rem).count("1")
            rem = rem%one_cnt   
            cnt += 1
        n_ansl[i] = cnt
 
    n = int(input())
    x = input()
    one_cnt = x.count('1')

    if one_cnt == 0:
        for i in range(n):
            print(1)
        exit()
    
    curr_2b = 1
    curr_2b_p = 1
    curr_2b_m = 1
    curr_sum = 0
    curr_sum_p1 = 0
    curr_sum_m1 = 0
    for i in range(n-1, -1, -1):
        if x[i] == '1':
            curr_sum += curr_2b
            curr_sum_p1 += curr_2b_p
            curr_sum_m1 += curr_2b_m
            curr_sum%=one_cnt
            curr_sum_p1%=(one_cnt+1)
            if one_cnt > 1:
                curr_sum_m1%=(one_cnt-1)
        curr_2b*=2
        curr_2b_p*=2
        curr_2b_m*=2
        curr_2b%=one_cnt
        curr_2b_p%=(one_cnt+1)
        if one_cnt > 1:
            curr_2b_m%=(one_cnt-1)
 
        
 
    curr_2b = 1
    curr_2b_p = 1
    curr_2b_m = 1
    ansl = [0]*(n)
    for i in range(n-1, -1, -1):
        if x[i] == '1':
            diff = (-1)*curr_2b_m
            if one_cnt > 1:
                ans = (curr_sum_m1 + diff)%(one_cnt-1)
                ansl[i] = n_ansl[ans]+1
            else:
                ansl[i] = 0
        else:
            diff = curr_2b_p
            ans = (curr_sum_p1 + diff)%(one_cnt+1)
            ansl[i] = (n_ansl[ans]+1)
        # curr_2b *= 2
        curr_2b*=2
        curr_2b_p*=2
        curr_2b_m*=2
        curr_2b%=one_cnt
        curr_2b_p%=(one_cnt+1)
        if one_cnt > 1:
            curr_2b_m%=(one_cnt-1)
 
 
    # print(ansl)
    for a in ansl:
        print(a)
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
n = int(input())
x = input()
x = int(x, 2)
print(x)

ansl = [0]*n
for i in range(1,n):
    ans = 0
    num = i
    while True:
        pc = bin(num).count("1")
        num %= pc
        ans += 1
        if num == 0: break
    ansl[i] = ans

print(ansl)
for i in range(n):
    bit = (1<<(n-1-i))
    num = x^bit
    pc = bin(num).count("1")
    next_num = num%pc
    a = ansl[next_num] + 1
    print(a)

Buggy submission (verdict: Runtime Error):
def main():
    n_ansl = [0]*(2*(10**5))
    for i in range(1,2*(10**5)):
    # for i in range(1,20):
        cnt = 0
        one_cnt = bin(i).count("1")
        rem = i
        while rem != 0:
            one_cnt = bin(rem).count("1")
            rem = rem%one_cnt   
            cnt += 1
        n_ansl[i] = cnt
 
    n = int(input())
    x = input()
    one_cnt = x.count('1')

    if one_cnt == 0:
        for i in range(n):
            print(1)
        exit()
    
    curr_2b_p = 1
    curr_2b_m = 1
    curr_sum_p1 = 0
    curr_sum_m1 = 0
    for i in range(n-1, -1, -1):
        if x[i] == '1':
            curr_sum_p1 += curr_2b_p
            curr_sum_m1 += curr_2b_m
            curr_sum_p1%=(one_cnt+1)
            if one_cnt > 1:
                curr_sum_m1%=(one_cnt-1)
        curr_2b_p*=2
        curr_2b_m*=2
        curr_2b%=one_cnt
        curr_2b_p%=(one_cnt+1)
        if one_cnt > 1:
            curr_2b_m%=(one_cnt-1)
 
        
 
    curr_2b_p = 1
    curr_2b_m = 1
    ansl = [0]*(n)
    for i in range(n-1, -1, -1):
        if x[i] == '1':
            diff = (-1)*curr_2b_m
            if one_cnt > 1:
                ans = (curr_sum_m1 + diff)%(one_cnt-1)
                ansl[i] = n_ansl[ans]+1
            else:
                ansl[i] = 0
        else:
            diff = curr_2b_p
            ans = (curr_sum_p1 + diff)%(one_cnt+1)
            ansl[i] = (n_ansl[ans]+1)
        curr_2b_p*=2
        curr_2b_m*=2
        curr_2b_p%=(one_cnt+1)
        if one_cnt > 1:
            curr_2b_m%=(one_cnt-1)
 
 
    # print(ansl)
    for a in ansl:
        print(a)
 
if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):

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

# def main():
n_ansl = [0]*(n)
for i in range(1,n):
# for i in range(1,20):
    cnt = 0
    one_cnt = bin(i).count("1")
    rem = i
    while rem != 0:
        one_cnt = bin(rem).count("1")
        rem = rem%one_cnt   
        cnt += 1
    n_ansl[i] = cnt


one_cnt = x.count('1')

if one_cnt == 0:
    for _ in range(n):
        print(1)

c2bl = [1]*n
c2blp = [1]*n
c2blm = [1]*n

curr_2b = 1
curr_2b_p = 1
curr_2b_m = 1
curr_sum = 0
curr_sum_p1 = 0
curr_sum_m1 = 0
for i in range(n-1, -1, -1):
    c2bl[i] = curr_2b
    c2blp[i] = curr_2b_p
    c2blm[i] = curr_2b_m
    if x[i] == '1':
        curr_sum += curr_2b
        curr_sum_p1 += curr_2b_p
        curr_sum_m1 += curr_2b_m
        curr_sum%=one_cnt
        curr_sum_p1%=(one_cnt+1)
        if one_cnt > 1:
            curr_sum_m1%=(one_cnt-1)
    curr_2b*=2
    curr_2b_p*=2
    curr_2b_m*=2
    curr_2b%=one_cnt
    curr_2b_p%=(one_cnt+1)
    if one_cnt > 1:
        curr_2b_m%=(one_cnt-1)



    
curr_2b = 1
curr_2b_p = 1
curr_2b_m = 1
ansl = [0]*(n)
for i in range(n-1, -1, -1):
    if x[i] == '1':
        # diff = (-1)*curr_2b_m
        diff = (-1)*c2blm[i]
        if one_cnt > 1:
            ans = (curr_sum_m1 + diff)%(one_cnt-1)
            ansl[i] = n_ansl[ans]+1
        else:
            ansl[i] = 0
    else:
        # diff = curr_2b_p
        diff = c2blp[i]
        ans = (curr_sum_p1 + diff)%(one_cnt+1)
        ansl[i] = (n_ansl[ans]+1)
    # curr_2b *= 2
    # curr_2b*=2
    # curr_2b_p*=2
    # curr_2b_m*=2
    # curr_2b%=one_cnt
    # curr_2b_p%=(one_cnt+1)
    # if one_cnt > 1:
    #     curr_2b_m%=(one_cnt-1)


# print(ansl)
for a in ansl:
    print(a)

# if __name__ == "__main__":
#     main()

Buggy submission (verdict: Runtime Error):


def main():
    n_ansl = [0]*(2*(10**5))
    for i in range(1,2*(10**5)):
    # for i in range(1,20):
        cnt = 0
        one_cnt = bin(i).count("1")
        rem = i
        while rem != 0:
            one_cnt = bin(rem).count("1")
            rem = rem%one_cnt   
            cnt += 1
        n_ansl[i] = cnt

    n = int(input())
    x = input()
    one_cnt = x.count('1')
    
    curr_2b = 1
    curr_2b_p = 1
    curr_2b_m = 1
    curr_sum = 0
    curr_sum_p1 = 0
    curr_sum_m1 = 0
    for i in range(n-1, -1, -1):
        if x[i] == '1':
            curr_sum += curr_2b
            curr_sum_p1 += curr_2b_p
            curr_sum_m1 += curr_2b_m
            curr_sum%=one_cnt
            curr_sum_p1%=(one_cnt+1)
            if one_cnt > 1:
                curr_sum_m1%=(one_cnt-1)
        curr_2b*=2
        curr_2b_p*=2
        curr_2b_m*=2
        curr_2b%=one_cnt
        curr_2b_p%=(one_cnt+1)
        if one_cnt > 1:
            curr_2b_m%=(one_cnt-1)

        

    curr_2b = 1
    curr_2b_p = 1
    curr_2b_m = 1
    ansl = [0]*(n)
    for i in range(n-1, -1, -1):
        if x[i] == '1':
            diff = (-1)*curr_2b_m
            if one_cnt > 1:
                ans = (curr_sum_m1 + diff)%(one_cnt-1)
                ansl[i] = n_ansl[ans]+1
            else:
                ansl[i] = 0
        else:
            diff = curr_2b_p
            ans = (curr_sum_p1 + diff)%(one_cnt+1)
            ansl[i] = (n_ansl[ans]+1)
        # curr_2b *= 2
        curr_2b*=2
        curr_2b_p*=2
        curr_2b_m*=2
        curr_2b%=one_cnt
        curr_2b_p%=(one_cnt+1)
        if one_cnt > 1:
            curr_2b_m%=(one_cnt-1)


    # print(ansl)
    for a in ansl:
        print(a)

if __name__ == "__main__":
    main()