Language: C++

#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()

typedef long long lint;

using namespace std;

int main(){
  int N;
  cin>>N;
  string X;
  cin>>X;
  int count=0;
  for(auto c:X)if(c=='1')count++;
  int C[3]={count-1,count,count+1};
  lint pow2[3][N+1];
  pow2[0][0]=1;
  pow2[1][0]=1;
  pow2[2][0]=1;
  REP(i,N){
    REP(j,3)if(C[j]!=0){
      pow2[j][i+1]=pow2[j][i]*2;
      pow2[j][i+1]%=C[j];
    }
  }
  REP(i,N)cerr<<pow2[2][i]<<" ";
  cerr<<endl;
  lint A[3]={0};
  REP(i,N){
    if(X[i]=='1'){
      REP(j,3)if(C[j]!=0){
        A[j]+=pow2[j][N-i-1];
        A[j]%=C[j];
      }
    }
  }
  cerr<<bitset<5>(A[0])<<" "<<bitset<5>(A[1])<<" "<<bitset<5>(A[2])<<endl;
  vector<int> ans(N,0);
  REP(i,N){
    int temp=1;
    lint Y,p;
    if(X[i]=='0'){
      p=C[2];
      Y=A[2];
      Y+=pow2[2][N-i-1];
      Y%=p;
    }else{
      p=C[0];
      if(p==0){
        ans[i]=0;
        continue;
      }
      Y=A[0];
      Y-=pow2[0][N-i-1];
      Y%=p;
      if(Y<0)Y+=p;
    }
    cerr<<bitset<5>(Y)<<" "<<p<<endl;
    while(Y>0){
      lint YC=Y;
      p=0;
      while(YC>0){
        p+=YC%2;
        YC/=2;
      }
      Y%=p;
      temp++;
    }
    ans[i]=temp;
  }
  for(auto e:ans)cout<<e<<endl;
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:49:9: error: 'bitset' was not declared in this scope
   49 |   cerr<<bitset<5>(A[0])<<" "<<bitset<5>(A[1])<<" "<<bitset<5>(A[2])<<endl;
      |         ^~~~~~
a.cc:11:1: note: 'std::bitset' is defined in header '<bitset>'; this is probably fixable by adding '#include <bitset>'
   10 | #include <string.h>
  +++ |+#include <bitset>
   11 | 
a.cc:49:24: error: invalid operands of types 'lint' {aka 'long long int'} and 'const char [2]' to binary 'operator<<'
   49 |   cerr<<bitset<5>(A[0])<<" "<<bitset<5>(A[1])<<" "<<bitset<5>(A[2])<<endl;
      |                  ~~~~~~^~~~~
      |                      |   |
      |                      |   const char [2]
      |                      lint {aka long long int}
a.cc:49:46: error: invalid operands of types 'lint' {aka 'long long int'} and 'const char [2]' to binary 'operator<<'
   49 |   cerr<<bitset<5>(A[0])<<" "<<bitset<5>(A[1])<<" "<<bitset<5>(A[2])<<endl;
      |                                        ~~~~~~^~~~~
      |                                            |   |
      |                                            |   const char [2]
      |                                            lint {aka long long int}
a.cc:49:68: error: invalid operands of types 'lint' {aka 'long long int'} and '<unresolved overloaded function type>' to binary 'operator<<'
   49 |   cerr<<bitset<5>(A[0])<<" "<<bitset<5>(A[1])<<" "<<bitset<5>(A[2])<<endl;
      |                                                              ~~~~~~^~~~~~
a.cc:70:23: error: invalid operands of types 'lint' {aka 'long long int'} and 'const char [2]' to binary 'operator<<'
   70 |     cerr<<bitset<5>(Y)<<" "<<p<<endl;
      |                    ~~~^~~~~
      |                     |   |
      |                     |   const char [2]
      |                     lint {aka long long int}