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: Accepted)

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ld> vd;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<vector<ll>> vvll;
typedef vector<pair<ll, ll>> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;  // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;     // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FOR(i, a, b) for (ll i = (ll)a; i < (ll)b; i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); i++)
#define FI first
#define SE second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define TS to_string
#define NP next_permutation
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) (ll) x.size()
#define SP(x) setprecision((ll)x)


ll INF = 1e9;
ll NIL = -1;
ll MOD = 1000000007;
ll LINF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;}     //最小公倍数

void yes(){ cout << "Yes" << endl;}
void no(){ cout << "No" << endl;}

//-----------------------------------------
ll num(ll x){
    ll res = 0;
    while(x>0){
        if(x%2==1){
            res++;
        }
        x /= 2;
    }
    return res;
}

long long power_mod(long long x, long long y, long long m){
    long long res = 1 ;
    if(y>0){
        res = power_mod(x,y/2,m) ;
        if(y%2==0){
            res = (res*res)%m ;
        }
        else{
            res = (((res*res)%m)*x)%m ;
        }
    }
    return res ;
}
//-----------------------------------------
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    string s;
    cin >> n >> s;
    reverse(ALL(s));

    ll m = 0;
    REP(i,n){
        if(s[i]=='1'){
            m++;
        }
    }

    vll mp(n,0);
    vll pp(n,0);

    REP(i,n){
        if(m!=1){
            mp[i] = power_mod(2,i,m-1);
        }
        pp[i] = power_mod(2,i,m+1);
    }

    ll a = 0;
    ll b = 0;

    REP(i,n){
        if(s[i]=='1'){
            if(m!=1){
                a += mp[i];
                a %= (m-1);
            }
            b += pp[i];
            b %= (m+1);
        }
    }

    vll vec;

    ll hoge, ans;

    REP(i,n){
        if(s[i]=='0'){
            hoge = b;
            hoge += pp[i];
            hoge %= (m+1);
            ans = 1;
            while(hoge>0){
                ans++;
                hoge %= num(hoge);
            }
            vec.PB(ans);
        }
        else{
            if(m==1){
                vec.PB(0);
                continue;
            }
            hoge = a;
            hoge -= mp[i];
            hoge += (m-1);
            hoge %= (m-1);
            ans = 1;
            while(hoge>0){
                ans++;
                hoge %= num(hoge);
            }
            vec.PB(ans);
        }
    }

    reverse(ALL(vec));

    REP(i,n){
        cout << vec[i] <<endl;
    }






    
    return 0;
}