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 static System.Console;
using static System.Math;

namespace D_AISING2020
{
    class MainClass
    {

        static int[] s, f;


        public static void Main(string[] args)
        {
            s = Array.ConvertAll(ReadLine().Split().ToArray(), int.Parse);
            int n = s[0];
            string x = ReadLine();

            f = new int[n + 1];

            var popcnt = 0;
            var xc = x.ToCharArray();
            popcnt = xc.Count(x => x == '1');

            var modplus = popcnt + 1;
            var modminus = popcnt - 1;

            // WriteLine("modplus : {0}  modminus : {1}",modplus,modminus);

            var allmodplus = 0;
            var allmodminus = 0;

            var binmodplus = new int[n];
            var binmodminus = new int[n];



            for (int i = 0; i < s[0]; i++)
            {
                var d = n - i - 1;
                if (i == 0)
                {
                    binmodplus[0] = 1 % modplus;
                    binmodminus[0] = modminus > 0 ? 1 % modminus : 0;


                }
                else
                {
                    binmodplus[i] = binmodplus[i - 1] * 2 % modplus;
                    binmodminus[i] = modminus > 0 ? binmodminus[i - 1] * 2 % modminus : 0;
                }
            }

            var prebinplus = 1;
            var prebinminus = 1;

            for (int i = n - 1; i >= 0; i--)
            {
                if (xc[i] == '1')
                {
                    allmodplus = (allmodplus + prebinplus) % modplus;
                    allmodminus = modminus > 0 ? (allmodminus + prebinminus) % modminus: allmodminus;
                }
                prebinplus = (prebinplus * 2) % modplus;
                prebinminus = modminus > 0 ? (prebinminus * 2) % modminus:prebinminus;
            }

            // WriteLine("allmodplus : {0} allmodminus : {1}", allmodplus, allmodminus);

            for (int i = 0; i < n; i++)
            {

                var ans = 0;
                var cnt = 0;
                if (xc[i] == '1')
                {
                    if(modminus>0){
                        cnt++;
                        ans = (allmodminus-binmodminus[n-i-1])%modminus;
                    ans = ans < 0 ? ans + modminus : ans;

                    }
                }
                else
                {
                    cnt ++;
                    ans = (allmodplus + binmodplus[n - i - 1]) % modplus;
                    // WriteLine("i : {0}  xc[i] : {1} allmodplus : {2} binmodplus : {3}", i, xc[i], allmodplus, binmodminus[n - i - 1]);
                }
                WriteLine(cnt + FuncCnt(ans));
            }
            return;

        }

        public static int FuncCnt(int x)
        {
            // WriteLine("FuncCnt : {0}", x);
            if (x == 0)
            {
                return 0;
            }
            if (f[x] != 0)
            {
                return f[x];
            }

            var popcnt = 0;

            var xstr = Convert.ToString(x, 2);
            var xstrc = xstr.ToCharArray();
            popcnt = xstrc.Count(x => x == '1');

            f[x] = FuncCnt(x % popcnt) + 1;
            return f[x];
        }





        public static int LowerBound<T>(T[] a, T v)
        {
            return LowerBound(a, v, Comparer<T>.Default);
        }

        public static int LowerBound<T>(T[] a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Length - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res == -1) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }
        public static int UpperBound<T>(T[] a, T v)
        {
            return UpperBound(a, v, Comparer<T>.Default);
        }

        public static int UpperBound<T>(T[] a, T v, Comparer<T> cmp)
        {
            var l = 0;
            var r = a.Length - 1;
            while (l <= r)
            {
                var mid = l + (r - l) / 2;
                var res = cmp.Compare(a[mid], v);
                if (res <= 0) l = mid + 1;
                else r = mid - 1;
            }
            return l;
        }
    }
}