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: Accepted)

#![allow(clippy::needless_range_loop)]
#![allow(unused_macros)]
#![allow(dead_code)]

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        #[allow(unused_mut)]
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

fn set_bits(i: u32) -> u32 {
    let i = i - ((i >> 1) & 0x55555555);
    let i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
    (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24
}

fn main() {
    input! {
        n: usize,
        x: chars
    }
    let set = x.iter().filter(|x| **x == '1').count();
    let exp_2_mod_hi: Vec<usize> = (0..n)
        .scan(1, |state, _| {
            let current = *state;
            *state = *state * 2 % (set + 1);
            Some(current)
        })
        .collect();
    let exp_2_mod_low: Vec<usize> = if set > 1 {
        (0..n)
            .scan(1, |state, _| {
                let current = *state;
                *state = *state * 2 % (set - 1);
                Some(current)
            })
            .collect()
    } else {
        vec![]
    };
    let x_mod_hi = x.iter().rev().zip(&exp_2_mod_hi).fold(0, |acc, (x, y)| {
        (acc + y * (*x == '1') as usize) % (set + 1)
    });
    let x_mod_low = x.iter().rev().zip(&exp_2_mod_low).fold(0, |acc, (x, y)| {
        (acc + y * (*x == '1') as usize) % (set - 1)
    });
    for (c, key) in x.iter().zip((0..n).rev()) {
        let mut r = if *c == '1' {
            if set == 1 {
                println!("0");
                continue;
            }
            (x_mod_low + (set - 1) - exp_2_mod_low[key]) % (set - 1)
        } else {
            (x_mod_hi + exp_2_mod_hi[key]) % (set + 1)
        } as u32;
        let mut cnt = 1;
        while r > 0 {
            r = r % set_bits(r);
            cnt += 1;
        }
        println!("{}", cnt);
    }
}