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

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
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;
}

int memo[200005];

int calc(int n) {
  if (n == 0)
    return 0;
  if (memo[n] != -1)
    return memo[n];
  int p = 0, m = n;
  while (m > 0) {
    p += m % 2;
    m /= 2;
  }
  if (p == 0)
    return 0;
  return calc(n % p) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  if (n == 1) {
    if (s[0] == '0')
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  memset(memo, -1, sizeof(memo));
  memo[0] = 0;
  for (int i = 1; i <= 200002; ++i) {
    memo[i] = calc(i);
    // if(i <= 10) cout << i << " " << memo[i] << endl;
  }
  int origin_cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++origin_cnt;
  }
  // cout << origin_cnt << endl;
  int lower_cnt = origin_cnt - 1;
  chmax(lower_cnt, 1);
  int upper_cnt = origin_cnt + 1;
  int lower_num = 0;
  int upper_num = 0;
  rep(i, n) {
    lower_num = (lower_num * 2 + (s[i] - '0')) % lower_cnt;
    upper_num = (upper_num * 2 + (s[i] - '0')) % upper_cnt;
  }
  // cout << "lower_num: " << lower_num << endl;
  // cout << "upper_num: " << upper_num << endl;
  vector<int> ans(n, 0);
  int lower_two = 1, upper_two = 1;
  for (int i = n - 1; i >= 0; --i) {
    int num = 0;
    if (s[i] == '1') {
      if (origin_cnt != 1) {
        num = (lower_num + lower_cnt - lower_two) % lower_cnt;
        ans[i] = memo[num] + 1;
      } else {
        ans[i] = 0;
      }
    } else {
      num = (upper_num + upper_two) % upper_cnt;
      ans[i] = memo[num] + 1;
    }
    // cout << "ans[" << i << "] = " << ans[i] << ", num = " << num << endl;
    lower_two = (lower_two * 2) % lower_cnt;
    upper_two = (upper_two * 2) % upper_cnt;
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
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;
}

int memo[200005];

int calc(int n) {
  if (n == 0)
    return 0;
  if (memo[n] != -1)
    return memo[n];
  int p = 0, m = n;
  while (m > 0) {
    p += m % 2;
    m /= 2;
  }
  return calc(n % p) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  if (n == 1) {
    if (s[0] == '0')
      cout << 1 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  memset(memo, -1, sizeof(memo));
  memo[0] = 0;
  for (int i = 1; i <= 200004; ++i) {
    memo[i] = calc(i);
    // if(i <= 10) cout << i << " " << memo[i] << endl;
  }
  int origin_cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++origin_cnt;
  }
  // cout << origin_cnt << endl;
  int lower_cnt = origin_cnt - 1;
  int upper_cnt = origin_cnt + 1;
  int lower_num = 0;
  int upper_num = 0;
  rep(i, n) {
    lower_num = (lower_num * 2 + (s[i] - '0')) % lower_cnt;
    upper_num = (upper_num * 2 + (s[i] - '0')) % upper_cnt;
  }
  // cout << "lower_num: " << lower_num << endl;
  // cout << "upper_num: " << upper_num << endl;
  vector<int> ans(n, 0);
  int lower_two = 1, upper_two = 1;
  for (int i = n - 1; i >= 0; --i) {
    int num = 0;
    if (s[i] == '1') {
      if (lower_cnt != 0) {
        num = (lower_num + lower_cnt - lower_two) % lower_cnt;
        ans[i] = memo[num] + 1;
      } else {
        ans[i] = 0;
      }
    } else {
      num = (upper_num + upper_two) % upper_cnt;
      ans[i] = memo[num] + 1;
    }
    // cout << "ans[" << i << "] = " << ans[i] << ", num = " << num << endl;
    if (lower_cnt != 0)
      lower_two = (lower_two * 2) % lower_cnt;
    upper_two = (upper_two * 2) % upper_cnt;
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}