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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define speed ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long 
#define ull unsigned long long
#define db double
#define vi vector<ll>
#define vvi vector<vector<int> >
#define vvii vector<vector<pii> >
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a-1;i>=b;i--)
#define endl "\n"
#define pii pair<ll,ll>
#define piii pair<ll,pair<ll,ll> >
#define mem(a,b) memset(a , b , sizeof a ) ; 
#define pb push_back
#define pf push_front
#define F first 
#define S second 
#define mp make_pair
#define all(v) v.begin(),v.end()

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}



#define deb1(a) cout<<#a<<"=*"<<a<<endl;
#define deb2(a) cout<<#a<<"=@"<<a<<endl;
#define deb3(a) cout<<#a<<"=#"<<a<<endl;
#define deb4(a,b) cout<<#a<<"="<<a<<" and "<<#b<<"="<<b<<endl;
#define deb5(a,b) cout<<#a<<"="<<a<<" and "<<#b<<"="<<b<<endl;


typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;


ll mod=1000000007;
ll vis[212345]={0};
vvi v(212345);
ll d[212345]={0};


ll pow(ll a, ll b, ll m)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%m;
		b/=2;
		a=(a*a)%m;
	}
	return ans;
}

ll ff(ll k)
{//	deb1(k);
	if(k==0) return 0;
	ll zz=__builtin_popcountll(k);
	ll t=k%zz;
	return 1+ff(t);
}



int main()
{
speed

ll n;
cin>>n;
ll x[n];
ll z=0;
per(i,n,0)
{

char ch;
cin>>ch;
x[i]=ch-'0';
z+=x[i];
//cout<<"$\n";
}
ll k1,k2;
k1=0;
k2=0;
ll r1,r2;
r1=0;
r2=0;
ll c1=1;
ll c2=1;


if(z<=1)
{
	if(z==0)
	{
		rep(i,0,n) cout<<"1\n";
	}
	else
	{
		if(x[0])
		{
			per(i,n,1) cout<<"2\n";
			cout<<"0\n";
		}
		else
		{
			per(i,n,0)
			{
				if(x[i]) cout<<"0\n";
				else cout<<"1\n";
			}
		}
	}
	
	return 0;
}
ll p1[n],p2[n];

rep(i,0,n)
{	if(i) c1*=2;
c1%=(z+1);
	if(i) c2*=2;
	c2%=(z-1);
	p1[i]=c1;
	p2[i]=c2;
	if(!x[i]) continue;
	//k1+=c/(z+1);
	r1+=c1;
	//k1+=r1/(z+1);
	r1=r1%(z+1);
	//k2+=c/(z-1);
	r2+=c2;
	
	//k2+=r2/(z-1);
	r2=r2%(z-1);	
	
}

per(i,n,0)
{
	if(x[i])
	{
		ll q=0;
		if(z==1)
		{
			cout<<"0\n";
			continue;
		}
		q=(r2+z-1-p2[i])%(z-1);
		cout<<1+ff(q)<<endl;
	}
	else
	{
		ll q;
		q=(r1+p1[i])%(z+1);
		cout<<1+ff(q)<<endl;
		
	}
}




}