Language: C++

#pragma region
#pragma GCC target("avx2")
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std; typedef long double ld; typedef long long ll;
typedef unsigned long long ull;
#define endl "\n"
#define MP make_pair
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORR(x,arr) for(auto& x:arr)
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define VPII vector<PII>
#define VPLL vector<PLL>
#define FI first 
#define SE second
#define ALL(x) (x).begin(), (x).end()
constexpr int INF=1<<30; constexpr ll LINF=1LL<<60; constexpr ll mod=1e9+7; constexpr int NIL = -1;
template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
#pragma endregion
//-------------------
int table[200000+7];

int main(){
    cin.tie(0); ios::sync_with_stdio(false); //cout << fixed << setprecision(15);
    int n; string s; cin >> n >> s;
    FOR(i,1,200000) {
        int cur = i;
        int ccnt = 0;
        while(cur>0) {
            ccnt += 1;
            cur %= __builtin_popcount(cur);
        }
        table[i] = ccnt;
    }

    // FOR(i,1,10) cout << table[i] << endl;
    
    int cnt = 0;
    FOR(i,0,n-1){
        if(s[i] == '1') cnt++;
    }

    int MX = 200000;
    int m0[MX], m2[MX];

    if(cnt-1 == 1) {
        m0[0] = 0;
    }else {
        m0[0] = 1;
    }
    FOR(i,1,MX) {
        m0[i] = (m0[i-1]*2)%(cnt-1);
    }
    m2[0] = 1;
    FOR(i,1,MX) {
        m2[i] = (m2[i-1]*2)%(cnt+1);
    }
    // FOR(i,0,3) cout << m2[i] << " ";

    int mm0=0, mm2=0;
    FOR(i,0,n-1) {
        if(s[i] == '1') {
            mm0 = (mm0+m0[n-i-1])%(cnt-1);
        }
    }
    FOR(i,0,n-1) {
        if(s[i] == '1') {
            // cout << m2[n-i-1] << " ";
            mm2 = (mm2+m2[n-i-1])%(cnt+1);
        }
    }
    // cout << mm0 << endl;
    // cout << mm2 << endl;
    FOR(i,0,n-1) {
        if(s[i] == '0') {
            // cout << mm2 << endl;
            int next = (mm2 + m2[n-i-1]) % (cnt+1);
            // cout << next << endl;
            cout << 1 + table[next] << endl;
        }
        else {
            if(cnt == 1) {
                cout << 0 << endl;
                continue;
            }
            int next = (mm0 - m0[n-i-1] + cnt-1) % (cnt-1);
            cout << 1 + table[next] << endl;
        }
        
    }

    return 0;
}


Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:5:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::__cxx11::basic_string<char>::_Alloc_hider::~_Alloc_hider()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = char]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:186:14: note: called from here
  186 |       struct _Alloc_hider : allocator_type // TODO check __is_final
      |              ^~~~~~~~~~~~