Problem p02609 (python)

Fixed submission:
def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f

def g(num_in):
    num = num_in
    cnt = 0
    if num == 0:
        return 0

    while True:
        base = popcount(num)
        if num%base == 0:
            cnt += 1
            break
        else:
            num = num%base
            cnt += 1
    return cnt


n = int(input())
s = input()

st = 0
for c in s:
    if c == "1":
        st += 1

if st > 1:

    mod_p1 = [0 for i in range(n)]
    mod_n1 = [0 for i in range(n)]
    base1 = 1
    base2 = 1
    for i in range(n):
        t1 = base1%(st+1)
        t2 = base2%(st-1)

        mod_p1[-i-1] = t1
        mod_n1[-i-1] = t2

        base1 *= 2
        base2 *= 2

        base1%=(st+1)
        base2%=(st-1)

    p1 = 0
    n1 = 0
    for i in range(n):
        if s[i] == "1":
            p1 += mod_p1[i]
            n1 += mod_n1[i]

    for i in range(n):
        if s[i] == "0":
            base = p1
            base += mod_p1[i]
            base %= st+1
            print(1+g(base))

        else:
            base = n1
            base -= mod_n1[i]
            base %= st-1
            print(1+g(base))

else:
    mod_p1 = [0 for i in range(n)]
    base1 = 1
    for i in range(n):
        t1 = base1 % (st + 1)

        mod_p1[-i - 1] = t1

        base1 *= 2

        base1 %= (st + 1)

    p1 = 0
    for i in range(n):
        if s[i] == "1":
            p1 += mod_p1[i]

    for i in range(n):
        if s[i] == "0":
            base = p1
            base += mod_p1[i]
            base %= st + 1
            print(1 + g(base))

        else:
            print(0)

Buggy submission (verdict: Runtime Error):
def popcount(x):
    '''xの立っているビット数をカウントする関数
    (xは64bit整数)'''

    # 2bitごとの組に分け、立っているビット数を2bitで表現する
    x = x - ((x >> 1) & 0x5555555555555555)

    # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

    x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
    x = x + (x >> 8) # 16bitごと
    x = x + (x >> 16) # 32bitごと
    x = x + (x >> 32) # 64bitごと = 全部の合計
    return x & 0x0000007f

def g(num_in):
    num = num_in
    cnt = 0
    if num == 0:
        return 0

    while True:
        base = popcount(num)
        if num%base == 0:
            cnt += 1
            break
        else:
            num = num%base
            cnt += 1
    return cnt


n = int(input())
s = input()

st = 0
for c in s:
    if c == "1":
        st += 1

mod_p1 = [0 for i in range(n)]
mod_n1 = [0 for i in range(n)]
base = 1
for i in range(n):
    t1 = base%(st+1)
    t2 = base%(st-1)

    mod_p1[-i-1] = t1
    mod_n1[-i-1] = t2

    base *= 2

p1 = 0
n1 = 0
for i in range(n):
    if s[i] == "1":
        p1 += mod_p1[i]
        n1 += mod_n1[i]

for i in range(n):
    if s[i] == "0":
        base = p1
        base += mod_p1[i]
        base %= st+1
        print(1+g(base))

    else:
        base = n1
        base -= mod_n1[i]
        base %= st-1
        print(1+g(base))