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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef pair<double, ll> pdl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<pdd> vdd;
typedef vector<pdi> vdi;
typedef vector<pdl> vdl;
#define fi first
#define se second
const ll INFLL = LLONG_MAX;
const int INF = INT_MAX;
const ll MAXLL = 0x3f3f3f3f3f3f3f3f;
const int MAX = 0x3f3f3f3f;
#define eb emplace_back
#define emp emplace
#define mp(a, b) make_pair(a, b)
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }
template <class T, class U> void sort(vector<T> &v, U func) {
  sort(v.begin(), v.end(), func);
}
template <class T> void rsort(vector<T> &v) { sort(v.rbegin(), v.rend()); }
template <class T> int lb_index(vector<T> &v, T k) {
  return lower_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> int ub_index(vector<T> &v, T k) {
  return upper_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> bool is_sorted(vector<T> &v) {
  return is_sorted(v.begin(), v.end());
}
template <class T> bool sorted(vector<T> &v) { return is_sorted(v); }
int n;
map<pii, ll> mp;
ll bm(ll b, ll p, ll m) {
  if (p == 0)
    return 1 % m;
  if (p & 1) {
    return (b % m * bm(b, p - 1, m)) % m;
  }
  ll i = bm(b, p >> 1, m);
  return i * i % m;
}
int st[200000];
int f(int x) {
  if (x == 0)
    return 0;
  if (st[x])
    return st[x];
  int cnt = 0;
  for (int i = 0; (1 << i) <= x; i++) {
    if (x & (1 << i))
      cnt++;
  }
  return st[x] = 1 + f(x % cnt);
}
void solve() {
  string s;
  cin >> n >> s;
  int mod = 0;
  for (int i = 0; i < n; i++) {
    mod += (s[i] == '1');
  }
  for (int i = 0; i <= n; i++) {
    mp[{i, mod + 1}] = bm(2, i, mod + 1);
    if (mod > 1)
      mp[{i, mod - 1}] = bm(2, i, mod - 1);
  }
  int add = 0, minus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      add = (add + mp[{n - i - 1, mod + 1}]) % (mod + 1);
      if (mod > 1)
        minus = (minus + mp[{n - i - 1, mod - 1}]) % (mod - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (mod == 1) {
        cout << 0 << '\\n';
        continue;
      }
      s[i] = '0';
      cout << 1 + f(((minus - mp[{n - i - 1, mod - 1}]) % (mod - 1) +
                     (mod - 1)) %
                    (mod - 1))
           << '\\n';
      s[i] = '1';
    } else {
      s[i] = '1';
      cout << 1 + f(((add + mp[{n - i - 1, mod + 1}]) % (mod + 1) + (mod + 1)) %
                    (mod + 1))
           << '\\n';
      s[i] = '0';
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  int numberofsubtestcases = 1;
  //	cin>>numberofsubtestcases;
  while (numberofsubtestcases--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef pair<double, ll> pdl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<pdd> vdd;
typedef vector<pdi> vdi;
typedef vector<pdl> vdl;
#define fi first
#define se second
const ll INFLL = LLONG_MAX;
const int INF = INT_MAX;
const ll MAXLL = 0x3f3f3f3f3f3f3f3f;
const int MAX = 0x3f3f3f3f;
#define eb emplace_back
#define emp emplace
#define mp(a, b) make_pair(a, b)
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> void sort(vector<T> &v) { sort(v.begin(), v.end()); }
template <class T, class U> void sort(vector<T> &v, U func) {
  sort(v.begin(), v.end(), func);
}
template <class T> void rsort(vector<T> &v) { sort(v.rbegin(), v.rend()); }
template <class T> int lb_index(vector<T> &v, T k) {
  return lower_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> int ub_index(vector<T> &v, T k) {
  return upper_bound(v.begin(), v.end(), k) - v.begin();
}
template <class T> bool is_sorted(vector<T> &v) {
  return is_sorted(v.begin(), v.end());
}
template <class T> bool sorted(vector<T> &v) { return is_sorted(v); }
int n;
map<pii, ll> mp;
ll bm(ll b, ll p, ll m) {
  if (p == 0)
    return 1 % m;
  if (p & 1) {
    return (b % m * bm(b, p - 1, m)) % m;
  }
  ll i = bm(b, p >> 1, m);
  return i * i % m;
}
int st[200000];
int f(int x) {
  if (x == 0)
    return 0;
  if (st[x])
    return st[x];
  int cnt = 0;
  for (int i = 0; (1 << i) <= x; i++) {
    if (x & (1 << i))
      cnt++;
  }
  return st[x] = 1 + f(x % cnt);
}
void solve() {
  string s;
  cin >> n >> s;
  int mod = 0;
  for (int i = 0; i < n; i++) {
    mod += (s[i] == '1');
  }
  for (int i = 0; i <= n; i++) {
    mp[{i, mod + 1}] = bm(2, i, mod + 1);
    if (mod > 1)
      mp[{i, mod - 1}] = bm(2, i, mod - 1);
  }
  int add = 0, minus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      add = (add + mp[{n - i - 1, mod + 1}]) % (mod + 1);
      minus = (minus + mp[{n - i - 1, mod - 1}]) % (mod - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      s[i] = '0';
      cout << 1 + f(((minus - mp[{n - i - 1, mod - 1}]) % (mod - 1) +
                     (mod - 1)) %
                    (mod - 1))
           << '\\n';
      s[i] = '1';
    } else {
      s[i] = '1';
      cout << 1 + f(((add + mp[{n - i - 1, mod + 1}]) % (mod + 1) + (mod + 1)) %
                    (mod + 1))
           << '\\n';
      s[i] = '0';
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  int numberofsubtestcases = 1;
  //	cin>>numberofsubtestcases;
  while (numberofsubtestcases--) {
    solve();
  }
}