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 Go (1.14.1) (judge verdict: Runtime Error)

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func main() {
	sc := NewScanner()

	n := sc.ReadInt()
	x := sc.ReadString()

	popcountX := 0
	for _, c := range x {
		if c == '1' {
			popcountX++
		}
	}
	popcountXPlus1 := popcountX + 1
	popcountXMinus1 := popcountX - 1

	pow2ModPopcountXPlus1 := make([]int, n)
	pow2ModPopcountXPlus1[0] = 1
	for i := 1; i < n; i++ {
		pow2ModPopcountXPlus1[i] = pow2ModPopcountXPlus1[i-1] * 2 % popcountXPlus1
	}

	pow2ModPopcountXMinus1 := make([]int, n)
	pow2ModPopcountXMinus1[0] = 1
	if popcountXMinus1 >= 0 {
		for i := 1; i < n; i++ {
			pow2ModPopcountXMinus1[i] = pow2ModPopcountXMinus1[i-1] * 2 % popcountXMinus1
		}
	}

	xModPopcountXPlus1 := 0
	for digit := 0; digit < n; digit++ {
		pos := n - 1 - digit
		if x[pos] == '1' {
			xModPopcountXPlus1 += pow2ModPopcountXPlus1[digit]
			xModPopcountXPlus1 %= popcountXPlus1
		}
	}

	xModPopcountXMinus1 := 0
	if popcountXMinus1 >= 0 {
		for digit := 0; digit < n; digit++ {
			pos := n - 1 - digit
			if x[pos] == '1' {
				xModPopcountXMinus1 += pow2ModPopcountXMinus1[digit]
				xModPopcountXMinus1 %= popcountXMinus1
			}
		}
	}

	f := make([]int, 200_000)
	var getF func(n int) int
	getF = func(n int) int {
		if n == 0 {
			return 0
		}
		if f[n] > 0 {
			return f[n]
		}
		ans := 1 + getF(n%popcount(n))
		f[n] = ans
		return ans
	}

	for pos := 0; pos < n; pos++ {
		digit := n - 1 - pos

		modXi := 0
		if x[pos] == '0' {
			modXi = xModPopcountXPlus1 + pow2ModPopcountXPlus1[digit]
			modXi %= popcountXPlus1
		} else {
			modXi = xModPopcountXMinus1 + (popcountXMinus1 - pow2ModPopcountXMinus1[digit])
			modXi %= popcountXMinus1
		}
		ans := 1 + getF(modXi)
		fmt.Println(ans)
	}
}

func popcount(n int) int {
	cnt := 0
	for ; n > 0; n >>= 1 {
		if n&1 == 1 {
			cnt++
		}
	}
	return cnt
}

// from [my library](https://github.com/ikngtty/go-contestlib)
// -   math/simple

func Abs(x int) int {
	if x >= 0 {
		return x
	}
	return -x
}

func Min(values ...int) int {
	if len(values) == 0 {
		panic("no values")
	}

	min := values[0]
	for _, v := range values {
		if v < min {
			min = v
		}
	}
	return min
}

func Max(values ...int) int {
	if len(values) == 0 {
		panic("no values")
	}

	max := values[0]
	for _, v := range values {
		if v > max {
			max = v
		}
	}
	return max
}

func Pow(base, exponent int) int {
	if exponent < 0 {
		panic(fmt.Sprintf("exponent (%d) should not be a minus", exponent))
	}

	answer := 1
	for i := 0; i < exponent; i++ {
		answer *= base
	}
	return answer
}

func Ceil(divident, dividor int) int {
	if dividor == 0 {
		panic("dividor should not be 0")
	}

	quo := divident / dividor
	rem := divident % dividor

	if rem != 0 {
		if (divident > 0 && dividor > 0) ||
			(divident < 0 && dividor < 0) {
			return quo + 1
		}
	}
	return quo
}

// -   sortutil

func ReverseInts(a []int) {
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
}

func ReverseStrings(a []string) {
	sort.Sort(sort.Reverse(sort.StringSlice(a)))
}

// -   io

type Scanner struct {
	bufScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bufSc := bufio.NewScanner(os.Stdin)
	bufSc.Split(bufio.ScanWords)
	bufSc.Buffer(nil, 100000000)
	return &Scanner{bufSc}
}

func (sc *Scanner) ReadString() string {
	bufSc := sc.bufScanner
	bufSc.Scan()
	return bufSc.Text()
}

func (sc *Scanner) ReadInt() int {
	bufSc := sc.bufScanner
	bufSc.Scan()
	text := bufSc.Text()

	num, err := strconv.Atoi(text)
	if err != nil {
		panic(err)
	}
	return num
}