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 <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const int MAX_N = 200000 * 2;

map<ll, vector<ll>> powers;

ll f(ll mod, int bit, bool to_one, const string &original_S) {
    // printf("mod: %lld, bit: %d, to_one: %d, %s\n", mod, bit, to_one, original_S.c_str());
    static map<ll, ll> memo;
    if (powers.find(mod) == powers.end()) {
        // power[i] is 2 ^ i % mod
        vector<ll> power(MAX_N + 10);
        power[0] = 1 % mod;

        for (int i = 1; i <= MAX_N; i++) {
            power[i] = power[i - 1] * 2 % mod;
            // printf("power[%d]: %lld\n", i, power[i]);
        }
        powers[mod] = power;
    }

    vector<ll> &power = powers[mod];

    if (memo.find(mod) == memo.end()) {
        ll v = 0;
        string T = original_S;
        reverse(T.begin(), T.end());
        for (int i = 0; i < (int) T.size(); i++) {
            if (T[i] == '1') {
                v = (v + power[i]) % mod;
            }
        }
        memo[mod] = v;
    }

    ll ans = memo[mod];
    // printf("memo: %lld\n", ans);

    if (to_one) {
        ans = (ans + power[bit]) % mod;
    } else {
        // if ((x += MOD-a.x) >= MOD) x -= MOD;
        if ((ans += mod - power[bit]) >= mod) ans -= mod;
        // ans = (ans - power[bit]) % mod;
        // while (ans < 0) {
        //     ans += mod;
        // }
    }

    // string T = S;
    // reverse(T.begin(), T.end());
    // for (int i = 0; i < (int) T.size(); i++) {
    //     if (T[i] == '1') {
    //         ans = (ans + (*power)[i]) % mod;
    //     }
    // }

    return ans;
}

int count_bit(const string &S) {
    int ans = 0;

    for (int i = 0; i < (int) S.size(); i++) {
        if (S[i] == '1') {
            ans++;
        }
    }

    return ans;
}

int main(void) {
    int N;
    cin >> N;
    string S;
    cin >> S;

    const int bit_count = count_bit(S);
    // printf("bit_count: %d\n", bit_count);

    for (int i = 0; i < N; i++) {
        string T = S;
        ll mod;
        if (T[i] == '1') {
            T[i] = '0';
            mod = bit_count - 1;
        } else {
            T[i] = '1';
            mod = bit_count + 1;
        }
        // printf("%s, %lld\n", T.c_str(), mod);
        // printf("%d: %lld\n", i, f(mod, T));

        ll x = f(mod, N - i - 1, T[i] == '1' ? true : false, S);

        int cnt = 1;
        // printf("x: %lld\n", x);
        while (x > 0) {
            int bit_count = __builtin_popcountll(x);
            // printf("x: %lld, bit_count: %d\n", x, bit_count);
            x = x % bit_count; // f(bit_count, to_string(x));
            cnt++;
        }

        printf("%d\n", cnt);
    }

    return 0;
}