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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.IO;
using System.Text;
using CompLib.Algorithm;
using CompLib.Util;

public class Program
{
    private int N;
    private char[] X;

    private int[] Dp;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        X = sc.NextCharArray();

        // XのpopCount
        int all = 0;
        foreach (char c in X)
        {
            if (c == '1') all++;
        }

        int p1 = 0;
        int m1 = 0;
        for (int i = 0; i < N; i++)
        {
            p1 *= 2;
            p1 += X[i] - '0';
            p1 %= all + 1;

            if (all - 1 > 0)
            {
                m1 *= 2;
                m1 += X[i] - '0';
                m1 %= all - 1;
            }
        }

        // 2^iを+1 ,-1で割った余り

        int[] d1 = new int[N];
        int[] d2 = new int[N];
        d1[0] = 1 % (all + 1);
        if (all - 1 > 0) d2[0] = 1 % (all - 1);
        for (int i = 1; i < N; i++)
        {
            d1[i] = (d1[i - 1] * 2) % (all + 1);
            if (all - 1 > 0) d2[i] = (d2[i - 1] * 2) % (all - 1);
        }

        Dp = new int[400000];
        Dp[0] = 0;
        for (int i = 1; i < 400000; i++)
        {
            Dp[i] = Dp[i % (Algorithm.BitCount(i))] + 1;
        }


        var sb = new StringBuilder();

        for (int i = 0; i < N; i++)
        {
            int d = N - i - 1;
            if (X[i] == '0')
            {
                // 1になる
                int m = (p1 + d1[d]) % (all + 1);
                sb.AppendLine((Dp[m] + 1).ToString());
            }
            else
            {
                // 0
                if (all == 1)
                {
                    sb.AppendLine("0");
                }
                else
                {
                    int m = (m1 - d2[d]) % (all - 1);
                    if (m < 0) m += (all - 1);
                    sb.AppendLine((Dp[m] + 1).ToString());
                }
            }
        }

        Console.Write(sb);

        // all+1で割ったあまり、all-1で割った余り、全体を
    }


    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Algorithm
{
    static class Algorithm
    {
        /// <summary>
        /// nの立っているbitの数
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public static int BitCount(long n)
        {
            n = (n & 0x5555555555555555) + (n >> 1 & 0x5555555555555555);
            n = (n & 0x3333333333333333) + (n >> 2 & 0x3333333333333333);
            n = (n & 0x0f0f0f0f0f0f0f0f) + (n >> 4 & 0x0f0f0f0f0f0f0f0f);
            n = (n & 0x00ff00ff00ff00ff) + (n >> 8 & 0x00ff00ff00ff00ff);
            n = (n & 0x0000ffff0000ffff) + (n >> 16 & 0x0000ffff0000ffff);
            return (int) ((n & 0x00000000ffffffff) + (n >> 32 & 0x00000000ffffffff));
        }
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}