Problem p02609 (python)

Fixed submission:
n = int(input())
x = str(input())
kosu1 = 0
kazu = 0
for i in range(n):
    kazu += 2 ** (n - i - 1) * int(x[i])
    if x[i] == '1':
        kosu1 += 1
if kosu1 ==1:
    kazup = kazu % (kosu1 + 1)
    magic17 = [0, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 3, 1, 2]
    box = [0] * (kosu1 + 1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1, n):
        amarip[i] = (amarip[i - 1] * 2) % (kosu1 + 1)
    for i in range(n):
        if x[i] == '1':
            print(0)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)

            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)))-2):
                    if str(bin(a))[-1 - i] == '1':

                        num += 1
                print(magic17[a % num] + 2)
else:
    kazup = kazu % (kosu1 + 1)
    kazum = kazu % (kosu1 - 1)
    magic17 = [0,1,1,2,1,2,1,2,1,2,1,2,1,2,2,3,1,2,1]
    box = [0] * (kosu1 +1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1,n):
        amarip[i] = (amarip[i - 1]*2) % (kosu1 + 1)
        amarim[i] = (amarim[i - 1]*2) % (kosu1 - 1)
    for i in range(n):
        if x[i] == '1':
            a = (kazum - amarim[n-1-i]) % (kosu1 - 1)

            if a <= 18:
                print(magic17[a]+1)
            else:
                num = 0

                for i in range(len(str(bin(a)))-2):
                    if str(bin(a))[-1-i] == '1':

                        num += 1
                print(magic17[a % num] + 2)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)
            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)))-2):
                    if str(bin(a))[-1 - i] == '1':

                        num += 1
                print(magic17[a % num] + 2)

Buggy submission (verdict: Runtime Error):
x = str(input())
kosu1 = 0
kazu = 0
for i in range(n):
    kazu += 2 ** (n - i - 1) * int(x[i])
    if x[i] == '1':
        kosu1 += 1
if kosu1 ==1:
    kazup = kazu % (kosu1 + 1)
    magic17 = [0, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 3, 1, 2]
    box = [0] * (kosu1 + 1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1, n):
        amarip[i] = (amarip[i - 1] * 2) % (kosu1 + 1)
    for i in range(n):
        if x[i] == '1':
            print(0)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)

            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == '1':
                        num += 1
                print(magic17[a % num] + 1)
else:
    kazup = kazu % (kosu1 + 1)
    kazum = kazu % (kosu1 - 1)
    magic17 = [0,1,1,2,1,2,1,2,1,2,1,2,1,2,2,3,1,2,1]
    box = [0] * (kosu1 +1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1,n):
        amarip[i] = (amarip[i - 1]*2) % (kosu1 + 1)
        amarim[i] = (amarim[i - 1]*2) % (kosu1 - 1)
    for i in range(n):
        if x[i] == '1':
            a = (kazum - amarim[n-1-i]) % (kosu1 - 1)

            if a <= 18:
                print(magic17[a]+1)
            else:
                num = 0
                for i in range(len(str(bin(a))-2)):
                    if str(bin(a))[-1-i] == '1':
                        num += 1
                print(magic17[a % num] + 1)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)
            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == '1':
                        num += 1
                print(magic17[a % num] + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = str(input())
kosu1 = 0
kazu = 0
for i in range(n):
    kazu += 2 ** (n - i - 1) * int(x[i])
    if x[i] == '1':
        kosu1 += 1
if kosu1 ==1:
    kazup = kazu % (kosu1 + 1)
    magic17 = [0, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 3, 1, 2]
    box = [0] * (kosu1 + 1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1, n):
        amarip[i] = (amarip[i - 1] * 2) % (kosu1 + 1)
    for i in range(n):
        if x[i] == '1':
            print(0)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)

            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == '1':
                        num += 1
                print(magic17[a % num] + 1)
else:
    kazup = kazu % (kosu1 + 1)
    kazum = kazu % (kosu1 - 1)
    magic17 = [0,1,1,2,1,2,1,2,1,2,1,2,1,2,2,3,1,2,1]
    box = [0] * (kosu1 +1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1,n):
        amarip[i] = (amarip[i - 1]*2) % (kosu1 + 1)
        amarim[i] = (amarim[i - 1]*2) % (kosu1 - 1)
    for i in range(n):
        if x[i] == '1':
            a = (kazum - amarim[n-1-i]) % (kosu1 - 1)

            if a <= 18:
                print(magic17[a]+1)
            else:
                num = 0
                for i in range(len(str(bin(a))-2)):
                    if str(bin(a))[-1-i] == '1':
                        num += 1
                print(magic17[a % num] + 1)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)
            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == '1':
                        num += 1
                print(magic17[a % num] + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = str(input())
kosu1 = 0
kazu = 0
for i in range(n):
    kazu += 2 ** (n - i - 1) * int(x[i])
    if x[i] == '1':
        kosu1 += 1
if kosu1 ==1:
    kazup = kazu % (kosu1 + 1)
    magic17 = [0, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 3, 1, 2]
    box = [0] * (kosu1 + 1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1, n):
        amarip[i] = (amarip[i - 1] * 2) % (kosu1 + 1)
    for i in range(n):
        if x[i] == '1':
            print(0)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)
            # print(2121,a)
            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == 1:
                        num += 1
                print(magic17[a % num] + 1)
else:
    kazup = kazu %(kosu1 + 1)
    kazum = kazu %(kosu1 - 1)
    magic17 = [0,1,1,2,1,2,1,2,1,2,1,2,1,2,2,3,1,2,1]
    box = [0] * (kosu1 +1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1,n):
        amarip[i] = (amarip[i - 1]*2) % (kosu1 + 1)
        amarim[i] = (amarim[i - 1]*2) % (kosu1 - 1)
    for i in range(n):
        if x[i] == '1':
            a = (kazum - amarim[n-1-i]) % (kosu1 - 1)
            #print(a,kazup,kazum,amarip,amarim,kosu1)
            if a <= 18:
                print(magic17[a]+1)
            else:
                num = 0
                for i in range(len(str(bin(a))-2)):
                    if str(bin(a))[-1-i] == 1:
                        num += 1
                print(magic17[a % num] + 1)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)
            if a <= 18:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == 1:
                        num += 1
                print(magic17[a % num] + 1)

Buggy submission (verdict: Runtime Error):
n = int(input())
x = str(input())
kosu1 = 0
kazu = 0
for i in range(n):
    kazu += 2 ** (n - i - 1) * int(x[i])
    if x[i] == '1':
        kosu1 += 1
if kosu1 ==1:
    kazup = kazu % (kosu1 + 1)
    magic17 = [0, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 3, 1, 2]
    box = [0] * (kosu1 + 1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1, n):
        amarip[i] = (amarip[i - 1] * 2) % (kosu1 + 1)
    for i in range(n):
        if x[i] == '1':
            print(0)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)
            # print(2121,a)
            if a <= 17:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == 1:
                        num += 1
                print(magic17[a % num] + 1)
else:
    kazup = kazu %(kosu1 + 1)
    kazum = kazu %(kosu1 - 1)
    magic17 = [0,1,1,2,1,2,1,2,1,2,1,2,1,2,2,3,1,2]
    box = [0] * (kosu1 +1)

    amarip = [1] * n
    amarim = [1] * n
    for i in range(1,n):
        amarip[i] = (amarip[i - 1]*2) % (kosu1 + 1)
        amarim[i] = (amarim[i - 1]*2) % (kosu1 - 1)
    for i in range(n):
        if x[i] == '1':
            a = (kazum - amarim[n-1-i]) % (kosu1 - 1)
            #print(a,kazup,kazum,amarip,amarim,kosu1)
            if a <= 17:
                print(magic17[a]+1)
            else:
                num = 0
                for i in range(len(str(bin(a))-2)):
                    if str(bin(a))[-1-i] == 1:
                        num += 1
                print(magic17[a%num] + 1)
        else:
            a = (kazup + amarip[n - 1 - i]) % (kosu1 + 1)
            #print(2121,a)
            if a <= 17:
                print(magic17[a] + 1)
            else:
                num = 0
                for i in range(len(str(bin(a)) - 2)):
                    if str(bin(a))[-1 - i] == 1:
                        num += 1
                print(magic17[a % num] + 1)