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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, size(v)) i >> v[j];
  return i;
}
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;
}
template <class T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  string s;
  cin >> n >> s;
  int one = count(all(s), '1');
  int mod1 = one - 1, mod2 = one + 1;
  vector<ll> memo1(n), memo2(n);
  memo1[n - 1] = memo2[n - 1] = 1;
  if (mod1 > 0)
    for (int i = n - 2; i >= 0; i--)
      memo1[i] = memo1[i + 1] * 2 % mod1;
  for (int i = n - 2; i >= 0; i--)
    memo2[i] = memo2[i + 1] * 2 % mod2;
  int sum1 = 0, sum2 = 0;
  if (mod1 > 0)
    rep(i, n) {
      if (s[i] == '1')
        sum1 += memo1[i], sum1 %= mod1;
    }
  rep(i, n) {
    if (s[i] == '1')
      sum2 += memo2[i], sum2 %= mod2;
  }
  if (one <= 1) {
    if (one == 0) {
      rep(i, n) cout << 1 << '\\n';
    } else if (one == 1) {
      rep(i, n) {
        if (s[i] == '1')
          cout << 0 << '\\n';
        else {
          int now = sum2;
          now = (now + memo2[i]) % mod2;
          int ans = 1;
          while (now > 0) {
            int m = __builtin_popcount(now);
            now = now % m;
            ans++;
          }
          cout << ans << '\\n';
        }
      }
    }
  } else {
    rep(i, n) {
      if (s[i] == '1') {
        int now = sum1;
        now = (now + mod1 - memo1[i]) % mod1;
        int ans = 1;
        while (now > 0) {
          int m = __builtin_popcount(now);
          now = now % m;
          ans++;
        }
        cout << ans << '\\n';
      } else {
        int now = sum2;
        now = (now + memo2[i]) % mod2;
        int ans = 1;
        while (now > 0) {
          int m = __builtin_popcount(now);
          now = now % m;
          ans++;
        }
        cout << ans << '\\n';
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var)                                                             \\
  do {                                                                         \\
    std::cout << #var << " : ";                                                \\
    view(var);                                                                 \\
  } while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
  for (const auto &e : v) {
    std::cout << e << " ";
  }
  std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
  rep(j, size(v)) i >> v[j];
  return i;
}
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;
}
template <class T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
  return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back

const int MOD = 1000000007;
const int INF = 1e9;

#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  string s;
  cin >> n >> s;
  int one = count(all(s), '1');
  int mod1 = one - 1, mod2 = one + 1;
  vector<ll> memo1(n), memo2(n);
  memo1[n - 1] = memo2[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--)
    memo1[i] = memo1[i + 1] * 2 % mod1;
  for (int i = n - 2; i >= 0; i--)
    memo2[i] = memo2[i + 1] * 2 % mod2;
  int sum1 = 0, sum2 = 0;
  if (mod1 > 0)
    rep(i, n) {
      if (s[i] == '1')
        sum1 += memo1[i], sum1 %= mod1;
    }
  rep(i, n) {
    if (s[i] == '1')
      sum2 += memo2[i], sum2 %= mod2;
  }
  if (one <= 1) {
    if (one == 0) {
      rep(i, n) cout << 1 << '\\n';
    } else if (one == 1) {
      rep(i, n) {
        if (s[i] == '1')
          cout << 0 << '\\n';
        else {
          int now = sum2;
          now = (now + memo2[i]) % mod2;
          int ans = 1;
          while (now > 0) {
            int m = __builtin_popcount(now);
            now = now % m;
            ans++;
          }
          cout << ans << '\\n';
        }
      }
    }
  } else {
    rep(i, n) {
      if (s[i] == '1') {
        int now = sum1;
        now = (now + mod1 - memo1[i]) % mod1;
        int ans = 1;
        while (now > 0) {
          int m = __builtin_popcount(now);
          now = now % m;
          ans++;
        }
        cout << ans << '\\n';
      } else {
        int now = sum2;
        now = (now + memo2[i]) % mod2;
        int ans = 1;
        while (now > 0) {
          int m = __builtin_popcount(now);
          now = now % m;
          ans++;
        }
        cout << ans << '\\n';
      }
    }
  }
}