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 fast { ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define pb push_back
#define ll long long
#define ld long double
#define vll vector<ll>
#define infl LONG_LONG_MAX
#define infd LDBL_MAX
#define F first
#define S second
#define pll pair<ll,ll> 
#define G(a,b) get<a>(b)
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define MT make_tuple
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi(i,a,b) for(ll i=(b-1);i>=a;i--)    
#define endl "\n"
#define nl cout<<"\n";
#define pr(x) cout<<x;
#define pr1(x) cout<<x<<" ";
#define pr2(x,y) cout<<x<<" "<<y;
#define sz(x) (ll)x.size() 
#define ara cout<<"ara-ara\n"; 
 


void func()
{
   ll n;
   cin>>n;
   char s[n+5];
   f(i,1,n+1) cin>>s[i];
   ll cnt1=0;
   f(i,1,n+1) if(s[i]=='1') cnt1++;

   if(cnt1==1)
   {
     ll idx=-1;
     f(i,1,n+1) 
     {
       if(s[i]=='1')
       {
        idx=i;
        break;
       }
     }

     ll ans[n+1];
     ll curr=0;

     fi(i,1,n+1)
     {
       if(i==idx) 
       {
         ans[i]=0;
         continue;
       }
       
       ll pp=idx,qq=curr;
       if(pp==0 || qq==0) ans[i]=1;
       else ans[i]=0;
       curr++;
     }
     
     f(i,1,n+1) cout<<ans[i]<<endl;
     return;

   }



   ll x=cnt1+1,y=cnt1-1;
   
   ll totx=0,toty=0,curr1=1%x,curr2=1%y;
   fi(i,1,n+1)
   {
     if(s[i]=='1') 
     {
       totx=(totx+curr1)%x;
       toty=(toty+curr2)%y;
     }
     curr1=(curr1*2)%x;
     curr2=(curr2*2)%y;
   }

   ll g[n+1];
   g[0]=0;
   f(i,1,n+1) 
   {
     ll xx=__builtin_popcountll(i);
     g[i]=1+g[i%xx];
   }

   ll ans[n+1];
   curr1=1%x,curr2=1%y;
   fi(i,1,n+1)
   {
     if(s[i]=='1')
     {
       ll z=(toty-curr2+y)%y;
       ans[i]=1+g[z];
     }
     else
     {
       ll z=(totx+curr1)%x;
       ans[i]=1+g[z];
     }

     curr1=(curr1*2)%x;
     curr2=(curr2*2)%y;
   }
   
   f(i,1,n+1) cout<<ans[i]<<"\n";
}   
     
     
     
int main() 
{
    fast
    //#ifndef ONLINE_JUDGE
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //#endif // ONLINE_JUDGE
        
     
    ll ntc=1;
    //cin>>ntc;
     
    f(i,1,ntc+1)
    {
      //cout<<"Case #"<<i<<": ";
       func();
    }
     
    return 0;
}