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 C++ (GCC 9.2.1) (judge verdict: Accepted)

#ifndef __SETS_H__
#define __SETS_H__

/* updated: 2020-03-28 */

// 集合系の関数などを置きたい

#include <cstdint>
#include <vector>
#include <cassert>

#ifdef _MSC_VER
#include <intrin.h>
#endif

// ビット演算系

/* 立っているビットの数を数える */
inline int popcount(uint64_t value) {
#ifdef _MSC_VER
	return __popcnt64(value);
#elif defined(__GNUC__)
	return __builtin_popcountll(value);
#else
	value = (value & 0x5555555555555555) + ((value >>  1) & 0x5555555555555555);
	value = (value & 0x3333333333333333) + ((value >>  2) & 0x3333333333333333);
	value = (value & 0x0f0f0f0f0f0f0f0f) + ((value >>  4) & 0x0f0f0f0f0f0f0f0f);
	value = (value & 0x00ff00ff00ff00ff) + ((value >>  8) & 0x00ff00ff00ff00ff);
	value = (value & 0x0000ffff0000ffff) + ((value >> 16) & 0x0000ffff0000ffff);
	value = (value & 0x00000000ffffffff) + ((value >> 32) & 0x00000000ffffffff);
	return value;
#endif
}

/* 最上位ビットの位置を返す (0-based, value == 0 のときは -1 を返す) */
/* floor(log2(value)) と一致する */
inline int msbPosition(uint64_t value) {
	value |= value >> 1;
	value |= value >> 2;
	value |= value >> 4;
	value |= value >> 8;
	value |= value >> 16;
	value |= value >> 32;
	return popcount(value) - 1;
}

/* 最下位ビットの位置を返す (0-based, value == 0 のときは 64 を返す) */
inline int lsbPosition(uint64_t value) {
	value |= value << 1;
	value |= value << 2;
	value |= value << 4;
	value |= value << 8;
	value |= value << 16;
	value |= value << 32;
	return 64 - popcount(value);
}

/* 最上位ビットを取得 (value == 0 のときは 0 を返す) */
inline uint64_t msb(uint64_t value) {
	if (value == 0) return uint64_t();
	return uint64_t(1) << msbPosition(value);
}

/* 最下位ビットを取得 (value == 0 のときは 0 を返す) */
/* TODO: これに関しては明らかに value & -value のほうが速いと思うけどどうしようか */
inline uint64_t lsb(uint64_t value) {
	if (value == 0) return uint64_t();
	return uint64_t(1) << lsbPosition(value);
}

/* ceil(log2(value)) を返す (value == 0 のときは -1 を返す) */
/* 戻り値の範囲は -1 <= ret <= 64 */
inline int ceilLog2(uint64_t value) {
	if (value == 0) return -1;
	return msbPosition(value - 1) + 1;
}

/* ビットの並びを逆転させる */
inline uint64_t bitReverse(uint64_t val) {
	val = (val & 0x5555555555555555) <<  1 | (val & 0xAAAAAAAAAAAAAAAA) >>  1;
	val = (val & 0x3333333333333333) <<  2 | (val & 0xCCCCCCCCCCCCCCCC) >>  2;
	val = (val & 0x0F0F0F0F0F0F0F0F) <<  4 | (val & 0xF0F0F0F0F0F0F0F0) >>  4;
	val = (val & 0x00FF00FF00FF00FF) <<  8 | (val & 0xFF00FF00FF00FF00) >>  8;
	val = (val & 0x0000FFFF0000FFFF) << 16 | (val & 0xFFFF0000FFFF0000) >> 16;
	val = (val & 0x00000000FFFFFFFF) << 32 | (val & 0xFFFFFFFF00000000) >> 32;
	return val;
}

/* 下位 digits ビットについてビットの並びを逆転させる (上位ビットは無視) */
inline uint64_t bitReverse(uint64_t val, int digits) {
	assert(digits > 0 || digits <= 64);
	return bitReverse(val) >> (64 - digits);
}

/* 1 << quantity を返す */
inline uint64_t shift(int quantity) {
	return uint64_t(1) << quantity;
}

/* 整数 sets を集合としてみたとき、element 番目の要素を持つかどうか bool(s & (1 << element)) */
inline bool has(uint64_t sets, int element) {
	return sets & shift(element);
}

template <class T>
void bitReverseArray(std::vector<T>& arr);

/* 配列の要素を添字のビット逆順に並べ替える (配列長は2冪) */
template <class T>
void bitReverseArray(std::vector<T>& arr) {
	const int rank = msbPosition(arr.size());
	const int length = 1 << rank;
	assert(arr.size() == length);

	for (int i = 0; i < length; ++i) {
		int j = bitReverse(i, rank);
		if (j > i) {
			std::swap(arr[i], arr[j]);
		}
	}
}


// 畳み込み演算系

template <class T>
void transformSubsetZeta(std::vector<T>& arr);
template <class T>
void transformInverseSubsetZeta(std::vector<T>& arr);
template <class T>
void transformSupersetZeta(std::vector<T>& arr);
template <class T>
void transformInverseSupersetZeta(std::vector<T>& arr);
template <class T>
void transformHadamard(std::vector<T>& vec);
template <class T>
std::vector<T> convolveOnXor(const std::vector<T>& a, const std::vector<T>& b);

/* 逆変換とまとめられないか？ */
/* 高速ゼータ変換 O(n log n), n は2冪 */
/* b[s] = sum[t is a subset of s] a[t] */
template <class T>
void transformSubsetZeta(std::vector<T>& arr) {
	const uint64_t length = arr.size();
	const int level = msbPosition(length);
	assert(length == shift(level));

	for (int k = 0; k < level; ++k) {
		for (uint64_t s = 0; s < length; ++s) {
			if (has(s, k)) continue;
			arr[s] += arr[s | shift(k)];
		}
	}
}

/* 高速ゼータ変換の逆変換 O(n log n), n は2冪 */
template <class T>
void transformInverseSubsetZeta(std::vector<T>& arr) {
	const uint64_t length = arr.size();
	const int level = msbPosition(length);
	assert(length == (uint64_t(1) << level));

	for (int k = 0; k < level; ++k) {
		for (uint64_t s = 0; s < length; ++s) {
			if (has(s, k)) continue;
			arr[s | shift(k)] -= arr[s];
		}
	}
}

/* 高速(上位集合)ゼータ変換 O(n log n), n は2冪 */
/* b[s] = sum[t is a superset of s] a[t] */
template <class T>
void transformSupersetZeta(std::vector<T>& arr) {
	const uint64_t length = arr.size();
	const int level = msbPosition(length);
	assert(length == (uint64_t(1) << level));

	for (int k = 0; k < level; ++k) {
		for (uint64_t s = 0; s < length; ++s) {
			if (has(s, k)) continue;
			arr[s | shift(k)] += arr[s];
		}
	}
}

/* 高速(上位集合)ゼータ変換の逆変換 O(n log n), n は2冪 */
template <class T>
void transformInverseSupersetZeta(std::vector<T>& arr) {
	const uint64_t length = arr.size();
	const int level = msbPosition(length);
	assert(length == (uint64_t(1) << level));

	for (int k = 0; k < level; ++k) {
		for (uint64_t s = 0; s < length; ++s) {
			if (has(s, k)) continue;
			arr[s] -= arr[s | shift(k)];
		}
	}
}

/* 高速アダマール変換 O(n log n), n は2冪 (オーバーフロー注意) */
/* オーバーフロー注意 */
template <class T>
void transformHadamard(std::vector<T>& vec) {
	const int rank = msbPosition(vec.size());
	const int length = 1 << rank;
	assert(vec.size() == length);

	/* バタフライ演算 */
	auto butterfly = [](T& a, T& b) -> void {
		T tmp(a);
		a += b;
		b = tmp - b;
	};

	/* FFT 同様に各ビットについてバタフライ演算を回す */
	for (int cnt = 0; cnt < rank; ++cnt) {
		const int blk_max = length >> (cnt + 1);
		const int blk_size = 1 << (cnt + 1);

		for (int blk = 0; blk < blk_max; ++blk) {
			const int idx_max = 1 << cnt;

			for (int idx = 0; idx < idx_max; ++idx) {
				const int left = (blk * blk_size + idx);
				const int right = left + blk_size / 2;

				butterfly(vec[left], vec[right]);
			}
		}
	}
}

/* xor 畳み込み O(n log n), n は2冪 (オーバーフロー注意) */
/* ret[n] = sum [i xor j == n] (a[i] * b[j]) */
template <class T>
std::vector<T> convolveOnXor(const std::vector<T>& a, const std::vector<T>& b) {
	const int rank = msbPosition(a.size());
	const int size = 1 << rank;
	assert(a.size() == b.size());
	assert(a.size() == size);

	std::vector<T> a_copied = a;
	std::vector<T> b_copied = b;
	transformHadamard(a_copied);
	transformHadamard(b_copied);

	std::vector<T> ret(size);
	for (int i = 0; i < size; ++i) {
		ret[i] = a_copied[i] * b_copied[i];
	}
	transformHadamard(ret);

	/* 関数 transformHadamard はユニタリ変換ではないので係数を調整 */
	/* IntMod など -> 逆元を前計算することも可能 */
	for (int i = 0; i < size; ++i) {
		ret[i] /= size;
	}
	return ret;
}



#endif

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "Interpolation.h"
#include "IntMod.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

vector<int> sub(int n, int mod) {
	vector<int> ret(n);
	if (mod <= 0) return ret;
	int base = 1 % mod;
	REP(i, 0, n) {
		ret[i] = base;
		base = base * 2 % mod;
	}
	return ret;
}

int cat(int curr) {
	int cnt = 1;
	while (curr > 0) {
		curr %= popcount(curr);
		++cnt;
	}
	return cnt;
}

int main() {
	int N;
	string S;
	cin >> N >> S;
	reverse(S.begin(), S.end());
	
	int cnt = 0;
	for (char c : S) cnt += c - '0';
	auto P = sub(N, cnt - 1);
	auto Q = sub(N, cnt + 1);
	int x = 0;
	int y = 0;

	REP(i, 0, N) {
		if (cnt > 1) x = (x + P[i] * (S[i] - '0')) % (cnt - 1);
		y = (y + Q[i] * (S[i] - '0')) % (cnt + 1);
	}

	REM(i, 0, N) {
		if (S[i] == '0') {
			int mod = cnt + 1;
			int curr = (y + mod + Q[i]) % mod;
			cout << cat(curr) << endl;
		} else {
			if (cnt == 1) {
				cout << 0 << endl;
			} else {
				int mod = cnt - 1;
				int curr = (x + mod - P[i]) % mod;
				cout << cat(curr) << endl;
			}
		}
	}

	return 0;
}