Problem p02609 (python)

Fixed submission:
#

import sys
input=sys.stdin.readline

def main():
    N=int(input())
    X=input().strip("\n")
    cnt=[0]*N
    for i in range(1,N):
        n=i
        while(n>0):
            pc=0
            for j in range(19):
                if (n>>j)&1==1:
                    pc+=1
            n%=pc
            cnt[i]+=1
    pc=X.count("1")
    if pc==0:
        for i in range(N):
            print(1)
        exit()

    rp=[0]*N
    rn=[0]*N
    for i in range(N):
        rp[i]=pow(2,i,pc+1)
        if pc!=1:
            rn[i]=pow(2,i,pc-1)
    rp=rp[::-1]
    rn=rn[::-1]
            
    Xrp=0
    Xrn=0
    for i in range(N):
        if X[i]=="1":
            Xrp=(Xrp+rp[i])%(pc+1)
            if pc!=1:
                Xrn=(Xrn+rn[i])%(pc-1)
            
    for i in range(N):
        if X[i]=="1":
            if pc==1:
                print(0)
                continue
            r=Xrn-rn[i]
            print(cnt[r%(pc-1)]+1)
        else:
            r=Xrp+rp[i]
            print(cnt[r%(pc+1)]+1)
    
    
    
if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
#

import sys
input=sys.stdin.readline

def main():
    N=int(input())
    X=input().strip("\n")
    cnt=[0]*N
    for i in range(1,N):
        n=i
        while(n>0):
            pc=0
            for j in range(19):
                if (n>>j)&1==1:
                    pc+=1
            n%=pc
            cnt[i]+=1
    pc=X.count("1")
    if pc==0:
        for i in range(N):
            print(1)
        exit()

    rp=[0]*N
    rn=[0]*N
    for i in range(N):
        rp[i]=pow(2,i,pc+1)
        if pc!=1:
            rn[i]=pow(2,i,pc-1)
    rp=rp[::-1]
    rn=rn[::-1]
            
    Xrp=0
    Xrn=0
    for i in range(N):
        if X[i]=="1":
            Xrp=(Xrp+rp[i])%(pc+1)
            Xrn=(Xrn+rn[i])%(pc-1)
            
    for i in range(N):
        if X[i]=="1":
            if pc==1:
                print(0)
                continue
            r=Xrn-rn[i]
            print(cnt[r%(pc-1)]+1)
        else:
            r=Xrp+rp[i]
            print(cnt[r%(pc+1)]+1)
    
    
    
if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
#

import sys
input=sys.stdin.readline

def main():
    N=int(input())
    X=input().strip("\n")[::-1]
    cnt=[0]*N
    for i in range(1,N):
        n=i
        while(n>0):
            pc=0
            for j in range(19):
                if (n>>j)&1==1:
                    pc+=1
            n%=pc
            cnt[i]+=1
    pc=X.count("1")
    t=0
    c=[0]*N
    for i in range(N):
        if X[i]=="1":
            t+=2**i
            c[i]=1
    X=t
    ans=[0]*N
    for i in range(N):
        if c[i]==1:
            x=X-pow(2,i)
            pci=pc-1
            x-=(x//pci)*pci
            ans[i]=cnt[x%pci]+1
        else:
            x=X+pow(2,i)
            pci=pc+1
            x-=(x//pci)*pci
            ans[i]=cnt[x%pci]+1
    ans=ans[::-1]
    for i in range(N):
        print(ans[i])
    
    
    
if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
#

import sys
input=sys.stdin.readline

def main():
    N=int(input())
    X=input().strip("\n")[::-1]
    cnt=[0]*N
    for i in range(1,N):
        n=i
        while(n>0):
            pc=0
            for j in range(22):
                if (n>>j)&1==1:
                    pc+=1
            n%=pc
            cnt[i]+=1
    pc=X.count("1")
    t=0
    for i in range(N):
        if X[i]=="1":
            t+=2**i
    X=t
    for i in range(N-1,-1,-1):
        if (X>>i)&1==1:
            x=X-2**i
            pci=pc-1
            print(cnt[x%pci]+1)
        else:
            x=X+2**i
            pci=pc+1
            print(cnt[x%pci]+1)
        
    
    
    
if __name__=="__main__":
    main()