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

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

using ll = int64_t;

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...)                                                               \\
  if (0) {                                                                     \\
  }
#endif

int dict[200003] = {};

ll pow_mod(ll a, int e, ll mod) {
  ll ret = 1LL;
  while (e > 0) {
    if (e & 1)
      ret = (ret * a) % mod;
    a = (a * a) % mod;
    e >>= 1; // e /= 2
  }
  return ret;
}

ll Get(string &s, ll mod) {
  if (mod == 0)
    return 0;
  ll x = 0;
  int n = s.length();
  for (int i = 0; i < n; ++i) {
    if (s[n - 1 - i] == '1') {
      x = (x + pow_mod(2, i, mod)) % mod;
    }
  }
  return x;
}

ll DFS(int x) {
  if (dict[x] != -1)
    return dict[x];
  return dict[x] = DFS(x % __builtin_popcount(x)) + 1;
}

void init() {
  memset(dict, -1, sizeof(dict));
  dict[0] = 1;
  for (int i = 1; i <= 200002; ++i) {
    dict[i] = DFS(i);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  init();
  int cnt = 0;
  for (char c : s)
    cnt += c == '1';
  if (cnt == 0) {
    for (int i = 0; i < n; ++i) {
      cout << 1 << endl;
    }
    return 0;
  }

  int mod_one_more = cnt + 1, mod_one_less = cnt - 1;
  int val_one_less = Get(s, mod_one_less);
  int val_one_more = Get(s, mod_one_more);
  for (int i = 0; i < n; ++i) {
    int p = n - 1 - i;
    if (s[i] == '0') {
      ll ans = (val_one_more + pow_mod(2, p, mod_one_more)) % mod_one_more;
      //      dbg(ans);
      cout << dict[ans] << endl;
    } else if (mod_one_less <= 0) {
      cout << 0 << endl;
    } else {
      ll ans = (val_one_less - pow_mod(2, p, mod_one_less) + mod_one_less +
                mod_one_less) %
               mod_one_less;
      //      dbg(ans);
      cout << dict[ans] << endl;
    }
  }
}


Error: 0

Fixed submission:

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

using ll = int64_t;

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...)                                                               \\
  if (0) {                                                                     \\
  }
#endif

int dict[200003] = {};

ll pow_mod(ll a, int e, ll mod) {
  ll ret = 1LL;
  while (e > 0) {
    if (e & 1)
      ret = (ret * a) % mod;
    a = (a * a) % mod;
    e >>= 1; // e /= 2
  }
  return ret;
}

ll Get(string &s, ll mod) {
  if (mod == 0)
    return 0;
  ll x = 0;
  int n = s.length();
  for (int i = 0; i < n; ++i) {
    if (s[n - 1 - i] == '1') {
      x = (x + pow_mod(2, i, mod)) % mod;
    }
  }
  return x;
}

ll DFS(int x) {
  if (dict[x] != -1)
    return dict[x];
  return dict[x] = DFS(x % __builtin_popcount(x)) + 1;
}

void init() {
  memset(dict, -1, sizeof(dict));
  dict[0] = 1;
  for (int i = 1; i <= 200002; ++i) {
    dict[i] = DFS(i);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  init();
  int cnt = 0;
  for (char c : s)
    cnt += c == '1';
  if (cnt == 0) {
    for (int i = 0; i < n; ++i) {
      cout << 1 << endl;
    }
    return 0;
  }

  int mod_one_more = cnt + 1, mod_one_less = cnt - 1;
  int val_one_less = Get(s, mod_one_less);
  int val_one_more = Get(s, mod_one_more);
  for (int i = 0; i < n; ++i) {
    int p = n - 1 - i;
    if (s[i] == '0') {
      ll ans = (val_one_more + pow_mod(2, p, mod_one_more)) % mod_one_more;
      //      dbg(ans);
      cout << dict[ans] << endl;
    } else {
      ll ans = (val_one_less - pow_mod(2, p, mod_one_less) + mod_one_less) %
               mod_one_less;
      //      dbg(ans);
      cout << dict[ans] << endl;
    }
  }
}