qyliang/CodeNanoFix huggingface dataset:
Buggy code:
def popcnt_int(n): # n:int
    return bin(n).count('1')

def popcnt_bin(s): # s:str
    return s.count('1')

def poptimes(n, cnt=1): #:int
    if n==0:
        return cnt
    else:
        n %= popcnt_int(n)
        cnt += 1
        return poptimes(n, cnt)

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

s_int = int(s,2)
pc = popcnt_bin(s)
pc_plus = pc+1
pc_minus = pc-1
#print(n,s,s_int,pc,pc_plus,pc_minus)

l_plus = [2**(n-i-1) % (pc+1) for i in range(n)]
l_minus = [2**(n-i-1) % (pc-1) for i in range(n)]
#print(l_plus)
#print(l_minus)

for i in range(n):
    if s[i] == '1':
        if pc != 1:
            tmp = (s_int % pc_minus) - (l_minus[i] % pc_minus)
            print(poptimes(tmp))
        else:
            print(0)
            continue
    
    else:
        tmp = (s_int % pc_plus) + (l_plus[i] % pc_plus)
        print(poptimes(tmp))


Fixed code:
def popcnt_int(n): # n:int
    return bin(n).count('1')

def popcnt_bin(s): # s:str
    return s.count('1')

def poptimes(n, cnt=1): #:int
    if n==0:
        return cnt
    else:
        n %= popcnt_int(n)
        cnt += 1
        return poptimes(n, cnt)

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

s_int = int(s,2)
pc = popcnt_bin(s)
pc_plus = s_int % (pc+1)
if  pc != 1:
    pc_minus = s_int % (pc-1)
#print(n,s,s_int,pc,pc_plus,pc_minus)

for i in range(n):
    if s[i] == '1':
        if pc != 1:
            tmp = pc_minus - pow(2, n-i-1, pc-1)
            print(poptimes(tmp % (pc-1)))
        else:
            print(0)
            continue
    
    else:
        tmp = pc_plus + pow(2, n-i-1, pc+1)
        print(poptimes(tmp % (pc+1)))
Buggy code:
def popcount(x):
    return bin(x)[2:].count("1")

def f(x):
    cnt=0
    while(x>0):
        x%=popcount(x)
        cnt+=1
    return cnt

n=int(input())
s=input()
d=s.count("1")
if d==0:
    for i in range(n):
        print(1)
elif d==1:
    if s[-1]=="1":
        for i in range(n-1):
            print(2)
        else:
            print(0)
    else:
        for i in range(n-1):
            if s[i]=="1":
                print(0)
            else:
                print(1)
        print(2)
else:
    ints=int(s,2)
    m=ints%(d-1)
    M=ints%(d+1)
    for i in range(n):
        if s[i]=="1":
            print(f((m-2**i)%(d-1))+1)
        else:
            print(f((M+2**i)%(d+1))+1)

Fixed code:
def popcount(x):
    return bin(x)[2:].count("1")

def f(x):
    cnt=0
    while(x>0):
        x%=popcount(x)
        cnt+=1
    return cnt

n=int(input())
s=input()
d=s.count("1")
if d==0:
    for i in range(n):
        print(1)
elif d==1:
    if s[-1]=="1":
        for i in range(n-1):
            print(2)
        else:
            print(0)
    else:
        for i in range(n-1):
            if s[i]=="1":
                print(0)
            else:
                print(1)
        print(2)
else:
    ints=int(s,2)
    m=ints%(d-1)
    M=ints%(d+1)
    s=s[::-1]
    toaddm=-1
    toaddM=1
    res=[None for i in range(n)]
    for i in range(n):
        if s[i]=="1":
            res[i]=f((m+toaddm)%(d-1))+1
        else:
            res[i]=f((M+toaddM)%(d+1))+1
        toaddm*=2
        toaddm%=d-1
        toaddM*=2
        toaddM%=d+1
    for i in res[::-1]:
        print(i)
Buggy code:
n=int(input())

s=input()
X=int(s,2)

def popcount(x):
    return bin(x).count("1")

one_cnt=popcount(X)

modp=X%(one_cnt+1)
modm=X%(one_cnt-1)

for i in range(n):
    if s[i]==1:
        num=(modm-pow(2,n-i-1,one_cnt-1))%(one_cnt-1)
    else:
        num=(modp+pow(2,n-i-1,one_cnt+1))%(one_cnt+1)

    if num==0:
        print(1)
    else:
        cnt=1
        while 1:
            num=num%popcount(num)
            cnt+=1
            if num==0:
                print(cnt)
                break












Fixed code:
def popcount(x):
    return bin(x).count("1")

n=int(input())
s=input()
X=int(s,2)

one_cnt=popcount(X)

modp=X%(one_cnt+1)
modm=X%(one_cnt-1) if one_cnt-1!=0 else 0

for i in range(n):
    if s[i]=="1":
        if one_cnt-1==0:
            print(0)
            continue
        else:
            num=(modm-pow(2,n-i-1,one_cnt-1))%(one_cnt-1)
    else:
        num=(modp+pow(2,n-i-1,one_cnt+1))%(one_cnt+1)

    #print(i,"num",num)
    cnt=1
    while num!=0:
        num=num%popcount(num)
        cnt+=1
    print(cnt)
Buggy code:
def popcount(x):
    return bin(x).count("1")


n=int(input())
x=input()
num=int(x,2)
cnt=popcount(num)

A=[]
for i in range(n):
    if x[i]=="1" and cnt==1:A.append(-1)
    elif x[i]=="1":
        num %=cnt-1
        A.append((num-pow(2,n-i-1,cnt-1))%(cnt-1))
    else:
        num %=cnt+1
        A.append((num+pow(2,n-i-1,cnt+1))%(cnt+1))

B=[]
for i in A:
    if i==-1:B.append(0)
    else:
        point=1
        while i!=0:
            i %=popcount(i)
            point +=1
        B.append(point)

for i in B:
    print(i)

Fixed code:
def popcount(x):
    return bin(x).count("1")


n=int(input())
x=input()
num=int(x,2)
cnt=popcount(num)

A=[]
for i in range(n):
    number=num
    if x[i]=="1" and cnt==1:A.append(-1)
    elif x[i]=="1":
        number %=cnt-1
        A.append((number-pow(2,n-i-1,cnt-1))%(cnt-1))
    else:
        number %=cnt+1
        A.append((number+pow(2,n-i-1,cnt+1))%(cnt+1))

for i in A:
    ans=i
    if ans==-1:print(0)
    else:
        point=1
        while ans!=0:
            ans %=popcount(ans)
            point +=1
        print(point)
Buggy code:
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

pc_memo = defaultdict(int)
memo = defaultdict(int)


def to_next(x):
    if x not in pc_memo.keys():
        pc_memo[x] = x % "{:b}".format(x).count('1')
    return pc_memo[x]


def f(x):
    if x == 0:
        return 0
    if x not in memo.keys():
        memo[x] = f(to_next(x))+1
    return memo[x]


for i in range(2*10**5):
    f(i)

n = int(input())
x = input()
one_cnt = x.count('1')

mod_minus = 0
mod_plus = 0
for i, a in enumerate(reversed(x)):
    if a == '1':
        mod_minus += pow(2, i, one_cnt-1)
        mod_minus %= one_cnt-1
        mod_plus += pow(2, i, one_cnt+1)
        mod_plus %= one_cnt+1


for i in range(n):
    if x[i] == '0':
        nx = mod_plus+pow(2, n-i-1, one_cnt+1)
        nx %= one_cnt+1
        print(f(nx)+1)
    else:
        nx = mod_minus-pow(2, n-i-1, one_cnt-1)
        nx %= one_cnt-1
        print(f(nx)+1)


Fixed code:
from collections import defaultdict
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)

pc_memo = defaultdict(int)
memo = defaultdict(int)


def to_next(x):
    if x not in pc_memo.keys():
        pc_memo[x] = x % "{:b}".format(x).count('1')
    return pc_memo[x]


def f(x):
    if x == 0:
        return 0
    if x not in memo.keys():
        memo[x] = f(to_next(x))+1
    return memo[x]


for i in range(2*10**5):
    f(i)

n = int(input())
x = input().rstrip()
one_cnt = x.count('1')

mod_minus = 0
mod_plus = 0
for i, a in enumerate(reversed(x)):
    if a == '1':
        if 0 < one_cnt-1:
            mod_minus += pow(2, i, one_cnt-1)
            mod_minus %= one_cnt-1
        mod_plus += pow(2, i, one_cnt+1)
        mod_plus %= one_cnt+1


for i in range(n):
    if x[i] == '0':
        nx = mod_plus+pow(2, n-i-1, one_cnt+1)
        nx %= one_cnt+1
        print(f(nx)+1)
    else:
        if 0 < one_cnt-1:
            nx = mod_minus-pow(2, n-i-1, one_cnt-1)
            nx %= one_cnt-1
            print(f(nx)+1)
        else:
            print(0)
Buggy code:
def modpow(a, n, p):
  if n == 0:
    return 1
  if n == 1:
    return a % p 
  elif n % 2 == 1:
    return a * modpow(a, n-1, p) % p
  else:
    return modpow(a, n//2, p) **2 % p

l = [-1]*(2*10**5+1)
l[0] = 0
def f(n):
  if l[n] != -1:
    return l[n]
  else:
    b = bin(n)
    k = b.count("1")
    l[n] = f(n%k)+1
    return l[n]

 
  
  
n = int(input())
x = input()
#x = "1000101010"*20000
k = x.count("1")
x_mod_1 = int(x, 2) % (k+1)
x_mod_0 = int(x, 2) % (k-1) if k > 1 else 0
for i in range(n):
  if x[i] == "0":
    print(f(x_mod_1 + modpow(2, n-1-i, k+1))+1)

  else:
    if k > 1:
      print(f(x_mod_0 - modpow(2, n-1-i, k-1))+1)
    else:
      print(0)
    

Fixed code:
def modpow(a, n, p):
  if n == 0:
    return 1
  if n == 1:
    return a % p 
  elif n % 2 == 1:
    return a * modpow(a, n-1, p) % p
  else:
    return modpow(a, n//2, p) **2 % p

l = [-1]*(2*10**5+1)
l[0] = 0
def f(n):
  if l[n] != -1:
    return l[n]
  else:
    b = bin(n)
    k = b.count("1")
    l[n] = f(n%k)+1
    return l[n]

 
  
  
n = int(input())
x = input()
#x = "1000101010"*20000
k = x.count("1")
x_mod_1 = int(x, 2) % (k+1)
x_mod_0 = int(x, 2) % (k-1) if k > 2 else 0
for i in range(n):
  if x[i] == "0":
    print(f((x_mod_1 + modpow(2, n-1-i, k+1))%(k+1))+1)

  else:
    if k > 2:
      print(f((x_mod_0 - modpow(2, n-1-i, k-1))%(k-1))+1)
    elif k > 1:
      print(1)
    else:
      print(0)
Buggy code:
N = int(input())
x = input()
#print(int(x,2))

def popcount(X,ones,count): 
 
    if ones==0:
        return count
    else:
      
        X=X%ones
        
        count+=1
 
        ones=str(bin(X)).count("1")
        
        return popcount(X,ones,count)
 
GroundOnes=x.count("1")
GroundX=int(x,2)
 
CaseOne= GroundX%(GroundOnes-1)
CaseZero= GroundX%(GroundOnes+1)

TwoPowerCaseOne=[]
TwoPowerCaseZero=[]

for j in range(N):
  TwoPowerCaseOne.append(2**(N-j-1) % (GroundOnes-1))
  TwoPowerCaseZero.append(2**(N-j-1) % (GroundOnes+1))
  
for i in range(N):
    
    #CaseOne
    if x[i]=="1":
        ones=GroundOnes-1
        X=CaseOne+TwoPowerCaseOne[i]
        
    #CaseZero
    else:
        ones=GroundOnes+1
        X=CaseZero+TwoPowerCaseZero[i]
            
 
    # print("for loop",i)
    # print("ones",ones)
    # print("new2bit",new2bit)
    # print(" ------ return --- ",popcount(X,ones,0))
    print(popcount(X,ones,0))

Fixed code:
N=int(input())
x=input()
def popcount(X,ones,count): 
    #print(count,bin(X),X,ones)
    if ones==0:
        return count
    else:
      
        X=X%ones
        
        count+=1
 
        ones=str(bin(X)).count("1")
        
        return popcount(X,ones,count)
 

    
GroundOnes=x.count("1")
GroundX=int(x,2)

if GroundOnes>1:
    CaseOne= GroundX%(GroundOnes-1)
else:
    CaseOne=2

CaseZero= GroundX%(GroundOnes+1)

TwoPowerCaseOne=[]
TwoPowerCaseZero=[]

# if GroundOnes>1:
#     for j in range(N):
#       TwoPowerCaseOne.append(pow(2,(N-j-1) , (GroundOnes-1)))
#       TwoPowerCaseZero.append(pow(2**(N-j-1), (GroundOnes+1)))
# else:
#     for j in range(N):
#       TwoPowerCaseZero.append(pow(2**(N-j-1), (GroundOnes+1)))
  
    
for i in range(N):
    
    #CaseOne
    if x[i]=="1":
        ones=GroundOnes-1
        if ones>0:
            X=CaseOne-pow(2,N-i-1,ones)
        else:
            X=0
    #CaseZero
    else:
        ones=GroundOnes+1
        X=CaseZero+pow(2,N-i-1,ones)
            
 
    # print("for loop",i)
    # print("ones",ones)
    # print("new2bit",new2bit)
    #print(" ------ return --- ",popcount(X,ones,0))
    print(popcount(X,ones,0))
Buggy code:
import math
# from collections import defaultdict
import time
def steps(n,p):
  c = 0
  while n:
    c += 1
    n = n%p
    p = bin(n).count('1')
  return c

n = 200000
x = "1"*n
# n = int(input())
# x = input()
incs = [1]
decs = [1]
p = x.count('1')
for i in range(1,n):
  incs.append((incs[-1]*2)%(p+1))
  decs.append((decs[-1]*2)%(p-1))
num = int(x,2)
inc = num%(p+1)
dec = num%(p-1)
start = time.time()
k = -1
for i in range(n):
  cp = p
  if x[i] == '0':
    cp += 1
    new = (inc+incs[k])%cp
  else:
    cp -= 1
    new = (dec-decs[k])%cp
  k -= 1
  ans = steps(new,cp)
  if ans==0:
    ans=1
  print(ans)
  # steps(new,cp)
#print(time.time()-start)

Fixed code:
import math
# from collections import defaultdict
import time
def steps(n,p):
  c = 0
  while n:
    c += 1
    n = n%p
    p = bin(n).count('1')
  return c

# n = 200000
# x = "1"*n
n = int(input())
x = input()
p = x.count('1')
ans = []
num = int(x,2)
if p==1:
  for i in range(n-1,-1,-1):
    if x[i]=='1':
      ans.append(0)
    else:
      if i==n-1 or x[n-1]=='1':
        ans.append(2)
      else:
        ans.append(1)
else:
  incs = 1
  decs = 1
  inc = num%(p+1)
  dec = num%(p-1)
  start = time.time()
  k = -1
  for i in range(n-1,-1,-1):
    cp = p
    if x[i] == '0':
      cp += 1
      new = (inc+incs)%cp
    else:
      cp -= 1
      new = (dec-decs)%cp
    k -= 1
    incs = (incs*2)%(p+1)
    decs = (decs*2)%(p-1)
    sol = steps(new,cp)
    if sol==0:
      sol=1
    ans.append(sol)
    # steps(new,cp)
  # print(time.time()-start)
ans = ans[::-1]
for x in ans:
  print(x)
Buggy code:
n, X = open(0).read().split()
n = int(n)
x = int(X, 2)
X = list(map(int, X))

def popcount(x):
    return bin(n).count('1')

ma = 2 * 10**5 + 1
dp = [0] * ma
for i in range(1, ma):
    dp[i] = dp[i % popcount(i)] + 1

c = popcount(x)
a = x % (c+1)
if c == 1:
    for i in range(n):
        if X[i]:
            print(0)
        else:
            print(dp[(a + pow(2, n-i-1, c+1)) % (c+1)] + 1)
else:
    b = x % (c-1)
    for i in range(n):
        if X[i]:
            print(dp[(b - pow(2, n-i-1, c-1)) % (c-1)] + 1)
        else:
            print(dp[(a + pow(2, n-i-1, c+1)) % (c+1)] + 1)

Fixed code:
n, X = open(0).read().split()
n = int(n)
x = int(X, 2)
X = list(map(int, X))

def popcount(n):
    return bin(n).count('1')

ma = 2 * 10**5 + 1
dp = [0] * ma
for i in range(1, ma):
    dp[i] = dp[i % popcount(i)] + 1

c = popcount(x)
a = x % (c+1)
if c == 1:
    for i in range(n):
        if X[i]:
            print(0)
        else:
            print(dp[(a + pow(2, n-i-1, c+1)) % (c+1)] + 1)
else:
    b = x % (c-1)
    for i in range(n):
        if X[i]:
            print(dp[(b - pow(2, n-i-1, c-1)) % (c-1)] + 1)
        else:
            print(dp[(a + pow(2, n-i-1, c+1)) % (c+1)] + 1)
Buggy code:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    x = input()
    k = int(x,2)
    c = x.count('1')
    kp = k%(c+1)
    if c > 1:
        kn = k%(c-1)
    
    for i in range(n):
        cnt = 1
        if x[i] == '1':
            if c == 1:
                print(0)
                continue
            mod = c-1
            tmp = (kn - pow(2,n-i-1,mod))%mod
            while 1:
                if tmp == 0:
                    print(cnt)
                    break
                tmp = tmp%format(tmp, 'b').count('1')
                cnt+=1
        else:
            mod = c+1
            tmp = (kn + pow(2,n-i-1,mod))%mod
            while 1:
                if tmp == 0:
                    print(cnt)
                    break
                tmp = tmp%format(tmp, 'b').count('1')
                cnt+=1
            


if __name__ == '__main__':
    main()

Fixed code:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    x = input()
    k = int(x,2)
    c = x.count('1')
    kp = k%(c+1)
    if c > 1:
        kn = k%(c-1)
    
    for i in range(n):
        cnt = 1
        if x[i] == '1':
            if c == 1:
                print(0)
                continue
            mod = c-1
            tmp = (kn - pow(2,n-i-1,mod))%mod
            while 1:
                if tmp == 0:
                    break
                tmp = tmp%format(tmp, 'b').count('1')
                cnt+=1
        else:
            mod = c+1
            tmp = (kp + pow(2,n-i-1,mod))%mod
            while 1:
                if tmp == 0:
                    break
                tmp = tmp%format(tmp, 'b').count('1')
                cnt+=1
        print(cnt)


if __name__ == '__main__':
    main()
Buggy code:
n = int(input())
x = input()

x_1 = x.count('1')
flag = 0
if(x_1 == 0):
    flag = 1
if(x_1 == 1):
    flag = 2

data = [0 for i in range(n+1)]
data[0] = 0
for i in range(1,n+1):
    i_str = format(i,'b')
    ne = i % (i_str.count('1'))
    data[i] = data[ne] + 1

print(data)

up = [0 for i in range(n)]
do = [0 for i in range(n)]

d = 1
for i in range(n):
    up[i] = d % (x_1 + 1)
    d *= 2
d = 1
if(flag == 0):
    for i in range(n):
        do[i] = d % (x_1 - 1)
        d *= 2

upp = 0
dow = 0

for i in range(n):
    if(x[n-i-1] == '1'):
        upp += up[i]
        upp %= (x_1+1)
        dow += do[i]
        dow %= (x_1-1)



for i in range(n):
    if(x[i] == '0'):
        sam = upp + up[n-i-1]
        sam %= (x_1+1)
        print(data[sam]+1)
    elif(flag == 0):
        sam = dow - do[n-i-1]
        sam %= (x_1-1)
        print(data[sam]+1)
    elif(flag == 2):
        print('0')

Fixed code:
n = int(input())
x = input()

x_1 = x.count('1')
flag = 0
if(x_1 == 0):
    flag = 1
if(x_1 == 1):
    flag = 2

data = [0 for i in range(n+1)]
data[0] = 0
for i in range(1,n+1):
    i_str = format(i,'b')
    ne = i % (i_str.count('1'))
    data[i] = data[ne] + 1

up = [0 for i in range(n)]
do = [0 for i in range(n)]

d = 1
upp = 0
for i in range(n):
    up[i] = d
    if(x[n-i-1] == '1'):
        upp += d
        upp %= (x_1+1)
    d *= 2
    d %= (x_1 + 1)
d = 1
dow = 0
if(flag == 0):
    for i in range(n):
        do[i] = d
        if(x[n-i-1] == '1'):
            dow += d
            dow %= (x_1-1)
        d *= 2
        d %= (x_1 - 1)


for i in range(n):
    if(x[i] == '0'):
        sam = upp + up[n-i-1]
        sam %= (x_1+1)
        print(data[sam]+1)
    elif(flag == 0):
        sam = dow - do[n-i-1]
        sam %= (x_1-1)
        print(data[sam]+1)
    elif(flag == 2):
        print('0')
Buggy code:
n = int(input())
x = input()
flg = [0] * n
for i in range(n):
    if x[i] == '1':
        flg[i] = 1
x = int(x, 2)
cnt = sum(flg)
x_a = x % (cnt - 1)
x_b = x % (cnt + 1)


for i in range(n):
    if flg[i] == 1:
        t = (x_a - pow(2, n-i-1, cnt-1)) % (cnt - 1)
    else:
        t = (x_b - pow(2, n-i-1, cnt+1)) % (cnt + 1)

    if t == 0:
        print(1)
    else:
        tmp = 1
        while t > 0:
            pop = bin(t).count('1')
            t = t % pop
            tmp += 1

        print(tmp)


Fixed code:
n = int(input())
x = input()
flg = [0] * n
for i in range(n):
    if x[i] == '1':
        flg[i] = 1
x = int(x, 2)
cnt = sum(flg)
if cnt == 1:
    x_a = 0
else:
    x_a = x % (cnt - 1)
x_b = x % (cnt + 1)


for i in range(n):
    if flg[i] == 1:
        if cnt == 1:
            print(0)
            continue
        t = (x_a - pow(2, n-i-1, cnt-1)) % (cnt - 1)
    else:
        t = (x_b + pow(2, n-i-1, cnt+1)) % (cnt + 1)

    if t == 0:
        print(1)
    else:
        tmp = 1
        while t > 0:
            pop = bin(t).count('1')
            t = t % pop
            tmp += 1

        print(tmp)
Buggy code:
def solve():
  N = int(input())
  X = input()
  bl = X.count('1')
  bit = 0
  for i in range(N):
    bit += int(X[i])*pow(2,N-1-i)
    bit %= bl
  ans = [0]*N
  dp = [0]*(N+1)
  for i in range(1,N+1):
    dp[i] = dp[i%(bin(i).count('1'))]+1
  for i in range(N):
    if X[i]=='1':
      bit1 = bit - pow(2,N-1-i)
      bl1 = bl-1
      if bl1==0:
        ans[i] = 0
        continue
    else:
      bit1 = bit + pow(2,N-1-i)
      bl1 = bl+1
    ans[i] = dp[bit1%bl1]+1
  return ans
print(*solve(),sep='\n')

Fixed code:
def solve():
  N = int(input())
  X = input()
  bl = X.count('1')
  bitp = 0
  for i in range(N):
    bitp += int(X[i])*pow(2,N-1-i,bl+1)
    bitp %= bl+1
  bitm = 0
  if bl>1:
    for i in range(N):
      bitm += int(X[i])*pow(2,N-1-i,bl-1)
      bitm %= bl-1
  
  ans = [0]*N
  dp = [0]*(N+1)
  for i in range(1,N+1):
    dp[i] = dp[i%(bin(i).count('1'))]+1
  for i in range(N):
    if X[i]=='1':
      bl1 = bl-1
      if bl1==0:
        ans[i] = 0
        continue
      bit1 = bitm - pow(2,N-1-i,bl1)
    else:
      bl1 = bl+1
      bit1 = bitp + pow(2,N-1-i,bl1)
    ans[i] = dp[bit1%bl1]+1
  return ans
print(*solve(),sep='\n')
Buggy code:
# import sys
# input = sys.stdin.readline

def mp(): return map(int, input().split())
def lmp(): return list(map(int, input().split()))

n = int(input())
x = list(input())
xc = x.count("1")
xu = []
xd = []
if xc == 0:
    for i in range(n):
        print(0)
        exit()
t = 1
for i in range(n):
    mod = xc+1
    xu.append(t%mod)
    t *= 2
t = 1
for i in range(n):
    mod = xc-1
    if mod == 0:
        xd.append(0)
    else:
        xd.append(t%mod)
        t *= 2
xu.reverse()
xd.reverse()
s_xu = 0
s_xd = 0
for i in range(n):
    if x[i] == "1":
        s_xu += xu[i]
        s_xd += xd[i]

# print(xu)
# print(xd)

for i in range(n):
    cnt = 1
    if x[i] == "0":
        tar = (s_xu+xu[i])%(xc+1)
        #print(tar)
        while tar != 0:
            b_tar = list(bin(tar)[2:])
            tar %= b_tar.count("1")
            cnt += 1
        print(cnt)
    else:
        if s_xd-xd[i] == 0:
            print(0)
        else:
            tar = (s_xd-xd[i])%(xc-1)
            #print(tar)
            while tar != 0:
                b_tar = list(bin(tar)[2:])
                tar %= b_tar.count("1")
                cnt += 1
            print(cnt)







Fixed code:
# import sys
# input = sys.stdin.readline

def mp(): return map(int, input().split())
def lmp(): return list(map(int, input().split()))

n = int(input())
x = list(input())
xc = x.count("1")
xu = []
xd = []
if xc == 0:
    for i in range(n):
        print(1)
        exit()
t = 1
for i in range(n):
    mod = xc+1
    xu.append(t%mod)
    t *= 2
    t %= mod
t = 1
flag = False
for i in range(n):
    mod = xc-1
    if mod == 0:
        xd.append(0)
        flag = True
    else:
        xd.append(t%mod)
        t *= 2
        t %= mod
xu.reverse()
xd.reverse()
s_xu = 0
s_xd = 0
for i in range(n):
    if x[i] == "1":
        s_xu += xu[i]
        s_xd += xd[i]

# print(xu)
# print(xd)

for i in range(n):
    cnt = 1
    if x[i] == "0":
        tar = (s_xu+xu[i])%(xc+1)
        #print(tar)
        while tar != 0:
            b_tar = bin(tar)
            tar %= b_tar.count("1")
            cnt += 1
        print(cnt)
    else:
        if flag:
            print(0)
        else:
            tar = (s_xd-xd[i])%(xc-1)
            #print(tar)
            while tar != 0:
                b_tar = bin(tar)
                tar %= b_tar.count("1")
                cnt += 1
            print(cnt)
Buggy code:
def solve():
  N = int(input())
  X = input()
  x = sum(map(int,list(X)))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = dp[i%sum(map(int,list(bin(i)[2:])))]+1
  cnt = 0
  if x==1:
    cnt1 = 0
  elif x>1:
    for i in range(N):
      cnt1 += int(X[-i-1])*pow(2,i,x-1)
      cnt1 %= x-1
  cnt2 = 0
  for i in range(N):
    cnt2 += int(X[-i-1])*pow(2,i,x+1)
    cnt2 %= x+1
  
  ans = [0]*N
  for i in range(N):
    if X[i]=='1':
      if x==1:
        ans[i] = 0
      else:
        num = (cnt1-pow(2,N-i-1,x-1))%(x-1)
        ans[i] = dp[num]+1
    else:
      num = (cnt2+pow(2,N-i-1,x+1))%(x+1)
      ans[i] = dp[num]+1
  return ans
print(*solve(),sep='\n')

Fixed code:
def solve():
  N = int(input())
  X = input()
  x = sum(map(int,list(X)))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = dp[i%sum(map(int,list(bin(i)[2:])))]+1
  cnt1 = 0
  if x>1:
    for i in range(N):
      cnt1 += int(X[-i-1])*pow(2,i,x-1)
      cnt1 %= x-1
  cnt2 = 0
  for i in range(N):
    cnt2 += int(X[-i-1])*pow(2,i,x+1)
    cnt2 %= x+1
  
  ans = [0]*N
  for i in range(N):
    if X[i]=='1':
      if x==1:
        ans[i] = 0
      else:
        num = (cnt1-pow(2,N-i-1,x-1))%(x-1)
        ans[i] = dp[num]+1
    else:
      num = (cnt2+pow(2,N-i-1,x+1))%(x+1)
      ans[i] = dp[num]+1
  return ans
print(*solve(),sep='\n')