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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (long long i = a; i < n; i++)
#define sz(A) (int)(A.size())
#define all(x) (x).begin(), (x).end()
#define get(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
int mai(int m, int n, int k) {
  int ans = 1;
  for (; n; n >>= 1, m = (long long)m * m % k)
    if (n & 1)
      ans = (long long)ans * m % k;
  return ans;
}
int cnt(int n) {
  int ans = 0;
  while (n) {
    if (n & 1)
      ans++;
    n >>= 1;
  }
  return ans;
}
int help(int n) {
  int op = 0;
  while (n) {
    op++;
    n %= cnt(n);
  }
  return op;
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sum = 0;
  rep(i, 0, n) {
    if (s[i] == '1') {
      sum++;
    }
  }
  ll t1 = 0, t2 = 0;
  int pos = n - 1;
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (sum > 1) {
        t1 += mai(2, pos, sum - 1);
        t1 %= (sum - 1);
      }
      if (sum < n) {
        t2 += mai(2, pos, sum + 1);
        t2 %= (sum + 1);
      }
    }
    pos--;
  }

  vector<ll> res(n + 1);
  rep(i, 0, n + 1) res[i] = help(i);
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (sum - 1 == 0) {
        cout << 0 << endl;
      } else if (sum - 1 == 1) {
        cout << 1 << endl;
      } else if (sum - 1 == 2) {
        if (i != n - 1) {
          if (s[n - 1] == '1') {
            cout << 2 << endl;
          } else {
            cout << 1 << endl;
          }
        } else {
          cout << 1 << endl;
        }
      } else {
        ll t1c = t1;
        t1c -= mai(2, n - i - 1, sum - 1);
        t1c %= (sum - 1);
        if (t1c < 0)
          t1c = (t1c + sum - 1);
        cout << 1 + res[t1c] << endl;
      }
    } else {
      ll t2c = t2;
      t2c += mai(2, n - i - 1, sum + 1);
      t2c %= (sum + 1);
      cout << 1 + res[t2c] << endl;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T_ = 1;
  // cin >> T_;
  for (int i = 0; i < T_; i++)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (long long i = a; i < n; i++)
#define sz(A) (int)(A.size())
#define all(x) (x).begin(), (x).end()
#define get(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
int mai(int m, int n, int k) {
  int ans = 1;
  for (; n; n >>= 1, m = (long long)m * m % k)
    if (n & 1)
      ans = (long long)ans * m % k;
  return ans;
}
int cnt(int n) {
  int ans = 0;
  while (n) {
    if (n & 1)
      ans++;
    n >>= 1;
  }
  return ans;
}
int help(int n) {
  int op = 0;
  while (n) {
    op++;
    n %= cnt(n);
  }
  return op;
}
void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sum = 0;
  rep(i, 0, n) {
    if (s[i] == '1') {
      sum++;
    }
  }
  ll t1 = 0, t2 = 0;
  int pos = n - 1;
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (sum > 0) {
        t1 += mai(2, pos, sum - 1);
        t1 %= (sum - 1);
      }
      if (sum < n) {
        t2 += mai(2, pos, sum + 1);
        t2 %= (sum + 1);
      }
    }
    pos--;
  }

  VI res(n);
  rep(i, 0, n) res[i] = help(i);
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (sum - 1 == 0) {
        cout << 0 << endl;
      } else if (sum - 1 == 1) {
        cout << 1 << endl;
      } else if (sum - 1 == 2) {
        if (i != n - 1) {
          if (s[n - 1] == '1') {
            cout << 2 << endl;
          } else {
            cout << 1 << endl;
          }
        } else {
          cout << 1 << endl;
        }
      } else {
        ll t1c = t1;
        t1c -= mai(2, n - i - 1, sum - 1);
        t1c %= (sum - 1);
        if (t1c < 0)
          t1c = (t1c + sum - 1) % (sum - 1);
        cout << 1 + res[t1c] << endl;
      }
    } else {
      ll t2c = t2;
      t2c += mai(2, n - i - 1, sum + 1);
      t2c %= (sum + 1);
      cout << 1 + res[t2c] << endl;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T_ = 1;
  // cin >> T_;
  for (int i = 0; i < T_; i++)
    solve();
  return 0;
}