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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

int calc(int a) {
  int cnt = 0;
  while (a) {
    cnt += a % 2;
    a /= 2;
  }
  return cnt;
}
int modpow(int a, int p, int mo) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {

    int halfP = p / 2;
    int half = modpow(a, halfP, mo);

    return half * half % mo;
  } else {

    return a * modpow(a, p - 1, mo) % mo;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  string S;
  cin >> N >> S;
  int po = 0;
  REP(i, N) if (S[i] == '1') po++;
  VI cnt(N + 10, 0);
  REP(i, cnt.size()) {
    int co = calc(i);
    if (i != 0)
      cnt[i] = cnt[i % co] + 1;
  }
  int u = 0, d = 0;
  reverse(S.begin(), S.end());

  bool F = 0;
  if (po == 1)
    F = 1;
  REP(i, N) {
    if (S[i] == '1')
      u += modpow(2, i, po + 1);
    if (!F) {
      if (S[i] == '1')
        d += modpow(2, i, po - 1);
    }
    u %= (po + 1);
    if (!F) {
      d %= (po - 1);
    }
  }
  VI ans(N);
  REP(i, N) {
    if (S[i] == '0') {
      int U = u;
      U += modpow(2, i, po + 1);
      U %= (po + 1);
      ans[i] = cnt[U] + 1;
    } else {
      if (F) {
        ans[i] = 0;
        continue;
      }
      int D = d;
      D -= modpow(2, i, po - 1);
      D += (po - 1);
      D %= (po - 1);
      ans[i] = cnt[D] + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  REP(i, N) cout << ans[i] << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

int calc(int a) {
  int cnt = 0;
  while (a) {
    cnt += a % 2;
    a /= 2;
  }
  return cnt;
}
int modpow(int a, int p, int mo) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {

    int halfP = p / 2;
    int half = modpow(a, halfP, mo);

    return half * half % mo;
  } else {

    return a * modpow(a, p - 1, mo) % mo;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  string S;
  cin >> N >> S;
  int po = 0;
  REP(i, N) if (S[i] == '1') po++;
  VI cnt(N + 10, 0);
  REP(i, cnt.size()) {
    int co = calc(i);
    if (i != 0)
      cnt[i] = cnt[i % co] + 1;
  }
  int u = 0, d = 0;
  reverse(S.begin(), S.end());
  REP(i, N) {
    if (S[i] == '1')
      u += modpow(2, i, po + 1);
    if (S[i] == '1')
      d += modpow(2, i, po - 1);
    u %= (po + 1);
    d %= (po - 1);
  }

  VI ans(N);
  REP(i, N) {
    if (S[i] == '0') {
      int U = u;
      U += modpow(2, i, po + 1);
      U %= (po + 1);
      ans[i] = cnt[U] + 1;
    } else {
      int D = d;
      D -= modpow(2, i, po - 1);
      D += (po - 1);
      D %= (po - 1);
      ans[i] = cnt[D] + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  REP(i, N) cout << ans[i] << '\\n';

  return 0;
}