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: Wrong Answer)

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repr(i,n) for(int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(),v.end()
typedef long long ll;

vector<ll> bitconversion(ll i, ll n, ll N){
    ll x = 1;
    rep(j,N){
        x *= n;
    }
    vector<ll> vec(N);
    rep(j,N){
        x /= n;
        vec[j] = i / x;
        i -= x * vec[j];
    }
    return vec;
}

int main(){
    ll N;
    cin >> N;
    string X;
    cin >> X;
    vector<ll> popcount(200004,0);
    for (ll i = 1; i <= 200003; i++){
        vector<ll> binary = bitconversion(i, 2, 20);
        ll count = 0;
        rep(j,20){
            if (binary[j] == 1) count++;
        }
        popcount[i] = count;
    }
    vector<ll> popcount_time(200004,0);
    popcount_time[1] = 1;
    popcount_time[2] = 1;
    popcount_time[3] = 2;
    popcount_time[4] = 1;
    popcount_time[5] = 2;
    popcount_time[6] = 1;
    popcount_time[7] = 2;
    popcount_time[8] = 1;
    popcount_time[9] = 2;
    popcount_time[10] = 1;
    popcount_time[11] = 2;
    popcount_time[12] = 1;
    popcount_time[13] = 2;
    popcount_time[14] = 2;
    popcount_time[15] = 3;
    popcount_time[16] = 1;
    popcount_time[17] = 2;
    popcount_time[18] = 1;
    popcount_time[19] = 2;
    popcount_time[20] = 1;
    for (ll i = 21; i <= 200003; i++){
        popcount_time[i] = (popcount_time[i % popcount[i]]) + 1;
    }
    ll cnt = 0;
    rep(i,N){
        if (X[i] == '1') cnt++;
    }
    ll cnt_minus = cnt - 1;
    ll cnt_plus = cnt + 1;
    if (cnt_minus == 0) cnt_minus = 2;
    vector<ll> bit_minus(N,0);
    vector<ll> bit_plus(N,0);
    ll num_minus = 1;
    rep(i,N){
        bit_minus[N - i - 1] = num_minus % cnt_minus;
        num_minus = (num_minus * 2) % cnt_minus;
    }
    ll num_minus_all = 0;
    rep(i,N){
        if (X[i] == '1') num_minus_all = (num_minus_all + bit_minus[i]) % cnt_minus;
    }
    ll num_plus = 1;
    rep(i,N){
        bit_plus[N - 1 - i] = num_plus % cnt_plus;
        num_plus = (num_plus * 2) % cnt_plus;
    }
    ll num_plus_all = 0;
    rep(i,N){
        if (X[i] == '1') num_plus_all = (num_plus_all + bit_plus[i]) % cnt_plus;
    }
    /*rep(i,N){
        if (X[i] == '1') cout << (num_minus_all + cnt_minus - bit_minus[i]) % cnt_minus << endl;
        else cout << (num_plus_all + cnt_plus + bit_plus[i]) % cnt_plus << endl;
    }*/
    rep(i,N){
        if (X[i] == '1'){
            if (cnt_minus == 0) cout << 1 << endl;
            else cout << popcount_time[(num_minus_all + cnt_minus * 1000000 - bit_minus[i]) % cnt_minus] + 1 << endl;
        }
        else{
            cout << popcount_time[(num_plus_all + cnt_plus + bit_plus[i]) % cnt_plus] + 1 << endl;
        }
    }
}