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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder2 {
	class Program {
		static void Main(string[] args) {
     	var inputArray = retreiveLongArray();
			var inputCount = 1;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			long N = inputArray[0];
			var X = Console.ReadLine();

			//扱いやすいように1の桁から並べる
			var Xrev = X.Reverse().ToArray();

			var popCount = Xrev.Where(x => x == '1').Count();
			
			//popCount の+-1を格納
			var popS = new long[2];
			popS[0] = popCount + 1;
			popS[1] = popCount - 1 <=0 ? 1 : popCount - 1;

			//N...2のN乗  2...pop+-　それぞれの余り
			var TwoMods = new long[N, 2];
			TwoMods[0,0] = 1 % popS[0];
			TwoMods[0,1] = 1 % popS[1];

			for (int p = 0; p < 2; p++) {
				for (int i = 1; i < N; i++) {
					TwoMods[i, p] = (TwoMods[i - 1, p] * 2) % popS[p];
				}
			}

			//Xをpop+-1で割ったあまり
			var Fx = new long[2];

			
			for (int i = 0; i < N; i++) {
				var c = Xrev[i];
				for (int p = 0; p < 2; p++) {
					if (c == '1') {
						Fx[p] += TwoMods[i, p];
						Fx[p] %= popS[p];
					}

				}
			}

			//i番目をひっくり返す　降順になる
			for (var i = N-1; i >= 0; i--) {
				var c = Xrev[i];
				var first = 0L;
				if (c=='1') {

					//1つしかない1を反転させると、操作0回になる
					if (popCount==1) {
						Console.WriteLine(0);
						continue;
					}

					var p = 1;
					first = Fx[p] - TwoMods[i, p];
					if (first < 0) {
						first += popS[p];
					}
				}
				else {
					var p = 0;
					first = Fx[p] + TwoMods[i, p];
					first %= popS[p];
				}

				var ans = 1 + function(first);
				Console.WriteLine(ans);
			}

		}


		/// <summary>
		/// 問題文のfunctionを実装した関数
		/// </summary>
		/// <param name="n"></param>
		/// <returns></returns>
		private static long function(long n) {

			var count = 0;

			while (n >0) {
				//1が何個あるか求める
				var OneCount = 0L;
				for (int i = 0; i < 32; i++) {
					if ((n >> i & 1) == 1) {
						OneCount++;
					}
				}

				//nを割ってあまりを求める
				n %= OneCount;
				count++;
			}
			return count;


		}



		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray = Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}


		/// <summary>
		/// 2組みずつの入力データ(数値)からTuppleListを作成する
		/// </summary>
		/// <param name="N"></param>
		/// <returns></returns>
		private static List<Tuple<long, long>> createLongTuppleList(long N) {
			var inpList = new List<Tuple<long, long>>();

			for (int i = 0; i < N; i++) {
				var inp = retreiveLongArray();
				var item1 = inp[0];
				var item2 = inp[1];
				inpList.Add(new Tuple<long, long>(item1, item2));
			}
			return inpList;
		}
	}
}