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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n;
string s;

ll pc(ll a) {
  ll res = 0;
  while (a > 0) {
    if (a % 2 == 1)
      res++;
    a /= 2;
  }
  return res;
}

ll u, d, nowd[210000], nowu[210000];
int main() {
  cin >> n >> s;
  ll npc = 0;
  REP(i, n) {
    if (s[i] == '1')
      npc++;
  }
  nowu[n - 1] = 1;
  nowd[n - 1] = 1;
  if (npc != 1) {
    nowd[n - 1] %= npc - 1;
    REPR(i, n - 1) {
      if (s[i] == '1')
        d += nowd[i];
      d %= (npc - 1);
      if (i > 0)
        nowd[i - 1] = nowd[i] * 2 % (npc - 1);
    }
  }
  nowu[n - 1] %= npc + 1;
  REPR(i, n - 1) {
    if (s[i] == '1')
      u += nowu[i];
    u %= (npc + 1);
    if (i > 0)
      nowu[i - 1] = nowu[i] * 2 % (npc + 1);
  }
  REP(i, n) {
    if (s[i] == '1') {
      if (npc == 1)
        cout << 0 << endl;
      else {
        ll cnt = 1, mod = npc - 1;
        ll now = (d - nowd[i] + mod) % mod;
        while (now != 0) {
          now %= pc(now);
          cnt++;
        }
        cout << cnt << endl;
      }
    } else {
      ll cnt = 1, mod = npc + 1;
      ll now = (u + nowu[i] + mod) % mod;
      // cout << i << " " << nowu[i] << " " << u << " " << mod << endl;
      while (now != 0) {
        now %= pc(now);
        cnt++;
      }
      cout << cnt << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n;
string s;

ll pc(ll a) {
  ll res = 0;
  while (a > 0) {
    if (a % 2 == 1)
      res++;
    a /= 2;
  }
  return res;
}

ll u, d, nowd[210000], nowu[210000];
int main() {
  cin >> n >> s;
  ll npc = 0;
  REP(i, n) {
    if (s[i] == '1')
      npc++;
  }
  nowu[n - 1] = 1;
  nowd[n - 1] = 1;
  if (npc != 1) {
    nowd[n - 1] %= npc - 1;
    REPR(i, n - 1) {
      if (s[i] == '1')
        d += nowd[i];
      d %= (npc - 1);
      if (i > 0)
        nowd[i - 1] = nowd[i] * 2 % (npc - 1);
    }
  }
  nowu[n - 1] %= npc + 1;
  REPR(i, n - 1) {
    if (s[i] == '1')
      u += nowu[i];
    u %= (npc + 1);
    if (i > 0)
      nowu[i - 1] = nowu[i] * 2 % (npc + 1);
  }
  REP(i, n) {
    if (s[i] == '1') {
      if (npc == 1)
        cout << 0 << endl;
      ll cnt = 1, mod = npc - 1;
      ll now = (d - nowd[i] + mod) % mod;
      while (now != 0) {
        now %= pc(now);
        cnt++;
      }
      cout << cnt << endl;
    } else {
      ll cnt = 1, mod = npc + 1;
      ll now = (u + nowu[i] + mod) % mod;
      // cout << i << " " << nowu[i] << " " << u << " " << mod << endl;
      while (now != 0) {
        now %= pc(now);
        cnt++;
      }
      cout << cnt << endl;
    }
  }
}