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 Rust (1.42.0) (judge verdict: Runtime Error)

use std::io::stdin;
use std::str::FromStr;

fn main(){
    let _n: usize = get_one();
    let mut s = get_chars();
    s.reverse();
    let m = s.iter().fold(0, |acc, &c| if c == '1' {acc + 1} else {acc});
    let mut z: Vec<Vec<usize>> = Vec::new();
    let mut d: Vec<usize> = vec![1, 1, 1];
    for _i in 0..s.len() {
        z.push(d.iter().map(|&x| x).collect());
        if m == 1 {
            d = vec![1, d[1]*2 % m, d[2]*2 % (m+1)];
        } else {
            d = vec![d[0]*2 % (m-1), d[1]*2 % m, d[2]*2 % (m+1)];
        }
    }
    // println!("m: {}", m);
    // for i in 0..d.len() {
        // println!("d: {}", d[i]);
    // }
    // println!("");

    let mut start_bases = Vec::new();
    for i in 0..3 {
        let mut b = 0;
        for j in 0..s.len() {
            if s[j] == '1' {
                b += z[j][i];
                if m == 1 && i ==0 {
                    b = 1;
                } else {
                    b %= m-1+i;
                }
            }
        }
        start_bases.push(b);
    }

    let mut starts = Vec::new();
    for i in 0..s.len() {
        if s[i] == '0' {
            let mut l = start_bases[2];
            l += z[i][2];
            starts.push(l % (m+1));
        } else {
            if m == 1 {
                starts.push(0);
            } else {
                let mut l = start_bases[0];
                l += (m-1) - z[i][0];
                starts.push(l % (m-1));
            }
        }
    }

    // for i in 0..starts.len() {
    //     println!("s: {}", starts[i]);
    // }
    // println!("");

    let anss: Vec<usize> = starts.iter().rev().map(|&i| f(i) + 1).collect();

    for ans in anss {
        println!("{}", ans);
    }
}

fn f(n: usize) -> usize {
    if n == 0 { 0 }
    else {
        let p = popcount(n);
        1 + f(n % p)
    }
}

fn popcount(n: usize) -> usize {
    if n == 0 { 0 }
    else { n % 2 + popcount(n/2) }
}

#[allow(dead_code)]
fn get_line() -> String {
    let mut s = String::new();
    match stdin().read_line(&mut s){
        Ok(_) => {s.trim().to_string()}
        Err(_) => String::new()
    }
}

#[allow(dead_code)]
fn get_vec<T: std::str::FromStr>() -> Vec<T> {
    let line = get_line();
    line.split_whitespace().filter_map(|x| x.parse().ok()).collect()
}

#[allow(dead_code)]
fn get_one<T: FromStr + Copy>() -> T {
    let v = get_vec();
    v[0]
}

#[allow(dead_code)]
fn get_pair<T: FromStr + Copy>() -> (T, T) {
    let v = get_vec();
    (v[0], v[1])
}

#[allow(dead_code)]
fn get_triple<T: FromStr + Copy>() -> (T, T, T) {
    let v = get_vec();
    (v[0], v[1], v[2])
}

#[allow(dead_code)]
fn get_chars() -> Vec<char> {
    get_line().chars().collect()
}

#[allow(dead_code)]
fn vec_min(xs: &Vec<i64>) -> i64 {
    xs.iter().map(|&x|x).fold(std::i64::MAX, std::cmp::min)
}

#[allow(dead_code)]
fn vec_max(xs: &Vec<i64>) -> i64 {
    xs.iter().map(|&x|x).fold(std::i64::MIN, std::cmp::max)
}

#[allow(dead_code)]
fn vec_sum(xs: &Vec<i64>) -> i64 {
    xs.iter().fold(0, |acc, &x| acc+x)
}