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.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.Aising_2020
{
    class D
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int n = ReadInt();
            string x = Read();

            int popCnt = 0;
            for(int i = 0; i < n; i++)
            {
                if (x[i] == '1')
                {
                    popCnt++;
                }
            }

            if (popCnt == 0)
            {
                for(int i = 0; i < n; i++)
                {
                    WriteLine(1);
                }
                return;
            }
            if (popCnt == 1)
            {
                for(int i = 0; i < n; i++)
                {
                    if (x[n - 1] == '1')
                    {
                        if (x[i] == '1')
                        {
                            WriteLine(0);
                        }
                        else
                        {
                            WriteLine(2);
                        }
                    }
                    else
                    {
                        if (i == n - 1)
                        {
                            WriteLine(2);
                        }
                        else if (x[i] == '1')
                        {
                            WriteLine(0);
                        }
                        else
                        {
                            WriteLine(1);
                        }
                    }
                }
                return;
            }

            int[] modsPlus = new int[n];
            int[] modsMinus = new int[n];
            modsPlus[0] = 1 % (popCnt + 1);
            modsMinus[0] = 1 % (popCnt - 1);
            int baseModPlus = 0;
            int baseModMinus = 0;
            if (x[n - 1] == '1')
            {
                baseModPlus += modsPlus[0];
                baseModMinus += modsMinus[0];
            }
            for (int i = 1; i < n; i++)
            {
                modsPlus[i] = (modsPlus[i - 1]*2) % (popCnt + 1);
                modsMinus[i] = (modsMinus[i - 1]*2) % (popCnt - 1);

                if (x[n - 1 - i] == '1')
                {
                    baseModPlus += modsPlus[i];
                    baseModPlus %= popCnt + 1;
                    baseModMinus += modsMinus[i];
                    baseModMinus %= popCnt - 1;
                }
            }

            for (int i = 0; i < n; i++)
            {
                int mod;
                if (x[i] == '1')
                {
                    mod = baseModMinus + (popCnt - 1 - modsMinus[n - 1 - i]);
                    mod %= popCnt - 1;
                }
                else
                {
                    mod = baseModPlus + modsPlus[n - 1 - i];
                    mod %= popCnt + 1;
                }

                int cnt = 1;
                while (mod > 0)
                {
                    int popCntTmp = 0;
                    int div = 1;
                    while (div <= mod)
                    {
                        if ((mod & div) > 0)
                        {
                            popCntTmp++;
                        }
                        div *= 2;
                    }

                    mod %= popCntTmp;
                    cnt++;
                }

                WriteLine(cnt);
            }
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}