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)

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <vector>

// lib/quo.hpp
#ifndef QUO_H
#define QUO_H

/*
a == tools::quo(a, b) * b + tools::mod(a, b)
0 <= tools::mod(a, b) < std::abs(b)
*/

namespace tools {
  template <typename T>
  constexpr T quo(const T& lhs, const T& rhs) {
    return lhs > static_cast<T>(0)
      ? lhs / rhs
      : rhs > static_cast<T>(0)
        ? -((-lhs - 1 + rhs) / rhs)
        : (-lhs - 1 + -rhs) / -rhs;
  }
}

#endif

// lib/bitcount.hpp
#ifndef BITCOUNT_HPP
#define BITCOUNT_HPP


namespace tools {
  ::std::uint32_t bitcount(::std::uint32_t x);
  ::std::uint64_t bitcount(::std::uint64_t x);
  ::std::int32_t bitcount(::std::int32_t x);
  ::std::int64_t bitcount(::std::int64_t x);
}

#endif

// lib/util.hpp
#ifndef UTIL_HPP
#define UTIL_HPP


using i64 = ::std::int_fast64_t;
using u64 = ::std::uint_fast64_t;
using i32 = ::std::int_fast32_t;
using u32 = ::std::uint_fast32_t;

namespace tools {
  template <typename T>
  void read(::std::istream& is, ::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    vector.reserve(size);
    ::std::copy_n(::std::istream_iterator<T>(is), size, ::std::back_inserter(vector));
  }
  template <typename T>
  void read(::std::vector<T>& vector, const typename ::std::vector<T>::size_type size) {
    ::tools::read(::std::cin, vector, size);
  }

  template <typename T, ::std::size_t N>
  void read(::std::istream& is, ::std::array<T, N>& array) {
    ::std::copy_n(std::istream_iterator<T>(is), N, array.begin());
  }
  template <typename T, ::std::size_t N>
  void read(::std::array<T, N>& array) {
    ::tools::read(::std::cin, array);
  }
}

#endif

// lib/mod.hpp
#ifndef MOD_HPP
#define MOD_HPP


/*
a == tools::quo(a, b) * b + tools::mod(a, b)
0 <= tools::mod(a, b) < std::abs(b)
*/

namespace tools {
  template <typename T>
  constexpr T mod(const T& lhs, const T& rhs) {
    return lhs - ::tools::quo(lhs, rhs) * rhs;
  }
}

#endif

// lib/bitcount.cpp

namespace tools {
  ::std::uint32_t bitcount(::std::uint32_t x) {
    x = (x & static_cast<::std::uint32_t>(0x55555555ull)) + (x >> static_cast<::std::uint32_t>(1) & static_cast<::std::uint32_t>(0x55555555ull));
    x = (x & static_cast<::std::uint32_t>(0x33333333ull)) + (x >> static_cast<::std::uint32_t>(2) & static_cast<::std::uint32_t>(0x33333333ull));
    x = (x & static_cast<::std::uint32_t>(0x0f0f0f0full)) + (x >> static_cast<::std::uint32_t>(4) & static_cast<::std::uint32_t>(0x0f0f0f0full));
    x = (x & static_cast<::std::uint32_t>(0x00ff00ffull)) + (x >> static_cast<::std::uint32_t>(8) & static_cast<::std::uint32_t>(0x00ff00ffull));
    return (x & static_cast<::std::uint32_t>(0x0000ffffull)) + (x >> static_cast<::std::uint32_t>(16) & static_cast<::std::uint32_t>(0x0000ffffull));
  }

  ::std::uint64_t bitcount(::std::uint64_t x) {
    x = (x & static_cast<::std::uint64_t>(0x5555555555555555ull)) + (x >> static_cast<::std::uint64_t>(1) & static_cast<::std::uint64_t>(0x5555555555555555ull));
    x = (x & static_cast<::std::uint64_t>(0x3333333333333333ull)) + (x >> static_cast<::std::uint64_t>(2) & static_cast<::std::uint64_t>(0x3333333333333333ull));
    x = (x & static_cast<::std::uint64_t>(0x0f0f0f0f0f0f0f0full)) + (x >> static_cast<::std::uint64_t>(4) & static_cast<::std::uint64_t>(0x0f0f0f0f0f0f0f0full));
    x = (x & static_cast<::std::uint64_t>(0x00ff00ff00ff00ffull)) + (x >> static_cast<::std::uint64_t>(8) & static_cast<::std::uint64_t>(0x00ff00ff00ff00ffull));
    x = (x & static_cast<::std::uint64_t>(0x0000ffff0000ffffull)) + (x >> static_cast<::std::uint64_t>(16) & static_cast<::std::uint64_t>(0x0000ffff0000ffffull));
    return (x & static_cast<::std::uint64_t>(0x00000000ffffffffull)) + (x >> static_cast<::std::uint64_t>(32) & static_cast<::std::uint64_t>(0x00000000ffffffffull));
  }

  ::std::int32_t bitcount(::std::int32_t x) {
    return static_cast<::std::int32_t>(::tools::bitcount(static_cast<::std::uint32_t>(x)));
  }

  ::std::int64_t bitcount(::std::int64_t x) {
    return static_cast<::std::int64_t>(::tools::bitcount(static_cast<::std::uint64_t>(x)));
  }
}

// main.cpp

i64 f(const i64& n) {
  return n == 0 ? 0 : f(n % tools::bitcount(n)) + 1;
}

int main() {
  i64 N;
  std::string X;
  std::cin >> N >> X;

  const i64 popcount = std::count(X.begin(), X.end(), '1');
  if (popcount >= 2) {
    const i64 R0 = popcount + 1;

    std::vector<i64> pow2_mod_R0(N);
    pow2_mod_R0[0] = tools::mod<i64>(1, R0);
    for (i64 i = 1; i < N; ++i) {
      pow2_mod_R0[i] = tools::mod(pow2_mod_R0[i - 1] * 2, R0);
    }

    i64 X_mod_R0 = 0;
    for (i64 i = 0; i < N; ++i) {
      if (X[i] == '1') {
        X_mod_R0 = tools::mod(X_mod_R0 + pow2_mod_R0[N - i - 1], R0);
      }
    }

    const i64 R1 = popcount - 1;

    std::vector<i64> pow2_mod_R1(N);
    pow2_mod_R1[0] = tools::mod<i64>(1, R1);
    for (i64 i = 1; i < N; ++i) {
      pow2_mod_R1[i] = tools::mod(pow2_mod_R1[i - 1] * 2, R1);
    }

    i64 X_mod_R1 = 0;
    for (i64 i = 0; i < N; ++i) {
      if (X[i] == '1') {
        X_mod_R1 = tools::mod(X_mod_R1 + pow2_mod_R1[N - i - 1], R1);
      }
    }

    for (i64 i = 0; i < N; ++i) {
      std::cout << f(X[i] == '0' ? tools::mod(X_mod_R0 + pow2_mod_R0[N - i - 1], R0) : tools::mod(X_mod_R1 - pow2_mod_R1[N - i - 1], R1)) + 1 << std::endl;
    }
  } else if (popcount == 1) {
    const i64 R0 = popcount + 1;

    std::vector<i64> pow2_mod_R0(N);
    pow2_mod_R0[0] = tools::mod<i64>(1, R0);
    for (i64 i = 1; i < N; ++i) {
      pow2_mod_R0[i] = tools::mod(pow2_mod_R0[i - 1] * 2, R0);
    }

    i64 X_mod_R0 = 0;
    for (i64 i = 0; i < N; ++i) {
      if (X[i] == '1') {
        X_mod_R0 = tools::mod(X_mod_R0 + pow2_mod_R0[N - i - 1], R0);
      }
    }

    for (i64 i = 0; i < N; ++i) {
      std::cout << (X[i] == '0' ? f(tools::mod(X_mod_R0 + pow2_mod_R0[N - i - 1], R0)) + 1 : 0) << std::endl;
    }
  } else {
    for (i64 i = 0; i < N; ++i) {
      std::cout << 1 << std::endl;
    }
  }

  return 0;
}