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>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;

#define inf 1e+9
#define endl "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {a = b;return 1;} return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {a = b;return 1;} return 0;
}
template <class T>
void line(vector<T> &x){for (auto&& xi : x) cin >> xi;}

inline void IN(void){ return;}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return;}

#define INT(...) int __VA_ARGS__; IN (__VA_ARGS__)
#define LINT(...) lint __VA_ARGS__; IN (__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
    sort(all(x)); x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end){
    for (auto i = first; i < end - 1; i++) cout << *i << " ";
    cout << *(end-1) << endl;
}
template <class T> void print(T a){ cout << a << endl;};
vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

lint popcount(lint n){
    int res = 0;
    for (int i = 0; i < 32; ++i){
        if (n & (1 << i)) res++;
    }
    return res;
};

vlint ans(300000, -1);

int main(){
    INT(n);
    ans[0] = 0;
    ans[1] = 1;
    rep1(i, n+1){
        ans[i+1] = ans[(i+1)%popcount(i+1)]+1;
    }
  	STR(x); lint pc = 0;
    rep(i, n){
        if (x[i] == '1') pc++;
    }
    lint m_a = pc-1; lint m_b = pc+1;
    vlint cnt_a(n+1, 1), cnt_b(n+1, 1);
  	cnt_a[0] = 1 % m_a; cnt_b[0] = 1 % m_b;
    rep1(i, n+1) {
        cnt_a[i] = cnt_a[i-1] * 2 % m_a;
        cnt_b[i] = cnt_b[i-1] * 2 % m_b;
    }
    lint x_a = 0; lint x_b = 0;
    rep(i, n){
        if (x[i] == '1'){
            x_a += cnt_a[n-1-i] % m_a;
            x_a %= m_a;
            x_b += cnt_b[n-1-i] % m_b;
            x_b %= m_b;
        }
    }
  	rep(i, n+1){
      cnt_a[i] = (x_a - cnt_a[i]) % m_a;
      cnt_b[i] = (x_b + cnt_b[i]) % m_b;
    }
  	lint k;lint ret;
    rep(i, n){
        k = n - 1 - i;
        if (x[i] == '1'){
            ret = cnt_a[k];
          	if (ret < 0) ret += m_a;
          
        }
        else{
            ret = cnt_b[k];
          	if (ret > m_b) ret %= m_b;
        }
        print(ans[ret] + 1);
    }
  
}