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

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
const int N_MAX = 202;

ll popcnt(string S) {
  int N = S.length();
  int cnt = 0;
  REP(i, N) {
    if (S[i] == '1')
      cnt++;
  }
  return cnt;
}

ll firstcomp(string S, ll m) {
  if (m == -1)
    return 0;
  if (m == 0)
    return 0;

  int N = S.length();
  ll Bs = 1;
  ll ret = 0;
  REPr(i, N) {
    if (S[i] == '1')
      ret += Bs % m;
    ret %= m;
    Bs *= 2;
    Bs %= m;
  }
  return ret % m;
}

ll secondcomp(ll x) {
  ll ret = 0;
  while (__builtin_popcountll(x) != 0) {
    x = x % __builtin_popcountll(x);
    ret++;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll pcnt = popcnt(S);

  ll cnt0 = firstcomp(S, popcnt(S));
  ll cnt1 = firstcomp(S, popcnt(S) + 1);
  ll cnt2 = firstcomp(S, popcnt(S) - 1);

  vector<ll> bin1(N + 3), bin2(N + 3);
  bin1[0] = 1 % (pcnt + 1);
  if (pcnt != 1)
    bin2[0] = 1 % (pcnt - 1);
  for (int i = 1; i <= N + 1; i++) {
    bin1[i] = (bin1[i - 1] * 2) % (pcnt + 1);
    if (pcnt != 1)
      bin2[i] = (bin2[i - 1] * 2) % (pcnt - 1);
  }

  REP(i, N) {
    if (S[i] == '0') {
      cout << 1 + secondcomp((cnt1 + (bin1[N - i - 1])) % (pcnt + 1)) << "\\n";
    } else {
      // cout << "cnt2 = " << cnt2 << "\\n";
      // cout << "bin2[N-i-1] = " << bin2[N - i - 1] << "\\n";
      if (pcnt == 1) {
        cout << 0 << "\\n";
        continue;
      }

      ll secin = ((cnt2 - (bin2[N - i - 1])) + (pcnt - 1)) % (pcnt - 1);
      cout << 1 + secondcomp(secin) << "\\n";
    }
  }
}

Error: 0

Fixed submission:

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPr(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for (int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for (int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \\
  cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\\n";
#define DEBUG                                                                  \\
  int x12345;                                                                  \\
  cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
const int N_MAX = 202;

ll popcnt(string S) {
  int N = S.length();
  int cnt = 0;
  REP(i, N) {
    if (S[i] == '1')
      cnt++;
  }
  return cnt;
}

ll firstcomp(string S, ll m) {
  if (m == -1)
    return -1;
  int N = S.length();
  ll Bs = 1;
  ll ret = 0;
  REPr(i, N) {
    if (S[i] == '1')
      ret += Bs % m;
    ret %= m;
    Bs *= 2;
    Bs %= m;
  }
  return ret % m;
}

ll secondcomp(ll x) {
  ll ret = 0;
  while (__builtin_popcountll(x) != 0) {
    x = x % __builtin_popcountll(x);
    ret++;
  }
  return ret;
}

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  ll cnt0 = firstcomp(S, popcnt(S));
  ll cnt1 = firstcomp(S, popcnt(S) + 1);
  ll cnt2 = firstcomp(S, popcnt(S) - 1);

  ll pcnt = popcnt(S);

  vector<ll> bin1(N + 3), bin2(N + 3);
  bin1[0] = 1 % (pcnt + 1);
  if (pcnt != 1)
    bin2[0] = 1 % (pcnt - 1);
  for (int i = 1; i <= N + 1; i++) {
    bin1[i] = (bin1[i - 1] * 2) % (pcnt + 1);
    if (pcnt != 1)
      bin2[i] = (bin2[i - 1] * 2) % (pcnt - 1);
  }

  REP(i, N) {
    if (S[i] == '0') {
      cout << 1 + secondcomp((cnt1 + (bin1[N - i - 1])) % (pcnt + 1)) << "\\n";
    } else {
      // cout << "cnt2 = " << cnt2 << "\\n";
      // cout << "bin2[N-i-1] = " << bin2[N - i - 1] << "\\n";
      ll secin = ((cnt2 - (bin2[N - i - 1])) + (pcnt - 1)) % (pcnt - 1);
      cout << 1 + secondcomp(secin) << "\\n";
    }
  }
}