Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int nmax = 200005;
vector<int> res(nmax);
vector<bool> mem(nmax);
ll f(int x) {
  if (mem[x]) {
    return res[x];
  }
  if (x == 0) {
    mem[x] = true;
    return res[x] = 0;
  }
  int num = x;
  int cnt = 0;
  while (num) {
    if (num & 1) {
      cnt++;
    }
    num /= 2;
  }
  int p = x % cnt;
  mem[x] = true;
  return res[x] = 1 + f(p);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  string x;
  cin >> x;
  int pop = 0;
  REP(i, n) {
    if (x[i] == '1') {
      pop++;
    }
  }
  if (pop == 1) {
    vector<int> t(n);
    t[0] = 1;
    FOR(i, 1, n) { t[i] = t[i - 1] * 2 % (pop + 1); }
    int v = 0;
    REP(i, n) {
      if (x[i] == '1') {
        (v += t[n - 1 - i]) %= (pop + 1);
      }
    }
    REP(i, n) {
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        int k = (v + pop + 1 + t[n - 1 - i]) % (pop + 1);
        cout << 1 + f(k) << endl;
      }
    }
  } else {
    vector<int> s(n), t(n);
    s[0] = 1;
    t[0] = 1;
    FOR(i, 1, n) {
      s[i] = s[i - 1] * 2 % (pop - 1);
      t[i] = t[i - 1] * 2 % (pop + 1);
    }
    int u = 0, v = 0;
    REP(i, n) {
      if (x[i] == '1') {
        (u += s[n - 1 - i]) %= (pop - 1);
        (v += t[n - 1 - i]) %= (pop + 1);
      }
    }
    REP(i, n) {
      if (x[i] == '1') {
        int k = (u + pop - 1 - s[n - 1 - i]) % (pop - 1);
        cout << 1 + f(k) << endl;
      } else {
        int k = (v + pop + 1 + t[n - 1 - i]) % (pop + 1);
        cout << 1 + f(k) << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(), (v).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1LL << 60;
const int inf = (1 << 30) - 1;
const int mod = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
const int nmax = 200005;
vector<int> res(nmax);
vector<bool> mem(nmax);
ll f(int x) {
  if (mem[x]) {
    return res[x];
  }
  if (x == 0) {
    mem[x] = true;
    return res[x] = 0;
  }
  int num = x;
  int cnt = 0;
  while (num) {
    if (num & 1) {
      cnt++;
    }
    num /= 2;
  }
  int p = x % cnt;
  mem[x] = true;
  return res[x] = 1 + f(p);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  string x;
  cin >> x;
  int pop = 0;
  REP(i, n) {
    if (x[i] == '1') {
      pop++;
    }
  }
  vector<int> s(n), t(n);
  s[0] = 1;
  t[0] = 1;
  FOR(i, 1, n) {
    s[i] = s[i - 1] * 2 % (pop - 1);
    t[i] = t[i - 1] * 2 % (pop + 1);
  }
  int u = 0, v = 0;
  REP(i, n) {
    if (x[i] == '1') {
      (u += s[n - 1 - i]) %= (pop - 1);
      (v += t[n - 1 - i]) %= (pop + 1);
    }
  }
  REP(i, n) {
    if (x[i] == '1') {
      int k = (u + pop - 1 - s[n - 1 - i]) % (pop - 1);
      cout << 1 + f(k) << endl;
    } else {
      int k = (v + pop + 1 + t[n - 1 - i]) % (pop + 1);
      cout << 1 + f(k) << endl;
    }
  }
}