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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n, m) for (int i = n; i < m; i++)

using ll = long long;
using ld = long double;
using Vii = vector<vector<int>>;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vd = vector<double>;
using Vld = vector<long double>;
using Vll = vector<vector<ll>>;
using Vldd = vector<vector<ld>>;
using Vs = vector<string>;
using Vb = vector<bool>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Pli = pair<ll, int>;
using VPi = vector<Pi>;
using VPl = vector<Pl>;
using VPli = vector<Pli>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;

  int d = 0;
  rep(i, n) if (x[i] == '1') d++;

  if (d == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  if (d == 1) {
    Vi p(n);
    Vi ans(n, -1);

    int z = (x[n - 1] - '0') % 2;
    rep(i, n) {
      if (x[i] == '1')
        p[i] == 0;
      else
        p[i] = 2;

      if (p[i] == 0)
        ans[i] = 0;
      else {
        if (z) {
          if (i == n - 1)
            ans[i] = 1;
          else
            ans[i] = 2;
        } else {
          if (i == n - 1)
            ans[i] = 2;
          else
            ans[i] = 1;
        }
      }
    }

    for (int u : ans)
      cout << u << endl;

    return 0;
  }

  reverse(x.begin(), x.end());

  Vi popx(n, 0);
  rep(i, n) {
    if (x[i] == '1')
      popx[i] = d - 1;
    else
      popx[i] = d + 1;
  }

  int A[3] = {0};
  for (int i = 0; i < 3; i++) {
    int t = 1;
    ll u = 0;
    for (int j = 0; j < n; j++) {
      if (x[j] == '1') {
        u = (u + t) % (d - 1 + i);
      }
      t = (t * 2) % (d - 1 + i);
    }
    u %= (d - 1 + i);
    A[i] = u;
  }

  Vi mod2(n, 0);
  int t1 = 1, t2 = 1, t3 = 1;
  rep(i, n) {
    int u = popx[i];
    if (x[i] == '0') {
      if (u == d - 1) {
        mod2[i] = (A[0] + t1) % u;
      } else if (u == d) {
        mod2[i] = (A[1] + t2) % u;
      } else {
        mod2[i] = (A[2] + t3) % u;
      }
    } else {
      if (u == d - 1) {
        mod2[i] = (u + A[0] - t1) % u;
      } else if (u == d) {
        mod2[i] = (u + A[1] - t2) % u;
      } else {
        mod2[i] = (u + A[2] - t3) % u;
      }
    }
    t1 = (t1 * 2) % (d - 1);
    t2 = (t2 * 2) % d;
    t3 = (t3 * 2) % (d + 1);
  }
  reverse(mod2.begin(), mod2.end());

  Vi ans(n, -1);
  rep(i, n) {
    if (mod2[i] == 0 && ans[i] == -1) {
      ans[i] = 1;
      continue;
    }
    int tans = 1;
    while (mod2[i] > 0) {
      int u = mod2[i];
      bitset<32> t = bitset<32>(u);
      int c = t.count();
      if (c == 0)
        break;
      mod2[i] %= c;
      tans++;
    }
    ans[i] = tans;
  }

  for (int u : ans) {
    cout << u << endl;
  }
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <new>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n, m) for (int i = n; i < m; i++)

using ll = long long;
using ld = long double;
using Vii = vector<vector<int>>;
using Gjudge = vector<vector<bool>>;
using Vi = vector<int>;
using Vl = vector<ll>;
using Vd = vector<double>;
using Vld = vector<long double>;
using Vll = vector<vector<ll>>;
using Vldd = vector<vector<ld>>;
using Vs = vector<string>;
using Vb = vector<bool>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Pli = pair<ll, int>;
using VPi = vector<Pi>;
using VPl = vector<Pl>;
using VPli = vector<Pli>;
using Ti = tuple<int, int, int>;
using Tl = tuple<ll, ll, ll>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int Mod = static_cast<int>(1e9 + 7);
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;

  int d = 0;
  rep(i, n) if (x[i] == '1') d++;

  if (d == 1) {
    Vi p(n);
    Vi ans(n, -1);

    int z = (x[n - 1] - '0') % 2;
    rep(i, n) {
      if (x[i] == '1')
        p[i] == 0;
      else
        p[i] = 2;

      if (p[i] == 0)
        ans[i] = 0;
      else {
        if (z) {
          if (i == n - 1)
            ans[i] = 1;
          else
            ans[i] = 2;
        } else {
          if (i == n - 1)
            ans[i] = 2;
          else
            ans[i] = 1;
        }
      }
    }

    for (int u : ans)
      cout << u << endl;

    return 0;
  }

  reverse(x.begin(), x.end());

  Vi popx(n, 0);
  rep(i, n) {
    if (x[i] == '1')
      popx[i] = d - 1;
    else
      popx[i] = d + 1;
  }

  int A[3] = {0};
  for (int i = 0; i < 3; i++) {
    int t = 1;
    ll u = 0;
    for (int j = 0; j < n; j++) {
      if (x[j] == '1') {
        u = (u + t) % (d - 1 + i);
      }
      t = (t * 2) % (d - 1 + i);
    }
    u %= (d - 1 + i);
    A[i] = u;
  }

  Vi mod2(n, 0);
  int t1 = 1, t2 = 1, t3 = 1;
  rep(i, n) {
    int u = popx[i];
    if (x[i] == '0') {
      if (u == d - 1) {
        mod2[i] = (A[0] + t1) % u;
      } else if (u == d) {
        mod2[i] = (A[1] + t2) % u;
      } else {
        mod2[i] = (A[2] + t3) % u;
      }
    } else {
      if (u == d - 1) {
        mod2[i] = (u + A[0] - t1) % u;
      } else if (u == d) {
        mod2[i] = (u + A[1] - t2) % u;
      } else {
        mod2[i] = (u + A[2] - t3) % u;
      }
    }
    t1 = (t1 * 2) % (d - 1);
    t2 = (t2 * 2) % d;
    t3 = (t3 * 2) % (d + 1);
  }
  reverse(mod2.begin(), mod2.end());

  Vi ans(n, -1);
  rep(i, n) {
    if (mod2[i] == 0 && ans[i] == -1) {
      ans[i] = 1;
      continue;
    }
    int tans = 1;
    while (mod2[i] > 0) {
      int u = mod2[i];
      bitset<32> t = bitset<32>(u);
      int c = t.count();
      if (c == 0)
        break;
      mod2[i] %= c;
      tans++;
    }
    ans[i] = tans;
  }

  for (int u : ans) {
    cout << u << endl;
  }
}