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)

#include "bits/stdc++.h"
#include<random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(lint i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first + r.first, l.second + r.second); }
template<typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2>& l, const pair<T1, T2>& r) { return make_pair(l.first - r.first, l.second - r.second); }
const lint MOD = 1e9 + 7, INF = 1e16;

lint dpL[200001][3], dpR[200001][3];
lint dp2[200001][3];
lint N, cnt = 0;
string s;
lint dp3[200001];

lint dfs(lint i) {
	if (dp3[i] != INF) return dp3[i];
	lint b_cnt = 0, curr = i;
	while (curr > 0) {
		if (curr % 2 == 1) b_cnt++;
		curr /= 2;
	}
	return dp3[i] = dfs(i % b_cnt) + 1;
}

int main() {
	cin >> N;
	cin >> s;
	REP(i, N) {
		if (s[i] == '1') cnt++;
	}
	REP(i, 3) {
		dpL[0][i] = 0;
		dpR[N][i] = 0;
	}
	REP(i, N) {
		dpL[i + 1][0] = (dpL[i][0] * 2 + (s[i] - '0')) % (cnt - 1);
		dpL[i + 1][1] = (dpL[i][1] * 2 + (s[i] - '0')) % cnt;
		dpL[i + 1][2] = (dpL[i][2] * 2 + (s[i] - '0')) % (cnt + 1);
	}
	dp2[N][0] = 1 % (cnt - 1);
	dp2[N][1] = 1 % (cnt - 0);
	dp2[N][2] = 1 % (cnt + 1);
	IREP(i, N) {
		dp2[i][0] = (dp2[i + 1][0] * 2) % (cnt - 1);
		dp2[i][1] = (dp2[i + 1][1] * 2) % (cnt);
		dp2[i][2] = (dp2[i + 1][2] * 2) % (cnt + 1);
	}
	IREP(i, N) {
		dpR[i][0] = (dpR[i + 1][0] + (s[i] - '0') * dp2[i + 1][0]) % (cnt - 1);
		dpR[i][1] = (dpR[i + 1][1] + (s[i] - '0') * dp2[i + 1][1]) % cnt;
		dpR[i][2] = (dpR[i + 1][2] + (s[i] - '0') * dp2[i + 1][2]) % (cnt + 1);
	}
	REP(i, N) {
		dp3[i] = INF;
	}
	dp3[0] = 0;
	REP(i, N + 1) {
		dfs(i);
	}
	REP(i, N) {
		if (s[i] == '0') {
			int a = (dpL[i + 1][2] * dp2[i+1][2] + dp2[i + 1][2] + dpR[i + 1][2]) % (cnt + 1);
			cout << dp3[a] + 1 << endl;
		}
		else {
			int a = (dpL[i][0] * dp2[i][0] + dpR[i + 1][0]) % (cnt - 1);
			cout << dp3[a] + 1 << endl;
			
		}
	}
}