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 Lua (LuaJIT 2.1.0) (judge verdict: Accepted)

local r_shift = bit.rshift
local b_and = bit.band

local N = io.read("*n", "*l")
local X = io.read("*l")
local b_0 = string.byte("0")
local X_s = {}
local popcountX = 0
for i = 1, N do
	local byte = X:byte(i)
	if byte == b_0 then
		X_s[i] = 0
	else
		popcountX = popcountX + 1
		X_s[i] = 1
	end
end

local popcountXPlus1, popcountXMinus1 = popcountX + 1, popcountX - 1
--- 2^i%(Xの1の数+1)の配列(i=1,2,3,...,N)
local pow2ModPopcountXPlus1 = {}
pow2ModPopcountXPlus1[1] = 1
for i = 2, N do
	pow2ModPopcountXPlus1[i] = (pow2ModPopcountXPlus1[i-1] * 2) % popcountXPlus1
end
--- 2^i%(Xの1の数-1)の配列(i=1,2,3,...,N)
local pow2ModPopcountXMinus1 = {}
pow2ModPopcountXMinus1[1] = 1
if popcountXMinus1 > 0 then
	for i = 2, N do
		pow2ModPopcountXMinus1[i] = (pow2ModPopcountXMinus1[i-1] * 2) % popcountXMinus1
	end
end

--- X%(Xの1の数+1)
local xModPopcountXPlus1 = 0
for digit = 1, N do
	local pos = N + 1 - digit
	-- あらかじめ計算しておいた桁ごとの余りを利用してX%(Xの1の数+1)を求めていく
	if X_s[pos] == 1 then
		xModPopcountXPlus1 = xModPopcountXPlus1 + pow2ModPopcountXPlus1[digit]
		xModPopcountXPlus1 = xModPopcountXPlus1 % popcountXPlus1
	end
end
--- X%(Xの1の数-1)
local xModPopcountXMinus1 = 0
-- (Xの1の数-1)は0,-1の可能性があるからその排除
if popcountXMinus1 > 0 then
	for digit = 1, N do
		local pos = N + 1 - digit
		if X_s[pos] == 1 then
			xModPopcountXMinus1 = xModPopcountXMinus1 + pow2ModPopcountXMinus1[digit]
			xModPopcountXMinus1 = xModPopcountXMinus1 + popcountXMinus1
		end
	end
end

local function popcount(num)
	local count = 0
	while num > 0 do
		if b_and(num, 1) == 1 then
			count = count + 1
		end
		num = r_shift(num, 1)
	end
	return count
end

--- メモ化の配列
local f = {}
local function getF(num)
	if num == 0 then
		return 0
	end
	if f[num] and f[num] > 0 then
		return f[num]
	end
	local ans = 1 + getF(num % popcount(num))
	f[num] = ans
	return ans
end

for pos = 1, N do
	local digit = N + 1 - pos

	--- (Xの左からi桁目の01を反転した値)%(Xの1の数+-1)
	local modXi = 0
	if X_s[pos] == 0 then
		modXi = xModPopcountXPlus1 + pow2ModPopcountXPlus1[digit]
		modXi = modXi % popcountXPlus1
		local ans = 1 + getF(modXi)
		print(ans)
	elseif popcountXMinus1 > 0 then
		-- +popcountXMinus1はあまりをだすときにmodXiが負にならないようにする
		modXi = xModPopcountXMinus1 + (popcountXMinus1 - pow2ModPopcountXMinus1[digit])
		modXi = modXi % popcountXMinus1
		local ans = 1 + getF(modXi)
		print(ans)
	else
		print(0)
	end
end