Problem p02609 (python)

Fixed submission:
n=int(input())
x=input()
t=int(x,2)
c=x.count('1')
a=t%-~c
b=0 if c<2 else t%~-c
for i in range(n):
  if int(x[i]):
    if c<2: print(0); continue
    else: t=(b-pow(2,n-i-1,c-1))%~-c
  else: t=(a+pow(2,n-i-1,c+1))%-~c
  z=1
  while t:
    d=bin(t).count('1')
    t%=d
    z+=1
  print(z)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
t=int(x,2)
c=x.count('1')
m=[0]*n
k=1
while k<n: m[k]=1; k*=2
def popcount(x):
  x = x - ((x >> 1) & 0x5555555555555555)
  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
  x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
  x = x + (x >> 8)
  x = x + (x >> 16)
  x = x + (x >> 32)
  return x & 0x0000007f
def f(x):
  if x==0: return 0
  if m[x]: return m[x]
  return f(x%popcount(x))+1
for i in range(n):
  if int(x[i]):
    d=c-1
    if c<1: print(0)
    else: print(f((t-pow(2,n-i-1,d))%d)+1)
  else:
    d=c+1
    print(f((t+pow(2,n-i-1,d))%d)+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
t=int(x,2)
c=x.count('1')
m=[0]*n
k=1
while k<n: m[k]=1; k*=2
def popcount(x):
  x = x - ((x >> 1) & 0x5555555555555555)
  x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
  x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f
  x = x + (x >> 8)
  x = x + (x >> 16)
  x = x + (x >> 32)
  return x & 0x0000007f
def f(x):
  if x==0: return 0
  if m[x]: return m[x]
  return f(x%popcount(x))+1
for i in range(n):
  if int(x[i]):
    d=c-1
    if c<1: print(0)
    print(f((t-pow(2,n-i-1,d))%d)+1)
  else:
    d=c+1
    print(f((t+pow(2,n-i-1,d))%d)+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
t=int(x,2)
c=x.count('1')
m=[0]*n
k=1
while k<n: m[k]=1; k*=2
def popcount(x):
  if x==0: return 0
  if m[x]: return m[x]
  return popcount(x%bin(x).count('1'))+1
for i in range(n):
  if int(x[i]):
    d=c-1
    if c<1: print(0)
    print(popcount((t-pow(2,n-i-1,d))%d)+1)
  else:
    d=c+1
    print(popcount((t+pow(2,n-i-1,d))%d)+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
t=int(x,2)
c=x.count('1')
import sys
sys.setrecursionlimit(10**6)
from functools import *
@lru_cache(None)
def popcount(x):
  if x==0: return 0
  return popcount(x%bin(x).count('1'))+1
for i in range(n):
  if int(x[i]):
    d=c-1
    if c<1: print(0)
    print(popcount((t-pow(2,n-i-1,d))%d)+1)
  else:
    d=c+1
    print(popcount((t+pow(2,n-i-1,d))%d)+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
t=int(x,2)
c=x.count('1')
import sys
sys.setrecursionlimit(10**6)
from functools import *
@lru_cache(None)
def popcount(x):
  if x==0: return 0
  return popcount(x%bin(x).count('1'))+1
for i in range(n):
  if int(x[i]):
    d=c-1
    if c<1: print(0)
    print(popcount((t-pow(2,n-i-1,d))%d)+1)
  else:
    d=c+1
    print(popcount((t+pow(2,n-i-1,d))%d)+1)

Buggy submission (verdict: Runtime Error):
n=int(input())
x=input()
t=int(x,2)
c=x.count('1')
from functools import *
@lru_cache(None)
def popcount(x):
  if x==0: return 0
  return popcount(x%bin(x).count('1'))+1
for i in range(n):
  if int(x[i]):
    d=c-1
    print(popcount((t-pow(2,n-i-1,d))%d)+1)
  else:
    d=c+1
    if c<1: print(0)
    print(popcount((t+pow(2,n-i-1,d))%d)+1)