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::*;
use std::str::FromStr;
use std::cmp;
use std::collections::HashMap;
use std::collections::HashSet;
use std::char;
use std::cmp::{min, max};
use itertools::{Itertools, enumerate};
use std::ops::{Add, AddAssign};
use num_integer::{Integer, gcd, lcm};
use std::collections::VecDeque;
use smallvec::alloc::collections::LinkedList;
use regex::internal::Char;
use std::iter::once;
use nalgebra::one;

fn read<T: FromStr>(stdin: &mut StdinLock) -> T {
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn read_tuple2<T: FromStr, S: FromStr>(stdin: &mut StdinLock) -> (T, S) {
    let a = read(stdin);
    let b = read(stdin);
    (a, b)
}

fn read_tuple3<T: FromStr, S: FromStr, R: FromStr>(stdin: &mut StdinLock) -> (T, S, R) {
    let a = read(stdin);
    let b = read(stdin);
    let c = read(stdin);
    (a, b, c)
}

fn lower_bound<T: Copy, F>(v: &Vec<T>, f: F) -> Option<usize> where
    F: Fn(T) -> bool
{
    let mut left = 0;
    let mut right = v.len() - 1;
    if !f(v[left]) {
        return None;
    }
    if f(v[right]) {
        return Some(right);
    }
    loop {
        if right - left == 1 {
            return Some(left);
        }
        let mid = (left + right) / 2;
        if f(v[mid]) {
            left = mid;
        } else {
            right = mid;
        }
    }
}

fn modPow(base:u64, time:u64, modnum:u64) -> u64{
    if time == 0 {
        1
    } else if time == 1 {
        base % modnum
    } else {
        let prev = modPow(base, time / 2, modnum);
        if time % 2 == 0 {
            prev * prev % modnum
        } else {
            prev * prev % modnum * base % modnum
        }
    }
}

fn f(num:u64) -> u64{
    let mut count = 0;
    let mut num = num;
    loop {
        if num == 0 {
            return count;
        }
        num = num % num.count_ones() as u64;
        count += 1;
    }
}

fn main() {
    // template
    let stdin = stdin();
    let mut stdin = stdin.lock();
    let out = stdout();
    let mut out = BufWriter::new(out.lock());
    // template ends
    let n:u64 = read(&mut stdin);
    let x:String = read(&mut stdin);
    let x_vec:Vec<char> = x.chars().collect_vec();
    let original_pc:u64 =  (&x_vec).iter().filter(|c| **c == '1').count() as u64;
    let pc_plus = original_pc + 1;
    let pc_minus = original_pc - 1;
    let mut total_plus = 0;
    let mut total_minus = 0;
    for i in &x_vec {
        total_plus = total_plus * 2 % pc_plus;
        if pc_minus != 0 {
            total_minus = total_minus * 2 % pc_minus;
        }
        if *i == '1' {
            total_plus += 1;
            if pc_minus != 0 {
                total_minus += 1;
            }
        }
    }
    let total_plus = total_plus % pc_plus;
    let total_minus = total_minus % pc_minus;
    for i in 0..n {
        let actual_keta = n - i;
        let init_pc = if x_vec[i as usize] == '0' { pc_plus } else { pc_minus };
        if init_pc == 0 {
            writeln!(out, "0");
            continue;
        }
        let init_total = if x_vec[i as usize] == '0' {
            (total_plus + modPow(2, actual_keta - 1, init_pc)) % init_pc
        }
        else {
            (init_pc + total_minus - modPow(2, actual_keta - 1, init_pc) ) % init_pc
        };
        writeln!(out, "{}", f(init_total) + 1);
    }
}