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

#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
#ifdef LMAO
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 42
#endif
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define tcase                                                                  \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  while (tt--)
#define tcase2                                                                 \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  for (int qq = 1; qq <= tt; qq++)
#define all(x) x.begin(), x.end()
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
const int MAX = 1000010;
const int MOD2 = 998244353;
// const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;
int MOD;
inline int add(int x, int y) {
  x += y;
  if (x > MOD)
    x %= MOD;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += MOD;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % MOD; }
inline int binpow(int a, int b, int f = 0) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}
inline int inv(int x) { return binpow(x, MOD - 2); }
/*****************start************/
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int co = 0;
  for (int i = 0; i < n; i++)
    co += (s[i] == '1');
  MOD = co;
  vector<int> lup(co + 1);
  for (int i = 0; i <= co; i++) {
    int j = i;
    int z = 1;
    while (j > 0) {
      z++;
      j %= __builtin_popcount(j);
    }
    lup[i] = z;
  }
  // trace(lup);
  int total1 = 0;
  MOD = co + 1;
  for (int i = 0; i < n; i++) {
    total1 = add(total1, mul((int)(s[i] == '1'), binpow(2, n - i - 1)));
  }
  total1 %= MOD;
  // trace(total1);
  int total2 = 0;
  MOD = co - 1;
  if (MOD) {
    for (int i = 0; i < n; i++) {
      total2 = add(total2, mul((int)(s[i] == '1'), binpow(2, n - i - 1)));
    }
    total2 %= MOD;
  }
  // trace(total2);

  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      MOD = co + 1;
      int lo = add(total1, binpow(2, n - i - 1));
      // trace(lo);
      cout << lup[lo % MOD] << endl;
    } else {
      MOD = co - 1;
      if (!MOD) {
        cout << 0 << endl;
        continue;
      }
      int lo = sub(total2, binpow(2, n - i - 1));
      // trace(lo);
      cout << lup[lo % MOD] << endl;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  cerr << fixed << setprecision(10);
  auto start = std::chrono::high_resolution_clock::now();
  // tcase{
  solve();
  // }
  auto stop = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
  // cerr << "Time taken : " << ((long double)duration.count())/((long double)
  // 1e9) <<"s "<< endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
#ifdef LMAO
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...) 42
#endif
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define endl "\\n"
#define tcase                                                                  \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  while (tt--)
#define tcase2                                                                 \\
  int tt;                                                                      \\
  cin >> tt;                                                                   \\
  for (int qq = 1; qq <= tt; qq++)
#define all(x) x.begin(), x.end()
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
const int MAX = 1000010;
const int MOD2 = 998244353;
// const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;
int MOD;
inline int add(int x, int y) {
  x += y;
  if (x > MOD)
    x %= MOD;
  return x;
}
inline int sub(int x, int y) {
  x -= y;
  if (x < 0)
    x += MOD;
  return x;
}
inline int mul(int x, int y) { return (x * 1ll * y) % MOD; }
inline int binpow(int a, int b, int f = 0) {
  int res = 1;
  while (b > 0) {
    if (b & 1)
      res = mul(res, a);
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}
inline int inv(int x) { return binpow(x, MOD - 2); }
/*****************start************/
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int co = 0;
  for (int i = 0; i < n; i++)
    co += (s[i] == '1');
  MOD = co;
  vector<int> lup(co + 1);
  for (int i = 0; i <= co; i++) {
    int j = i;
    int z = 1;
    while (j > 0) {
      z++;
      j %= __builtin_popcount(j);
    }
    lup[i] = z;
  }
  // trace(lup);
  int total1 = 0;
  MOD = co + 1;
  for (int i = 0; i < n; i++) {
    total1 = add(total1, mul((int)(s[i] == '1'), binpow(2, n - i - 1)));
  }
  total1 %= MOD;
  // trace(total1);
  int total2 = 0;
  MOD = co - 1;
  for (int i = 0; i < n; i++) {
    total2 = add(total2, mul((int)(s[i] == '1'), binpow(2, n - i - 1)));
  }
  total2 %= MOD;
  // trace(total2);

  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      MOD = co + 1;
      int lo = add(total1, binpow(2, n - i - 1));
      // trace(lo);
      cout << lup[lo % MOD] << endl;
    } else {
      MOD = co - 1;
      int lo = sub(total2, binpow(2, n - i - 1));
      // trace(lo);
      cout << lup[lo % MOD] << endl;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed << setprecision(25);
  cerr << fixed << setprecision(10);
  auto start = std::chrono::high_resolution_clock::now();
  // tcase{
  solve();
  // }
  auto stop = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
  // cerr << "Time taken : " << ((long double)duration.count())/((long double)
  // 1e9) <<"s "<< endl;
  return 0;
}