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

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

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> lll;

const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);

#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); i++)
#define RFOR(i, a, b)                                                          \\
  for (long long i = (long long)(a); i >= (long long)(b); i--)
#define ull unsigned long long
#define deb(x) cerr << #x << " = " << x << endl;
#define endl "\\n"
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define fill(x, y) memset(x, y, sizeof(y))
#define all(x) (x).begin(), (x).end()
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define verify(x)                                                              \\
  if (x >= MOD)                                                                \\
    x -= MOD;

inline void debug_vi(vi a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void debug_vll(vll a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }

template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;

template <typename T> using maxpq = priority_queue<T>;
/*----------------------------------------------------------------------*/

const int nax = 2e5 + 5;
int p2[nax], p2minus[nax], p2plus[nax];

void pre(int cnt, int n) {
  p2[0] = p2minus[0] = p2plus[0] = 1;

  if (cnt > 1) {
    FOR(i, 1, nax) { p2minus[i] = (p2minus[i - 1] * 1LL * 2) % (cnt - 1); }
  }

  if (cnt < n) {
    FOR(i, 1, nax) { p2plus[i] = (p2plus[i - 1] * 1LL * 2) % (cnt + 1); }
  }
}

ll getMod(string str, int n, int k) {

  // pwrTwo[i] will store ((2^i) % k)
  ll pwrTwo[n];
  pwrTwo[0] = 1LL % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = (pwrTwo[i - 1] * 2LL) % k;
  }

  // To store the result
  ll res = 0;
  int i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (str[j] == '1') {

      // Add the current power of 2
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}

int simulate(int x) {
  int steps = 0;

  while (x) {
    x = (x % __builtin_popcount(x));
    steps++;
  }

  return steps;
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  FOR(i, 0, n) cnt += (s[i] == '1');
  // compute sum mod cnt, cnt - 1 and cnt + 1
  pre(cnt, n);
  int sm[3] = {-1, -1, -1};
  if (cnt > 1)
    sm[1] = getMod(s, n, cnt - 1);
  if (cnt < n)
    sm[2] = getMod(s, n, cnt + 1);

  FOR(i, 0, n) {
    int res;
    if (s[i] == '1') {
      if (cnt == 1) {
        res = 0;
      } else {
        // use cnt - 1;
        int curmod = (sm[1] - p2minus[n - 1 - i] + (cnt - 1)) % (cnt - 1);
        res = 1 + simulate(curmod);
      }
    } else {
      // use cnt + 1
      int curmod = (sm[2] + p2plus[n - 1 - i]) % (cnt + 1);
      res = 1 + simulate(curmod);
    }
    cout << res << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll, ll> lll;

const ll MOD = 1000000007;
const ll INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);

#define FOR(i, a, b) for (long long i = (long long)(a); i < (long long)(b); i++)
#define RFOR(i, a, b)                                                          \\
  for (long long i = (long long)(a); i >= (long long)(b); i--)
#define ull unsigned long long
#define deb(x) cerr << #x << " = " << x << endl;
#define endl "\\n"
#define coud(a, d) cout << fixed << showpoint << setprecision(d) << a;
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define fill(x, y) memset(x, y, sizeof(y))
#define all(x) (x).begin(), (x).end()
#define bitcount(x) __builtin_popcount(x)
#define cps CLOCKS_PER_SEC
#define verify(x)                                                              \\
  if (x >= MOD)                                                                \\
    x -= MOD;

inline void debug_vi(vi a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void debug_vll(vll a) { FOR(i, 0, a.size()) cout << a[i] << " "; }
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }

template <typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;

template <typename T> using maxpq = priority_queue<T>;
/*----------------------------------------------------------------------*/

const int nax = 2e5 + 5;
int p2[nax], p2minus[nax], p2plus[nax];

void pre(int cnt, int n) {
  p2[0] = p2minus[0] = p2plus[0] = 1;
  FOR(i, 1, nax) { p2[i] = (p2[i - 1] * 1LL * 2) % cnt; }

  if (cnt > 1) {
    FOR(i, 1, nax) { p2minus[i] = (p2minus[i - 1] * 1LL * 2) % (cnt - 1); }
  }

  if (cnt < n) {
    FOR(i, 1, nax) { p2plus[i] = (p2plus[i - 1] * 1LL * 2) % (cnt + 1); }
  }
}

ll getMod(string str, int n, int k) {

  // pwrTwo[i] will store ((2^i) % k)
  ll pwrTwo[n];
  pwrTwo[0] = 1 % k;
  for (int i = 1; i < n; i++) {
    pwrTwo[i] = pwrTwo[i - 1] * (2 % k);
    pwrTwo[i] %= k;
  }

  // To store the result
  int res = 0;
  int i = 0, j = n - 1;
  while (i < n) {

    // If current bit is 1
    if (str[j] == '1') {

      // Add the current power of 2
      res += (pwrTwo[i]);
      res %= k;
    }
    i++;
    j--;
  }
  return res;
}

int simulate(int x) {
  int steps = 0;

  while (x) {
    x = (x % __builtin_popcount(x));
    steps++;
  }

  return steps;
}

int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  FOR(i, 0, n) cnt += (s[i] == '1');
  // compute sum mod cnt, cnt - 1 and cnt + 1
  pre(cnt, n);
  int sm[3] = {-1, -1, -1};
  if (cnt > 1)
    sm[1] = getMod(s, n, cnt - 1);
  if (cnt < n)
    sm[2] = getMod(s, n, cnt + 1);

  FOR(i, 0, n) {
    int res;
    if (s[i] == '1') {
      if (cnt == 1) {
        res = 0;
      } else {
        // use cnt - 1;
        int curmod = (sm[1] - p2minus[n - 1 - i] + (cnt - 1)) % (cnt - 1);
        res = 1 + simulate(curmod);
      }
    } else {
      // use cnt + 1
      int curmod = (sm[2] + p2plus[n - 1 - i]) % (cnt + 1);
      res = 1 + simulate(curmod);
    }
    cout << res << endl;
  }

  return 0;
}