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

// In the name of Allah :)
#include <bits/stdc++.h>
using namespace std;
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char *s) { return (string)s; }
string to_string(string s) { return s; }
template <class A> string to_string(complex<A> c) {
  stringstream ss;
  ss << c;
  return ss.str();
}
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = 0; i < (int)v.size(); i++)
    res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ> string to_string(bitset<SZ> b) {
  string res = "";
  for (size_t i = 0; i < SZ; i++)
    res += char('0' + b[i]);
  return res;
}
template <class A, class B> string to_string(pair<A, B> p);
template <class T> string to_string(T v) { // containers with begin(), end()
  bool fst = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!fst)
      res += ", ";
    fst = 0;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <class A, class B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.f) + ", " + to_string(p.s) + ")";
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T> void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t))
    cerr << ", ";
  DBG(t...);
}
#ifdef LOCAL // compile with -DLOCAL
#define wis(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [",          \\
      DBG(__VA_ARGS__)
#else
#define wis(...) 0
#endif
typedef long long ll;
const int INF = 1e9 + 5;
ll MOD;
inline ll powmod(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1, a = a * a % MOD)
    if (b & 1)
      res = res * a % MOD;
  return res;
}
inline ll power(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1, a = a * a)
    if (b & 1)
      res = res * a;
  return res;
}

const int MAXN = 1e7 + 10;

inline int count(string &x) {
  int ans = 0;
  for (char i : x)
    ans += i == '1';
  return ans;
}

inline int rem(string &n, int div) {
  if (div == 0)
    return 0;
  MOD = div;
  ll ans = 0;
  for (int i = 0; i < (int)n.size(); i++) {
    if (n[i] == '1')
      ans = (ans + powmod(2, i)) % MOD;
  }
  return ans;
}

int dp[MAXN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (int i = 1; i < MAXN; i++) {
    dp[i] = 1 + dp[i % __builtin_popcount(i)];
  }
  int n;
  string x;
  cin >> n;
  cin >> x;
  reverse(x.begin(), x.end());
  int cnt = count(x);
  ll zero = rem(x, cnt + 1);
  ll one = rem(x, cnt - 1);
  for (int i = n - 1; i > -1; i--) {
    if (x[i] == '0') {
      MOD = cnt + 1;
      zero += powmod(2, i);
      zero %= MOD;
      cout << 1 + dp[zero] << '\\n';
      zero -= powmod(2, i);
      zero += MOD;
      zero %= MOD;
    } else {
      MOD = cnt - 1;
      if (MOD == 0) {
        cout << 0 << '\\n';
        continue;
      }
      one -= powmod(2, i);
      one += MOD;
      one %= MOD;
      cout << 1 + dp[one] << '\\n';
      one += powmod(2, i);
      one %= MOD;
    }
  }
}


Error: 0

Fixed submission:

// In the name of Allah :)
#include <bits/stdc++.h>
using namespace std;
string to_string(char c) { return string(1, c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char *s) { return (string)s; }
string to_string(string s) { return s; }
template <class A> string to_string(complex<A> c) {
  stringstream ss;
  ss << c;
  return ss.str();
}
string to_string(vector<bool> v) {
  string res = "{";
  for (int i = 0; i < (int)v.size(); i++)
    res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ> string to_string(bitset<SZ> b) {
  string res = "";
  for (size_t i = 0; i < SZ; i++)
    res += char('0' + b[i]);
  return res;
}
template <class A, class B> string to_string(pair<A, B> p);
template <class T> string to_string(T v) { // containers with begin(), end()
  bool fst = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!fst)
      res += ", ";
    fst = 0;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <class A, class B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.f) + ", " + to_string(p.s) + ")";
}
void DBG() { cerr << "]" << endl; }
template <class H, class... T> void DBG(H h, T... t) {
  cerr << to_string(h);
  if (sizeof...(t))
    cerr << ", ";
  DBG(t...);
}
#ifdef LOCAL // compile with -DLOCAL
#define wis(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [",          \\
      DBG(__VA_ARGS__)
#else
#define wis(...) 0
#endif
typedef long long ll;
const int INF = 1e9 + 5;
int MOD;
inline ll powmod(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1, a = a * a % MOD)
    if (b & 1)
      res = res * a % MOD;
  return res;
}
inline ll power(ll a, ll b) {
  ll res = 1;
  for (; b; b >>= 1, a = a * a)
    if (b & 1)
      res = res * a;
  return res;
}

const int MAXN = 1e7 + 10;

inline int count(string &x) {
  int ans = 0;
  for (char i : x)
    ans += i == '1';
  return ans;
}

inline int rem(string &n, int div) {
  MOD = div;
  ll ans = 0;
  for (int i = 0; i < (int)n.size(); i++) {
    if (n[i] == '1')
      ans = (ans + powmod(2, i)) % MOD;
  }
  return ans;
}

int dp[MAXN];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  for (int i = 1; i < MAXN; i++) {
    dp[i] = 1 + dp[i % __builtin_popcount(i)];
  }
  int n;
  string x;
  cin >> n;
  cin >> x;
  int cnt = count(x);
  reverse(x.begin(), x.end());
  ll zero = rem(x, cnt + 1);
  ll one = rem(x, cnt - 1);
  for (int i = n - 1; i > -1; i--) {
    if (x[i] == '0') {
      MOD = cnt + 1;
      zero += powmod(2, i);
      zero %= MOD;
      if (zero >= MAXN) {
        while (1) {
          int x = 5;
        }
      }
      // assert(zero <= n);
      cout << 1 + dp[zero] << '\\n';
      zero -= powmod(2, i);
      zero += MOD;
      zero %= MOD;
    } else {
      MOD = cnt - 1;
      one -= powmod(2, i);
      one += MOD;
      one %= MOD;
      if (one >= MAXN) {
        while (1) {
          int x = 5;
        }
      }
      // assert(one <= n);
      cout << 1 + dp[one] << '\\n';
      one += powmod(2, i);
      one %= MOD;
    }
  }
}