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: Wrong Answer)

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

fn input<T:FromStr>() -> T {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    line.trim().parse().ok().unwrap()
}

fn input_char_vec() -> Vec<char> {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    line.trim().chars().collect()
}

fn digit(n: u64) -> u64 {
    if n == 0 {
        return 1;
    }
    for i in 0..200001u32 {
        if n < 2u64.pow(i) {
            return i as u64;
        }
    }
    20u64
}

fn calc(x: &Vec<char>, count: u64) -> u64{
    let d = digit(count);
    // println!("d = {}", d);
    let mut buf: u64 = 0;
    for i in 0..d-1 {
        buf *= 2;
        buf += x[i as usize] as u64 - '0' as u64;
    }
    // println!("init buf = {}", buf);
    for i in d as usize - 1..x.len() {
        buf *= 2;
        buf += x[i] as u64 - '0' as u64;
        buf %= count;
        // println!("buf = {}", buf);
    }
    buf
}

fn digit_sum(n: u64) -> u64 {
    let mut m = n;
    let mut sum = 0u64;
    let mut i = 20u32;
    while i > 0 {
        let l = m / 2u64.pow(i);
        sum += l;
        m -= l * 2u64.pow(i);
        i -= 1;
    }
    sum + m
}

fn f(n: u64, init_count: u64) -> u64 {
    if n == 0 {
        return 0;
    }
    let mut n = n;
    let mut count = 0;
    n %= init_count;
    count += 1;
    while n > 0 {
        // println!("digit_sum = {}", digit_sum(n));
        n %= digit_sum(n);
        count += 1;
    }
    count
}

fn main() {
    let n: u64 = input();
    let x = input_char_vec();

    // let mut num = 0;
    // for c in &x {
    //     num *= 2;
    //     num += *c as u64 - '0' as u64;
    // }
    // println!("num = {}", num);

    let mut count = 0;
    for c in &x {
        if *c == '1' {
            count += 1;
        }
    }
    // println!("count = {}", count);
    if count == 0 {
        for i in 0..n {
            println!("0");
        }
        return;
    }
    if count == 1 {
        let a = calc(&x, count + 1);
        let mut a_vec = vec![1 % (count + 1); n as usize];
        for i in 1..n as usize {
            a_vec[i] = a_vec[i - 1] * 2 % (count + 1);
        }
        for i in 0..n as usize {
            if x[i] == '1' {
                println!("0");
            } else {
                let n = a + a_vec[n as usize - 1 - i];
                println!("{}", f(n, count + 1));
            }
        }
        return;
    }

    let a = calc(&x, count + 1);
    let b = calc(&x, count - 1);
    // println!("a = {}, b = {}", a, b);
    let mut a_vec = vec![1 % (count + 1); n as usize];
    let mut b_vec= vec![1 % (count - 1); n as usize];
    for i in 1..n as usize {
        a_vec[i] = a_vec[i - 1] * 2 % (count + 1);
        b_vec[i] = b_vec[i - 1] * 2 % (count - 1);
    }
    // println!("{:?}", a_vec);
    // println!("{:?}", b_vec);
    for i in 0..n as usize {
        if x[i] == '1' {
            let n = b + count - 1 - b_vec[n as usize - 1 - i];
            // println!("{}", n);
            println!("{}", f(n, count - 1));
        } else {
            let n = a + a_vec[n as usize - 1 - i];
            println!("{}", f(n, count + 1));
        }
    }
}