Problem:
Score : 400 points

Problem Statement

Let \mathrm{popcount}(n) be the number of 1s in the binary representation of n.
For example, \mathrm{popcount}(3) = 2, \mathrm{popcount}(7) = 3, and \mathrm{popcount}(0) = 0.

Let f(n) be the number of times the following operation will be done when we repeat it until n becomes 0: "replace n with the remainder when n is divided by \mathrm{popcount}(n)." (It can be proved that, under the constraints of this problem, n always becomes 0 after a finite number of operations.)

For example, when n=7, it becomes 0 after two operations, as follows:

\mathrm{popcount}(7)=3, so we divide 7 by 3 and replace it with the remainder, 1.

\mathrm{popcount}(1)=1, so we divide 1 by 1 and replace it with the remainder, 0.

You are given an integer X with N digits in binary.
For each integer i such that 1 \leq i \leq N, let X_i be what X becomes when the i-th bit from the top is inverted.
Find f(X_1), f(X_2), \ldots, f(X_N).

Constraints

1 \leq N \leq 2 \times 10^5

X is an integer with N digits in binary, possibly with leading zeros.

Input

Input is given from Standard Input in the following format:

N
X

Output

Print N lines. The i-th line should contain the value f(X_i).

Sample Input 1

3
011

Sample Output 1

2
1
1

X_1 = 7, which will change as follows: 7 \rightarrow 1 \rightarrow 0. Thus, f(7) = 2.

X_2 = 1, which will change as follows: 1 \rightarrow 0. Thus, f(1) = 1.

X_3 = 2, which will change as follows: 2 \rightarrow 0. Thus, f(2) = 1.

Sample Input 2

23
00110111001011011001110

Sample Output 2

2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
3

Solution in Java (OpenJDK 11.0.6) (judge verdict: Runtime Error)

import java.util.*;
import java.io.*;
public class Main {
    static class Scan {
        private byte[] buf=new byte[1024];
        private int index;
        private InputStream in;
        private int total;
        public Scan()
        {
            in=System.in;
        }
        public int scan()throws IOException
        {
            if(total<0)
            throw new InputMismatchException();
            if(index>=total)
            {
                index=0;
                total=in.read(buf);
                if(total<=0)
                return -1;
            }
            return buf[index++];
        }
        public int scanInt()throws IOException
        {
            int integer=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    integer*=10;
                    integer+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            return neg*integer;
        }
        public double scanDouble()throws IOException
        {
            double doub=0;
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            int neg=1;
            if(n=='-')
            {
                neg=-1;
                n=scan();
            }
            while(!isWhiteSpace(n)&&n!='.')
            {
                if(n>='0'&&n<='9')
                {
                    doub*=10;
                    doub+=n-'0';
                    n=scan();
                }
                else throw new InputMismatchException();
            }
            if(n=='.')
            {
                n=scan();
                double temp=1;
                while(!isWhiteSpace(n))
                {
                    if(n>='0'&&n<='9')
                    {
                        temp/=10;
                        doub+=(n-'0')*temp;
                        n=scan();
                    }
                    else throw new InputMismatchException();
                }
            }
            return doub*neg;
        }
        public String scanString()throws IOException
        {
            StringBuilder sb=new StringBuilder();
            int n=scan();
            while(isWhiteSpace(n))
            n=scan();
            while(!isWhiteSpace(n))
            {
                sb.append((char)n);
                n=scan();
            }
            return sb.toString();
        }
        private boolean isWhiteSpace(int n)
        {
            if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
            return true;
            return false;
        }
    }
    public static void main(String args[]) throws IOException {
        Scan input=new Scan();
        int n=input.scanInt();
        String str=input.scanString();
        int cnt=0;
        for(int i=0;i<n;i++) {
            if(str.charAt(i)=='1') {
                cnt++;
            }
        }
        int pow0[]=new int[n];
        pow0[0]=1;
        for(int i=1;i<n;i++) {
            pow0[i]=2*pow0[i-1];
            pow0[i]%=(cnt-1);
        }
        int pow1[]=new int[n];
        pow1[0]=1;
        for(int i=1;i<n;i++) {
            pow1[i]=2*pow1[i-1];
            pow1[i]%=(cnt+1);
        }
        int num0=0,num1=0;
        if(cnt!=1) {
            for(int i=0;i<n;i++) {
                if(str.charAt(i)=='1') {
                    num0+=pow0[n-i-1];
                    num0%=(cnt-1);
                }
            }
        }
        for(int i=0;i<n;i++) {
            if(str.charAt(i)=='1') {
                num1+=pow1[n-i-1];
                num1%=(cnt+1);
            }
        }
        StringBuilder ans=new StringBuilder("");
        for(int i=0;i<n;i++) {
            if(str.charAt(i)=='1') {
                if(cnt==1) {
                    ans.append(0);
                    continue;
                }
                num0-=pow0[n-i-1];
                if(num0<0) {
                    num0+=(cnt-1);
                }
                ans.append((calc(num0)+1)+"\n");
                num0+=pow0[n-i-1];
                num0%=(cnt-1);
            }
            else {
                num1+=pow1[n-i-1];
                num1%=(cnt+1);
                ans.append((calc(num1)+1)+"\n");
                num1-=pow1[n-i-1];
                if(num1<0) {
                    num1+=(cnt+1);
                }
            }
        }
        System.out.println(ans);
    }
    public static int calc(int n) {
        int cnt=0;
        while(n!=0) {
            int tmp=n;
            int ones=0;
            while(tmp!=0) {
               ones+=tmp%2;
               tmp/=2;
            }
            n%=ones;
            cnt++;
        }
        return cnt;
    }
}