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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.begin(), a.end(), greater<int>()
#define ROT(a) a.begin(), a.begin() + 1, a.end()
#define RROT(a) a.begin(), a.end() - 1, a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9 + 7;
const int MAX = 10000000;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// cout << 'H' << endl;
/*--------------------------------------------*/

int ct_1(string s) {
  int c = 0;
  rep(i, sz(s)) {
    if (s[i] == '1') {
      c++;
    }
  }

  return c;
}

int main() {
  // cout << fixed << setprecision(10)
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  string X;
  cin >> X;

  int ct = 0;
  rep(i, N) {
    if (X[i] == '1') {
      ct++;
    }
  }

  int m = ct - 1;
  int M = ct + 1;

  vector<int> mod_m(N);
  vector<int> mod_M(N);

  if (m)
    mod_m[0] = 1 % m;
  mod_M[0] = 1 % M;

  REP(i, N - 1) {
    if (m)
      mod_m[i] = (2 * mod_m[i - 1]) % m;
    mod_M[i] = (2 * mod_M[i - 1]) % M;
  }

  int tmp_m = 0;
  int tmp_M = 0;

  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      tmp_m += mod_m[N - i - 1];
      if (m)
        tmp_m %= m;
      tmp_M += mod_M[N - i - 1];
      tmp_M %= M;
    }
  }

  vector<int> f(N + 1);
  for (int i = 1; i <= N; i++) {
    auto bs = bitset<20>(i);
    int tmp = bs.count();

    f[i] = 1 + f[i % tmp];
  }

  rep(i, N) {
    if (X[i] == '1') {
      if (m == 0) {
        cout << 0 << endl;
      } else {

        int tmp = tmp_m;
        tmp += m;
        tmp -= mod_m[N - i - 1];

        cout << 1 + f[tmp % m] << endl;
      }
    } else {
      int tmp = tmp_M;
      //            cout << tmp << endl;
      tmp += mod_M[N - i - 1];
      //           cout << tmp << endl;
      cout << 1 + f[tmp % M] << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<long long, long long>> vpl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.begin(), a.end(), greater<int>()
#define ROT(a) a.begin(), a.begin() + 1, a.end()
#define RROT(a) a.begin(), a.end() - 1, a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9 + 7;
const int MAX = 10000000;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// cout << 'H' << endl;
/*--------------------------------------------*/

int ct_1(string s) {
  int c = 0;
  rep(i, sz(s)) {
    if (s[i] == '1') {
      c++;
    }
  }

  return c;
}

int main() {
  // cout << fixed << setprecision(10)
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  string X;
  cin >> X;

  int ct = 0;
  rep(i, N) {
    if (X[i] == '1') {
      ct++;
    }
  }

  int m = ct - 1;
  int M = ct + 1;

  vector<int> mod_m(N);
  vector<int> mod_M(N);

  mod_m[0] = 1 % m;
  mod_M[0] = 1 % M;

  REP(i, N - 1) {
    mod_m[i] = (2 * mod_m[i - 1]) % m;
    mod_M[i] = (2 * mod_M[i - 1]) % M;
  }

  int tmp_m = 0;
  int tmp_M = 0;

  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      tmp_m += mod_m[N - i - 1];
      tmp_m %= m;
      tmp_M += mod_M[N - i - 1];
      tmp_M %= M;
    }
  }

  vector<int> f(N + 1);
  for (int i = 1; i <= N; i++) {
    auto bs = bitset<20>(i);
    int tmp = bs.count();

    f[i] = 1 + f[i % tmp];
  }

  rep(i, N) {
    if (X[i] == '1') {
      int tmp = tmp_m;
      tmp += m;
      tmp -= mod_m[N - i - 1];

      cout << 1 + f[tmp % m] << endl;
    } else {
      int tmp = tmp_M;
      //            cout << tmp << endl;
      tmp += mod_M[N - i - 1];
      //           cout << tmp << endl;
      cout << 1 + f[tmp % M] << endl;
    }
  }
}