Problem p02609 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define all(x) (x).begin(), (x).end()
typedef long long ll;

using namespace std;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<int> vu(N), vd(N), u(N), d(N);
  int mod, modu, modd, uu, dd;
  mod = bitset<200000>(X).count();
  modu = mod + 1;
  modd = mod - 1;

  if (modd == 0) {
    for (int i = 0; i < N - 1; i++) {
      if (X[N - 1] == '1')
        cout << 2 << endl;
      else if (X[i] == '1')
        cout << 0 << endl;
      else
        cout << 1 << endl;
    }
    if (X[N - 1] == '1')
      cout << 0 << endl;
    else
      cout << 2 << endl;
  } else {
    int tmp, tmpu, tmpd;
    tmpu = 1;
    tmpd = 1;
    u[N - 1] = 1;
    d[N - 1] = 1;
    if (X[N - 1] == '1') {
      vu[0] = 1;
      vd[0] = 1;
    } else {
      vu[0] = 0;
      vd[0] = 0;
    }
    for (int i = N - 2; i >= 0; i--) {
      tmpu = tmpu * 2 % modu;
      tmpd = tmpd * 2 % modd;
      if (X[i] == '1') {
        vu[N - 1 - i] = (vu[N - 2 - i] + tmpu) % modu;
        vd[N - 1 - i] = (vd[N - 2 - i] + tmpd) % modd;
      } else {
        vu[N - 1 - i] = vu[N - 2 - i];
        vd[N - 1 - i] = vd[N - 2 - i];
      }
      u[i] = tmpu;
      d[i] = tmpd;
    }
    uu = vu[N - 1];
    dd = vd[N - 1];

    int ans;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        tmp = (dd + modd - d[i]) % modd;
      } else {
        tmp = (uu + u[i]) % modu;
      }
      ans = 1;
      while (bitset<200000>(tmp).count() > 0) {
        tmp = tmp % bitset<200000>(tmp).count();
        ans++;
      }
      cout << ans << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define all(x) (x).begin(), (x).end()
typedef long long ll;

using namespace std;

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<int> vu(N), vd(N), u(N), d(N);
  int mod, modu, modd, uu, dd;
  mod = bitset<200000>(X).count();
  modu = mod + 1;
  modd = mod - 1;

  if (modd == 0) {
    for (int i = 0; i < N - 1; i++) {
      if (X[N - 1] == '1')
        cout << 2 << endl;
      else if (X[i] == '1')
        cout << 0 << endl;
      else
        cout << 1 << endl;
    }
    if (X[N - 1] == '1')
      cout << 0 << endl;
    else
      cout << 2 << endl;
  } else {
    int tmp, tmpu, tmpd;
    tmpu = 1;
    tmpd = 1;
    u[N - 1] = 1;
    d[N - 1] = 1;
    if (X[N - 1] == '1') {
      vu[0] = 1;
      vd[0] = 1;
    } else {
      vu[0] = 0;
      vd[0] = 0;
    }
    for (int i = N - 2; i >= 0; i--) {
      tmpu = tmpu * 2 % modu;
      tmpd = tmpd * 2 % modd;
      if (X[i] == '1') {
        vu[N - 1 - i] = (vu[N - 2 - i] + tmpu) % modu;
        vd[N - 1 - i] = (vd[N - 2 - i] + tmpd) % modd;
      } else {
        vu[N - 1 - i] = vu[N - 2 - i];
        vd[N - 1 - i] = vd[N - 2 - i];
      }
      u[i] = tmpu;
      d[i] = tmpd;
    }
    uu = vu[N - 1];
    dd = vd[N - 1];

    int ans;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        tmp = (dd + modd - d[i]) % modd;
      } else {
        tmp = (uu + u[i]) % modu;
      }
      ans = 1;
      while (__builtin_popcount(tmp) > 0) {
        tmp = tmp % __builtin_popcount(tmp);
        ans++;
      }
      cout << ans << endl;
    }
  }
}