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.IO;
using System.Linq;
using System.Numerics;
using static System.Math;

namespace CSharpSpiral {
	class Program {
		public static void Main(string[] args) {
			var sw = new StreamWriter(Console.OpenStandardOutput()) {
				AutoFlush=
#if DEBUG
			true
#else
			false
#endif
			};
			Console.SetOut(sw);
			new Program().Solve(new ConsoleInput(
#if DEBUG
			new StreamReader("../../../inputData.txt")
#else
			Console.In
#endif
			, ' '));
			Console.Out.Flush();
		}
		public void Solve(ConsoleInput cin) {
			var N = cin.ReadInt;
			var X = cin.ReadLine;
			var popc = X.Count(c => c=='1');
			if(popc==1) {
				var uniq1 = X.IndexOf('1');
				if(uniq1==X.Length-1) {
					Console.WriteLine(
						Enumerable.Range(0, N).Select(i => i==N-1 ? 0 : 1)
					.Join("\n"));
				} else {
					Console.WriteLine(
						Enumerable.Range(0, N).Select(i => i==uniq1 ? 0 : i==X.Length-1 ? 2 : 1)
					.Join("\n"));
				}
				return;
			}
			//(1<<(N-i))%(popc+1)
			var tableP = new long[N]; //from MSB
			tableP[N-1]=popc==0 ? 0 : 1;
			for(int i = N-2; i>=0; i--) {
				tableP[i]=tableP[i+1]*2%(popc+1);
			}
			//(1<<(N-i))%(popc-1)
			var tableN = new long[N];
			tableN[N-1]=popc==2 ? 0 : 1;
			for(int i = N-2; i>=0; i--) {
				tableN[i]=tableN[i+1]*2%(popc-1);
			}
			var f1 = new long[N];
			long origP = 0;
			for(int i = 0; i<N; i++) {
				origP=(origP*2+(X[i]=='1' ? 1 : 0))%(popc+1);
			}
			long origN = 0;
			for(int i = 0; i<N; i++) {
				origN=(origN*2+(X[i]=='1' ? 1 : 0))%(popc-1);
			}
			for(int i = 0; i<N; i++) {
				if(X[i]=='1') {
					f1[i]=(origN-tableN[i]+(popc-1))%(popc-1);
				} else {
					f1[i]=(origP+tableP[i]+(popc+1))%(popc+1);
				}
			}
			var c1 = Enumerable.Repeat(1, N).ToArray();
			for(int i = 0; i<N; i++) {
				while(f1[i]!=0) {
					c1[i]++;
					f1[i]%=numofbits5(f1[i]);
				}
			}
			Console.WriteLine(c1.Join("\n"));
		}
		//http://www.nminoru.jp/~nminoru/programming/bitcount.html
		int numofbits5(long bits) {
			bits=(bits&0x55555555)+(bits>>1&0x55555555);
			bits=(bits&0x33333333)+(bits>>2&0x33333333);
			bits=(bits&0x0f0f0f0f)+(bits>>4&0x0f0f0f0f);
			bits=(bits&0x00ff00ff)+(bits>>8&0x00ff00ff);
			return (int)((bits&0x0000ffff)+(bits>>16&0x0000ffff));
		}
	}

	static class ExtendClass {
		public static string Join<T>(this IEnumerable<T> X, string separator = " ")
			=> string.Join(separator, X);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator=separator;
			this._stream=stream;
			inputStream=new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count!=0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i<tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i<N; ++i)
				ret[i]=Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i<N; ++i) ret[i]=ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i<N; ++i) ret[i]=ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i<N; ++i) ret[i]=ReadDouble; return ret; }
		public bool isEOF {
			get { return _stream.Peek()==-1; }
		}
	}
	
}