Problem p02609 (python)

Fixed submission:
## 最初のmodをどうにか高速にやりたい → modの性質を使う
## xiのpopcount（1の数）はxのpopcount+1または-1
n=int(input())
x_str=input()
x_popcount=x_str.count('1')
dp=[0]*(x_popcount+1)
for i in range(1,x_popcount+1):
    popcount=format(i,'b').count('1')
    dp[i]=dp[i%popcount]+1
# print(dp)
# x=int(x_str,2)
if x_popcount>1:
    pows_minus=[pow(2,i,(x_popcount-1)) for i in range(n)]
    # x_minus=x%(x_popcount-1)
    x_minus=0
    for i in range(n):
        if x_str[i]=='1':
            x_minus+=pows_minus[n-1-i]
pows_plus=[pow(2,i,(x_popcount+1)) for i in range(n)]
# x_plus=x%(x_popcount+1)
x_plus=0
for i in range(n):
    if x_str[i]=='1':
        x_plus+=pows_plus[n-1-i]

for i in range(len(x_str)):
    ## xi=xi mod popcount(x)+-1
    xi=-1
    if x_str[i]=='1':
        if x_popcount>1: ## else: xi=-1
            xi=(x_minus-pows_minus[n-1-i])%(x_popcount-1)
    else:
        xi=(x_plus+pows_plus[n-1-i])%(x_popcount+1)
    if xi==-1:
        print(0)
    else:
        print(dp[xi]+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x_str=input()
x=int(x_str,2)
x_popcount=x_str.count('1')
dp=[0]*(x_popcount+1)
for i in range(1,x_popcount+1):
    popcount=format(i,'b').count('1')
    dp[i]=dp[i%popcount]+1
# print(dp)
x_minus=x%(x_popcount-1)
x_plus=x%(x_popcount+1)
pows_minus=[(2**i)%(x_popcount-1) for i in range(n)]
pows_plus=[(2**i)%(x_popcount+1) for i in range(n)]

for i in range(len(x_str)):
    ## xi=xi mod popcount(x)+-1
    xi=0
    if x_str[i]=='1':
        xi=(x_minus-pows_minus[n-1-i])%(x_popcount-1)
    else:
        xi=(x_plus+pows_plus[n-1-i])%(x_popcount+1)
    print(dp[xi]+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
x_popcount=x.count('1')
f=[0]*(x_popcount+1)
for i in range(1,x_popcount+1):
    popcount=format(i,'b').count('1')
    f[i]=f[i%popcount]+1
# print(f)
for i in range(len(x)):
    if x[i]=='1':
        x_changed=x[:i]+'0'+x[i+1:]
        popcount=x_popcount-1
    else:
        x_changed=x[:i]+'1'+x[i+1:]
        popcount=x_popcount+1
    num_changed=int(x_changed,2)
    # print(x_changed,num_changed)
    if num_changed<popcount:
        print(f[num_changed])
    else:
        print(f[num_changed%popcount]+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
x_pop=format(x,'b').count('1')
f=[0]*(x_pop+1)
for i in range(1,x_pop+1):
    popcount=format(i,'b').count('1')
    f[i]=f[i%popcount]+1
# print(f)
for i in range(len(x)):
  popcount=x_pop
    if x[i]=='1':
        x_changed=x[:i]+'0'+x[i+1:]
        popcount-=1
    else:
        x_changed=x[:i]+'1'+x[i+1:]
        popcount+=1
    num_changed=int(x_changed,2)
    # print(x_changed,num_changed)
    if num_changed<popcount:
        print(f[num_changed])
    else:
        print(f[num_changed%popcount]+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
x_pop=format(x,'b').count('1')
f=[0]*(x_pop+1)
for i in range(1,x_pop+1):
    popcount=format(i,'b').count('1')
    f[i]=f[i%popcount]+1
# print(f)
for i in range(len(x)):
  popcount=x_pop
    if x[i]=='1':
        x_changed=x[:i]+'0'+x[i+1:]
        popcount-=1
    else:
        x_changed=x[:i]+'1'+x[i+1:]
        popcount+=1
    num_changed=int(x_changed,2)
    # print(x_changed,num_changed)
    if num_changed<n:
        print(f[num_changed])
    else:
        print(f[num_changed%popcount]+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
x_pop=format(x,'b').count('1')
f=[0]*(x_pop+1)
for i in range(1,x_pop+1):
    popcount=format(i,'b').count('1')
    f[i]=f[i%popcount]+1
# print(f)
for i in range(len(x)):
    if x[i]=='1':
        x_changed=x[:i]+'0'+x[i+1:]
        popcount=x_pop-1
    else:
        x_changed=x[:i]+'1'+x[i+1:]
        popcount=x_pop+1
    num_changed=int(x_changed,2)
    # print(x_changed,num_changed)
    if num_changed<n:
        print(f[num_changed])
    else:
        print(f[num_changed%popcount]+1)