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

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

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}

void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; }

int binpow(int base, int e, int mod) {
  int ret = 1;
  while (e) {
    if (e & 1)
      ret = (long long)ret * base % mod;
    base = (long long)base * base % mod;
    e >>= 1;
  }
  return ret;
}

int f(int val) {
  int ret = 0;
  while (val > 0) {
    int cnt = __builtin_popcount(val);
    val %= cnt;
    ret++;
  }
  return ret;
}

int main() {
  fast();
  int n;
  string x;
  cin >> n >> x;
  int cnt = 0;
  for (char ch : x)
    cnt += (ch == '1');
  int sum0 = 0, sum1 = 0;
  if (cnt > 1) {
    for (int i = 0; i < n; i++)
      sum0 = (sum0 + (x[i] == '1') * binpow(2, n - 1 - i, cnt - 1)) % (cnt - 1);
  }
  for (int i = 0; i < n; i++)
    sum1 = (sum1 + (x[i] == '1') * binpow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
  // debug(sum0, sum1);
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (cnt == 1) {
        cout << 0 << '\\n';
        continue;
      }
      int val =
          ((sum0 - binpow(2, n - 1 - i, cnt - 1)) % (cnt - 1) + (cnt - 1)) %
          (cnt - 1);
      // debug(i, val);
      cout << f(val) + 1 << '\\n';
    } else {
      if (cnt == 0) {
        cout << 1 << '\\n';
        continue;
      }
      int val = (sum1 + binpow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      cout << f(val) + 1 << '\\n';
    }
  }
}


Error: 0

Fixed submission:

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

#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) (int)((v).size())
#define fs first
#define sd second

typedef long long ll;

typedef pair<int, int> pi;
typedef pair<pi, int> ppi;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
typedef vector<ppi> vppi;

void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}

vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}

void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

double get_time() { return 1.0 * clock() / CLOCKS_PER_SEC; }

int binpow(int base, int e, int mod) {
  int ret = 1;
  while (e) {
    if (e & 1)
      ret = (long long)ret * base % mod;
    base = (long long)base * base % mod;
    e >>= 1;
  }
  return ret;
}

int f(int val) {
  int ret = 0;
  while (val > 0) {
    int cnt = __builtin_popcount(val);
    val %= cnt;
    ret++;
  }
  return ret;
}

int main() {
  fast();
  int n;
  string x;
  cin >> n >> x;
  int cnt = 0;
  for (char ch : x)
    cnt += (ch == '1');
  int sum0 = 0, sum1 = 0;
  if (cnt > 0) {
    for (int i = 0; i < n; i++)
      sum0 = (sum0 + (x[i] == '1') * binpow(2, n - 1 - i, cnt - 1)) % (cnt - 1);
  }
  for (int i = 0; i < n; i++)
    sum1 = (sum1 + (x[i] == '1') * binpow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
  // debug(sum0, sum1);
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (cnt == 1) {
        cout << 0 << '\\n';
        continue;
      }
      int val =
          ((sum0 - binpow(2, n - 1 - i, cnt - 1)) % (cnt - 1) + (cnt - 1)) %
          (cnt - 1);
      // debug(i, val);
      cout << f(val) + 1 << '\\n';
    } else {
      if (cnt == 0) {
        cout << 0 << '\\n';
        continue;
      }
      int val = (sum1 + binpow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      cout << f(val) + 1 << '\\n';
    }
  }
}