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++ (GCC 9.2.1) (judge verdict: Runtime Error)

#define _USE_MATH_DEFINES
#include  <iostream>
#include  <algorithm>
#include  <climits>
#include  <vector>
#include  <string>
#include  <cstring>
#include  <stack>
#include  <queue>
#include  <cmath>
#include  <iomanip>
#include  <set>
#include  <map>
#include  <new>
#include <cstdint>
#include <cctype>
#include  <cstdio>
#include  <sstream>
#include  <bitset>
using namespace std;
#define rep(i,n)  for(int i = 0; i < n; i++)
#define lrep(i,n,m)  for(int i = n; i < m; i++)

using ll = long long;
using ld = long double;
using Vii = vector<vector<int> >;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vd = vector<double>;
using Vld = vector<long double>;
using Vll = vector<vector<ll> >;
using Vldd = vector<vector<ld> >;
using Vs = vector<string>;
using Vb = vector<bool>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Pli = pair<ll, int>;
using VPi = vector<Pi>;
using VPl = vector<Pl>;
using VPli = vector<Pli>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int dx[4] = { 1,0,-1,0 };
const int dy[4] = { 0,1,0,-1 };

Vi ans;

int main()
{
	int n;
	cin >> n;
	string x;
	cin >> x;

	ans.resize(n, -1);

	int d1 = 0;
	rep(i, n) {
		if (x[i] == '1') {
			d1++;
		}
	}

	reverse(x.begin(), x.end());

	Vi popx(n, 0);
	rep(i, n) {
		if (x[i] == '1') popx[i] = d1 - 1;
		else popx[i] = d1 + 1;
		if (popx[i] == 0) ans[n - 1 - i] = 0;
	}

	
	Vi mod2(n, 0);
	int A[3] = { 0 };
	for (int i = 0; i < 3; i++) {
		if (d1 == 1 && i == 0) continue;
		int t = 1;
		ll u = 0;
		for (int j = 0; j < n; j++) {
			if (x[j] == '1') {
				u = (u + t) % (d1 - 1 + i);
			}
			t = (t * 2) % (d1 - 1 + i);
		}
		u %= (d1 - 1 + i);
		A[i] = u;
	}

	int t1 = 1, t2 = 1, t3 = 1;
	rep(i, n) {
		int u = popx[i];
		if (u <= 0) continue;
		if (x[i] == '0') {
			if (u == d1 - 1) {
				mod2[i] = (A[0] + t1) % u;
			}
			else if (u == d1) {
				mod2[i] = (A[1] + t2) % u;
			}
			else {
				mod2[i] = (A[2] + t3) % u;
			}
		}
		else {
			if (u == d1 - 1) {
				mod2[i] = (u + A[0] - t1) % u;
			}
			else if (u == d1) {
				mod2[i] = (u + A[1] - t2) % u;
			}
			else {
				mod2[i] = (u + A[2] - t3) % u;
			}
		}
		if (d1 - 1 > 0) t1 = (t1 * 2) % (d1 - 1);
		t2 = (t2 * 2) % d1;
		t3 = (t3 * 2) % (d1 + 1);
	}
	reverse(mod2.begin(), mod2.end());

	rep(i, n) {
		if (ans[i] == 0) continue;
		if (mod2[i] < 0) continue;
		if (mod2[i] == 0 && ans[i] == -1) {
			ans[i] = 1;
			continue;
		}
		int tans = 1;
		while (mod2[i] > 0) {
			int u = mod2[i];
			bitset<20> t = bitset<20>(u);
			int c = t.count();
			if (c == 0) break;
			mod2[i] %= c;
			tans++;
		}
		ans[i] = tans;
	}

	for (int u : ans) {
		cout << u << endl;
	}

}