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 <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cmath>
#include <tuple>
#include <iomanip>
#include <numeric>
#include <unordered_map>
#include <sstream>
#include<limits.h>
#include<float.h>
#include<list>
#include <array>
#include <complex>
#include<stdio.h>
#include<string.h>
#include <bitset>
// #include<assert.h>
#include<random>
using namespace std;
#define int long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
// #define MOD1 1000000007
// #define MOD1 998244353 
#define MEM_SIZE 10000
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }


//BELOW
int MOD1;
struct mint1
{
  int x; // typedef long long int;
  mint1(int x = 0) : x((x % MOD1 + MOD1) % MOD1) {}
  mint1 &operator+=(const mint1 a)
  {
    if ((x += a.x) >= MOD1)
      x -= MOD1;
    return *this;
  }
  mint1 &operator-=(const mint1 a)
  {
    if ((x += MOD1 - a.x) >= MOD1)
      x -= MOD1;
    return *this;
  }
  mint1 &operator*=(const mint1 a)
  {
    (x *= a.x) %= MOD1;
    return *this;
  }
  mint1 operator+(const mint1 a) const
  {
    mint1 res(*this);
    return res += a;
  }
  mint1 operator-(const mint1 a) const
  {
    mint1 res(*this);
    return res -= a;
  }
  mint1 operator*(const mint1 a) const
  {
    mint1 res(*this);
    return res *= a;
  }
  mint1 pow(int t) const
  {
    if (!t)
      return 1;
    mint1 a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD1
  mint1 inv() const
  {
    return pow(MOD1 - 2);
  }
  mint1 &operator/=(const mint1 a)
  {
    return (*this) *= a.inv();
  }
  mint1 operator/(const mint1 a) const
  {
    mint1 res(*this);
    return res /= a;
  }
};

int MOD2;
struct mint2
{
  int x; // typedef long long int;
  mint2(int x = 0) : x((x % MOD2 + MOD2) % MOD2) {}
  mint2 &operator+=(const mint2 a)
  {
    if ((x += a.x) >= MOD2)
      x -= MOD2;
    return *this;
  }
  mint2 &operator-=(const mint2 a)
  {
    if ((x += MOD2 - a.x) >= MOD2)
      x -= MOD2;
    return *this;
  }
  mint2 &operator*=(const mint2 a)
  {
    (x *= a.x) %= MOD2;
    return *this;
  }
  mint2 operator+(const mint2 a) const
  {
    mint2 res(*this);
    return res += a;
  }
  mint2 operator-(const mint2 a) const
  {
    mint2 res(*this);
    return res -= a;
  }
  mint2 operator*(const mint2 a) const
  {
    mint2 res(*this);
    return res *= a;
  }
  mint2 pow(int t) const
  {
    if (!t)
      return 1;
    mint2 a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD2
  mint2 inv() const
  {
    return pow(MOD2 - 2);
  }
  mint2 &operator/=(const mint2 a)
  {
    return (*this) *= a.inv();
  }
  mint2 operator/(const mint2 a) const
  {
    mint2 res(*this);
    return res /= a;
  }
};


void solve(void)
{
  int N;cin>>N;
  
  string str;cin>>str;
  reverse(ALL(str));
  int cnt = 0;
  int M = 200010;
  vector<int> table (M,-1);
  table[0] = 0;
  for (int i = 1; i < M; i++)
  {
    int p = __builtin_popcount(i);
    table[i] = table[i%p]+1;
  }
  


  for (int i = 0; i < N; i++)
  {
    if(str[i] ==  '1')cnt++;
  }

  MOD1 = cnt-1;
  MOD2 = cnt+1;
  if(MOD1 == 0)MOD1 = 1e9+7;
  mint1 Q1 = 0;
  mint2 Q2 = 0;
  for (int i = 0; i < N; i++)
  {
    mint1 X1 = 2;
    mint2 X2 = 2;

    if(str[i] == '1')
    {
      X1 = X1.pow(i);
      X2 = X2.pow(i);
      Q1 += X1;
      Q2 += X2;
    }
  }
  
  
  for (int i = N-1; i >= 0; i--)
  {
    if(str[i] == '0')
    {
      mint2 X = 2;
      X = X.pow(i);
      mint2 Z = Q2 + X;
      cout<< table[Z.x] + 1<<endl;
    }
    else
    {
      if(MOD1 == 1e9+7)
      {
        cout<<0<<endl;
        continue;
      }
      mint1 X = 2;
      X = X.pow(i);
      mint1 Z = Q1 - X;
      cout<< table[Z.x] + 1<<endl;
    }
  }
  

  


  return; 
}

int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}