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 <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>


#define int  long long
#define rep(i,begin,end) for(__typeof(end)i=(begin)-((begin)>(end));i!=(end)-((begin)>(end));i+=1-2*((begin)>(end)))
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pb push_back
#define pf push_front
#define eb emplace_back 
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mod2 998244353
#define what_is(x) cerr << #x << " is " << x <<"\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a) for(auto x:a) cout<<x<<" ";
#define INF 1000000000000

// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>


// using namespace __gnu_pbds;
using namespace std;

using ll  = long long;
using vi  = vector<int>;
using pii = pair<int,int>;
using vii = vector<pii>;
using mi  = multiset<int>;
using mii = multiset<pii>;

int lcm(int a,int b) {return (a*(b/__gcd(a,b)));}

int modp(int a,int m,int n)
{
    if(n==0) return 1;
    if(n==1) return a%m;
    int res=modp(a,m,n/2);
    if(n%2==0) return (res*res)%m;
    else return (a*res*res)%m;
}

void solve()
{
    int n,m,t,i,l,k,r,u,v,w,j,ss,sum=0,pos,flag=0,count=0;
    string S,T,U;
    // int x,y,a,b,c;
    cin>>n>>S;
    // cout<<modp(2,3,2);
    // return;
    int popcnt=0;
    rep(i,0,n) popcnt+=(S[i]=='1');
    int num=0,minus1=0,plus1=0;
    reverse(all(S));
    rep(i,0,n)
    {
        num=(num%popcnt+(S[i]-'0')*modp(2,popcnt,i))%popcnt;
    }
    rep(i,0,n)
    {
        plus1=(plus1%(popcnt+1)+(S[i]-'0')*modp(2,popcnt+1,i))%(popcnt+1);
        minus1=(minus1%(popcnt-1)+(S[i]-'0')*modp(2,popcnt-1,i))%(popcnt-1);
    }
    // cout<<minus1<<" "<<plus1<<"\n";
    // reverse(all(S));
    vi ans(n);
    // rep(i,0,n) cout<<S[i]<<" ";
    // cout<<"\n";
    // rep(i,0,n)
    rep(i,0,n)
    {
        t=num;
        if(S[i]=='0')
        {
            t=(plus1+modp(2,popcnt+1,i)+(popcnt+1))%(popcnt+1);
            // cout<<t<<" ";
            // cout<<"\n"<<plus1<<"+"<<modp(2,popcnt+1,i)<<"+"<<popcnt+1<<" "<<i<<"\n";
            count=1;
            while(t)
            {
                t=t%(__builtin_popcountll(t));
                count++;
            }
        }
        else
        {
            t=(minus1-modp(2,popcnt-1,i)+(popcnt-1))%(popcnt-1);
            count=1;
            // cout<<t<<" ";
            while(t)
            {
                t=t%(__builtin_popcountll(t));
                count++;
            }
        }
        ans[i]=count;
    }
    // cout<<"\n";
    reverse(all(ans));
    for(int x:ans) cout<<x<<"\n";
}



signed main()
{
    #ifndef ONLINE_JUDGE 
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
    #endif
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t=1;
    // cin>>t;
    int T=0;
    while(t--)
    {
        T++;
        // cout<<"Scenario #"<<T<<":\n";
        solve();
        if(t)cout<<"\n";
    }
    return 0;
}


/*
6 4
1 3 4 5 6 2
*/