Problem p02609 (python)

Fixed submission:
N=int(input())
Xl=list(map(int,list(input())))
#X=int(input())
#print(Xl)

def func(x):
  cnt=0
  while x>0:
    num1=0
    xcopy=x
    while xcopy:
      if xcopy&1:
        num1+=1
      xcopy>>=1
      
    #print(x,num1)
    x=x%num1
    cnt+=1
  return cnt

func_dic={}
for i in range(N+1):
  func_dic[i]=func(i)
#print(func_dic)

num1_X=Xl.count(1)
#print(num1_X)

mod_nminus=0
mod_nplus=0
for i in range(N):
  if num1_X-1>0:
    mod_nminus*=2
  mod_nplus*=2  
  if Xl[i]==1:
    mod_nminus+=1
    mod_nplus+=1

  if num1_X-1>0:
    mod_nminus%=num1_X-1
  mod_nplus%=num1_X+1
  
#print(mod_nminus,mod_nplus)

answer_list=[]
modpow_plus=1
modpow_minus=1
for i in reversed(range(N)):
  if Xl[i]==0:
    mod=(mod_nplus+modpow_plus)%(num1_X+1)
    answer_list.append(func_dic[mod]+1)
  else:
    if num1_X-1==0:
      answer_list.append(0)
    else:
      mod=(mod_nminus-modpow_minus)%(num1_X-1)
      answer_list.append(func_dic[mod]+1)
  
  modpow_plus*=2  
  modpow_plus%=num1_X+1
  if num1_X-1>0:
    modpow_minus*=2
    modpow_minus%=num1_X-1
  
for i in reversed(range(N)):
  print(answer_list[i])

Buggy submission (verdict: Runtime Error):
N=int(input())
Xl=list(map(int,list(input())))
#X=int(input())
#print(Xl)

def func(x):
  cnt=0
  while x>0:
    num1=0
    xcopy=x
    while xcopy:
      if xcopy&1:
        num1+=1
      xcopy>>=1
      
    #print(x,num1)
    x=x%num1
    cnt+=1
  return cnt

func_dic={}
for i in range(N+1):
  func_dic[i]=func(i)
#print(func_dic)

num1_X=Xl.count(1)
#print(num1_X)

mod_nminus=0
mod_nplus=0
for i in range(N):
  mod_nminus*=2
  mod_nplus*=2
  if Xl[i]==1:
    mod_nminus+=1
    mod_nplus+=1

  mod_nminus%=num1_X-1
  mod_nplus%=num1_X+1
#print(mod_nminus,mod_nplus)

answer_list=[]
modpow_plus=1
modpow_minus=1
for i in reversed(range(N)):
  if Xl[i]==0:
    mod=(mod_nplus+modpow_plus)%(num1_X+1)
    answer_list.append(func_dic[mod]+1)
  else:
    if num1_X-1==0:
      answer_list.append(0)
    else:
      mod=(mod_nminus-modpow_minus)%(num1_X-1)
      answer_list.append(func_dic[mod]+1)
  
  modpow_plus*=2  
  modpow_plus%=num1_X+1
  if num1_X-1>0:
    modpow_minus*=2
    modpow_minus%=num1_X-1
  
for i in reversed(range(N)):
  print(answer_list[i])

Buggy submission (verdict: Runtime Error):
N=int(input())
Xl=list(map(int,list(input())))
#X=int(input())
#print(Xl)

def func(x):
  cnt=0
  while x>0:
    num1=0
    xcopy=x
    while xcopy:
      if xcopy&1:
        num1+=1
      xcopy>>=1
      
    #print(x,num1)
    x=x%num1
    cnt+=1
  return cnt

func_dic={}
for i in range(N):
  func_dic[i]=func(i)
#print(func_dic)

num1_X=Xl.count(1)
#print(num1_X)

mod_nminus=0
mod_nplus=0
for i in range(N):
  mod_nminus*=2
  mod_nplus*=2
  if Xl[i]==1:
    mod_nminus+=1
    mod_nplus+=1

  mod_nminus%=num1_X-1
  mod_nplus%=num1_X+1
#print(mod_nminus,mod_nplus)

answer_list=[]
modpow_plus=1
modpow_minus=1
for i in reversed(range(N)):
  if Xl[i]==0:
    mod=(mod_nplus+modpow_plus)%(num1_X+1)
    answer_list.append(func_dic[mod]+1)
  else:
    if num1_X-1==0:
      answer_list.append(0)
    else:
      mod=(mod_nminus-modpow_minus)%(num1_X-1)
      answer_list.append(func_dic[mod]+1)
  
  modpow_plus*=2  
  modpow_plus%=num1_X+1
  if num1_X-1>0:
    modpow_minus*=2
    modpow_minus%=num1_X-1
  
for i in reversed(range(N)):
  print(answer_list[i])

Buggy submission (verdict: Runtime Error):
N=int(input())
Xl=list(map(int,list(input())))
#X=int(input())
#print(Xl)

def func(x):
  cnt=0
  while x>0:
    num1=0
    xcopy=x
    while xcopy:
      if xcopy&1:
        num1+=1
      xcopy>>=1
      
    #print(x,num1)
    x=x%num1
    cnt+=1
  return cnt

func_dic={}
for i in range(N):
  func_dic[i]=func(i)
#print(func_dic)

num1_X=Xl.count(1)
#print(num1_X)

mod_nminus=0
mod_nplus=0
for i in range(N):
  mod_nminus*=2
  mod_nplus*=2
  if Xl[i]==1:
    mod_nminus+=1
    mod_nplus+=1

  mod_nminus%=num1_X-1
  mod_nplus%=num1_X+1
#print(mod_nminus,mod_nplus)

answer_list=[]
modpow_plus=1
modpow_minus=1
for i in reversed(range(N)):
  if Xl[i]==0:
    mod=(mod_nplus+modpow_plus)%(num1_X+1)
    answer_list.append(func_dic[mod]+1)
  else:
    mod=(mod_nminus-modpow_minus)%(num1_X-1)
    answer_list.append(func_dic[mod]+1)
  
  modpow_plus*=2
  modpow_minus*=2
  modpow_plus%=num1_X+1
  modpow_minus%=num1_X-1
  
for i in reversed(range(N)):
  print(answer_list[i])