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::collections::*;
use std::io::*;
use std::*;
macro_rules! get {
    ($($t:tt),*; $n:expr) => {
        {
            let stdin = std::io::stdin();
            let ret = std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
                let line = line.unwrap();
                let mut it = line.split_whitespace();
                _get!(it; $($t),*)
            }).collect::<Vec<_>>();
            ret
        }
    };
    ($($t:tt),*) => {
        {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut it = line.split_whitespace();
            _get!(it; $($t),*)
        }
    };
}
macro_rules! _get {
    // iterator(splitted input-string) => iterator(output-type)
    ($it:ident; [char]) => {
        _get!($it; String).chars().collect::<Vec<_>>()
    };
    ($it:ident; usize1) => {
        $it.next().unwrap().parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
    };
    ($it:ident; [usize1]) => {
        $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
    };
    ($it:ident; [$t:ty]) => {
        $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
    };
    ($it:ident; $t:ty) => {
        $it.next().unwrap().parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
    };
    ($it:ident; $($t:ty),+) => {
        ($(_get!($it; $t)),*)
    };
}
fn modpow(a: i64, p: i64, m: i64) -> i64 {
    if p == 0 {
        return 1;
    }
    if p % 2 == 0 {
        let d = modpow(a, p / 2, m);
        return d * d % m;
    } else {
        return a * modpow(a, p - 1, m) % m;
    }
}

fn main() {
    let n = get!(usize);
    if n == 1 {
        let x = get!(i32);
        if x == 1 {
            println!("{}", 0);
        } else {
            println!("{}", 1);
        }
        return;
    }
    let mut x = get!([char]);
    let mut dp = vec![[0; 2]; n as usize];
    dp[0][0] = if x[0] == '1' { 1 } else { 0 };
    dp[0][1] = if x[0] == '1' { 1 } else { 0 };
    let pops = x.iter().filter(|c| c == &&'1').count() as i64;
    for i in 1..n {
        dp[i][0] = dp[i - 1][0] * 2 + if x[i] == '1' { 1 } else { 0 };
        dp[i][0] %= pops - 1;
        dp[i][1] = dp[i - 1][1] * 2 + if x[i] == '1' { 1 } else { 0 };
        dp[i][1] %= pops + 1;
    }
    for i in 0..n {
        let pop = if x[i] == '1' { 0 } else { 1 };
        let popcount: i64 = pops + if x[i] == '1' { -1 } else { 1 };
        if popcount == 0 {
            println!("{}", 0);
            continue;
        }
        let mut left = dp[n - 1][pop] as i64
            + (if pop == 0 { -1 } else { 1 }) * modpow(2, (n - i - 1) as i64, popcount as i64);
        left = (left + popcount) % popcount;
        let mut count = 1;
        while left != 0 {
            left = left % left.count_ones() as i64;
            count += 1;
        }
        println!("{}", count);
    }
}