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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use std::collections::BTreeMap;

fn popcount(x: u32) -> u32 {
    if x <= 1 {
        return x;
    }

    let mut x = x;
    let mut cnt = 0;
    while x != 1 {
        if x % 2 == 1 {
            cnt += 1;
        }
        x /= 2;
    }

    cnt + 1
}

fn exec(x: u32, memo: &BTreeMap<u32, u32>) -> u32 {
    let mut tmp = x;
    let mut num = 0;
    while tmp != 0 {
        if memo.contains_key(&tmp) {
            num += memo.get(&tmp).unwrap();
            break;
        } else {
            let c = popcount(tmp);
            tmp %= c;
            num += 1;
        }
    }

    num
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_popcount() {
        assert_eq!(popcount(0), 0);
        assert_eq!(popcount(1), 1);
        assert_eq!(popcount(2), 1);
        assert_eq!(popcount(3), 2);
        assert_eq!(popcount(4), 1);
        assert_eq!(popcount(5), 2);
        assert_eq!(popcount(6), 2);
    }

    #[test]
    fn test_exec() {
        let mut memo = BTreeMap::new();
        assert_eq!(exec(0, &memo), 0);
        assert_eq!(exec(1, &memo), 1);
        assert_eq!(exec(5, &memo), 2); // 110
        assert_eq!(exec(6, &memo), 1); // 110
        assert_eq!(exec(7, &memo), 2);

        memo.insert(2, 0);

        assert_eq!(exec(2, &memo), 0);
    }
}

fn main() {
    use proconio::input;
    input!(n: usize);
    input!(mut cs: proconio::marker::Chars);

    let mut xi = 0;
    for &c in cs.iter() {
        if c == '1' {
            xi += 1;
        }
    }

    if xi == 0 {
        for _ in 0..n {
            println!("{}", 1);
        }
        return;
    } else if xi == 1 {
        let tail_one = cs.last().unwrap() == &'1';
        if tail_one {
            for _ in 0..n - 1 {
                println!("{}", 2);
            }
            println!("{}", 0);
            return;
        } else {
            for (idx, &c) in cs.iter().enumerate() {
                if c == '1' {
                    println!("{}", 0);
                } else {
                    if idx == n - 1 {
                        println!("{}", 2);
                    } else {
                        println!("{}", 1);
                    }
                }
            }
        }

        return;
    };

    // 以下 xi >= 2

    let mut x1 = 0;
    let mut x2 = 0;
    let mut tmp1 = 1;
    let mut tmp2 = 1;
    cs.reverse();
    for &c in cs.iter() {
        if c == '1' {
            x1 += tmp1;
            x1 %= xi + 1;
            x2 += tmp2;
            x2 %= xi - 1;
        }

        tmp1 *= 2;
        tmp1 %= xi + 1;
        tmp2 *= 2;
        tmp2 %= xi - 1;
    }

    let mut memo: BTreeMap<u32, u32> = BTreeMap::new();
    memo.insert(0, 0);

    for i in 1..xi + 2 {
        let num = exec(i, &memo);
        memo.insert(i, num);
    }

    // println!("{:?}", memo);

    let mut del1 = 1;
    let mut del2 = 1;
    let mut ans = vec![];
    // println!("{}, {}", x1, x2);
    for &c in cs.iter() {
        let x = if c == '0' {
            // 1 にするので del1 側を使う
            (x1 + del1) % (xi + 1)
        } else {
            let tmp = if x2 < del2 {
                x2 + (xi - 1) - del2
            } else {
                x2 - del2
            };
            tmp % (xi - 1)
        };

        // println!("c = {} -> x = {}", c, x);

        let a = memo.get(&x).unwrap();
        ans.push(a + 1);

        del1 *= 2;
        del1 %= xi + 1;
        del2 *= 2;
        del2 %= xi - 1;
    }
    let l = ans.len();
    for i in 0..l {
        let idx = l - i - 1;
        println!("{}", ans[idx]);
    }
}
*/

fn main() {
    let exe = "/tmp/bin40F35CE1";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";