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: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {
  private static class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;

    public FastScanner(InputStream in) {
      reader = new BufferedReader(new InputStreamReader(in));
      tokenizer = null;
    }

    public String next() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          tokenizer = new StringTokenizer(reader.readLine());
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      return tokenizer.nextToken();
    }

    public String nextLine() {
      if (tokenizer == null || !tokenizer.hasMoreTokens()) {
        try {
          return reader.readLine();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }

      return tokenizer.nextToken("\n");
    }

    public long nextLong() {
      return Long.parseLong(next());
    }

    public int nextInt() {
      return Integer.parseInt(next());
    }

    public double nextDouble() {
      return Double.parseDouble(next());
    }

    public int[] nextIntArray(int n) {
      int[] a = new int[n];
      for (int i = 0; i < n; i++)
        a[i] = nextInt();
      return a;
    }

    public long[] nextLongArray(int n) {
      long[] a = new long[n];
      for (int i = 0; i < n; i++)
        a[i] = nextLong();
      return a;
    }
  }

  private static int powmod(int a, int n, int mod) {
    if (n == 0) return 1;
    int half = powmod(a, n / 2, mod) % mod;
    if (n % 2 == 0) return (half * half) % mod;
    return (a * ((half * half) % mod)) % mod;
  }

  private static int mod(String bin, int mod) {
    int val = 0;
    for (int i = bin.length() - 1; i >= 0; i--) {
      if (bin.charAt(i) == '0') continue;
      int n = bin.length() - 1 - i;
      val += powmod(2, n, mod);
      val %= mod;
    }
    return val;
  }

  private static int getF(String bin, int mod, Map<String, Integer> f) {
    if (f.containsKey(bin)) return f.get(bin);
    if (mod == 0) {
      f.put(bin, 0);
      return f.get(bin);
    }
    int m = mod(bin, mod);
    String mbin = Integer.toString(m, 2);
    int count = 0;
    for (int i = 0; i < mbin.length(); i++) {
      if (mbin.charAt(i) == '1') count++;
    }
    f.put(bin, 1 + getF(mbin, count, f));
    return f.get(bin);
  }

  public static void main(String[] args) {
    FastScanner sc = new FastScanner(System.in);
    int n = sc.nextInt();
    String x = sc.next();
    StringBuilder sb = new StringBuilder();
    BigInteger xbin = new BigInteger(x, 2);
    BigInteger zeroBin = BigInteger.valueOf(0L);
    Map<String, Integer> f = new HashMap<>();
    f.put(zeroBin.toString(), 0);
    for (int i = 0; i < n; i++) {
      if (sb.length() > 0) sb.append("\n");
      BigInteger xibin = xbin.flipBit(i);
      BigInteger xibinInitial = xbin.flipBit(i);
      int count = 0;
      while (!f.containsKey(xibin.toString())) {
        count++;
        int m = xibin.bitCount();
        xibin = xibin.mod(BigInteger.valueOf((long) m));
      }
      f.put(xibinInitial.toString(), count + f.get(xibin.toString()));
      sb.append(f.get(xibinInitial.toString()));
    }
    System.out.println(sb.reverse().toString());
//    int ones = 0;
//    for (int i = 0; i < n; i++) {
//      if (x.charAt(i) == '1') ones++;
//    }
//
//    Map<String, Integer> f = new HashMap<>();
//    StringBuilder sb = new StringBuilder();
//    for (int i = 0; i < n; i++) {
//      if (sb.length() > 0) sb.append("\n");
//      String xi;
//      int m;
//      if (x.charAt(i) == '0') {
//        m = ones + 1;
//        xi = x.substring(0, i) + '1' + x.substring(i + 1);
//      } else {
//        m = ones - 1;
//        xi = x.substring(0, i) + '0' + x.substring(i + 1);
//      }
//      int count = getF(xi, m, f);
//      sb.append(count);
//    }
  }
}