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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

map<s64, s64> ans;

s64 popcount(string &x) {
  int ret = 0;
  rep(i, x.size()) { ret += x[i] == '1'; }
  return ret;
}

s64 popcount(s64 x) {
  s64 ret = 0;
  while (x) {
    if (x & 1)
      ++ret;
    x >>= 1;
  }
  return ret;
}

s64 f(string &x, s64 mod) {
  if (mod == 0)
    return 0;
  s64 ret = 0;
  rep(i, x.size()) {
    ret = ret * 2 + (x[i] - '0');
    ret %= mod;
  }
  return ret;
}

s64 calc(s64 x) {
  if (x == 0)
    return 0;
  if (ans.find(x) != ans.end())
    return ans[x];

  s64 pcnt = popcount(x);
  s64 ret = 0;
  ret += calc(x % pcnt) + 1;
  return ans[x] = ret;
}

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

  int n;
  cin >> n;

  string x;
  cin >> x;

  s64 pcnt = popcount(x);
  s64 xmod_plus, xmod_minus;
  if (pcnt == 0) {
    xmod_plus = f(x, pcnt + 1);
  } else if (pcnt == n) {
    if (pcnt - 1 != 0)
      xmod_minus = f(x, pcnt - 1);
  } else {
    xmod_plus = f(x, pcnt + 1);
    if (pcnt - 1 != 0)
      xmod_minus = f(x, pcnt - 1);
  }

  vs64 num_plus(n);
  vs64 num_minus(n);
  if (pcnt != n)
    num_plus[0] = 1 % (pcnt + 1);
  if (pcnt != 0 && pcnt - 1 != 0)
    num_minus[0] = 1 % (pcnt - 1);
  rep(i, n - 1) {
    if (pcnt != n)
      num_plus[i + 1] = num_plus[i] * 2 % (pcnt + 1);
    if (pcnt != 0 && pcnt - 1 != 0)
      num_minus[i + 1] = num_minus[i] * 2 % (pcnt - 1);
  }

  vs64 cnt(n);
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '0') {
      s64 temp = xmod_plus + num_plus[n - i - 1];
      temp %= (pcnt + 1);
      cnt[i] = calc(temp) + 1;
    } else {
      if (pcnt - 1 == 0) {
        cnt[i] = 0;
        continue;
      }
      s64 temp = xmod_minus + (pcnt - 1) - num_minus[n - i - 1];
      temp %= (pcnt - 1);
      cnt[i] = calc(temp) + 1;
    }
  }

  rep(i, n) { cout << cnt[i] << "\\n"; }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for (int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

map<s64, int> ans;

s64 popcount(string &x) {
  int ret = 0;
  rep(i, x.size()) { ret += x[i] == '1'; }
  return ret;
}

s64 popcount(s64 x) {
  s64 ret = 0;
  while (x) {
    if (x & 1)
      ++ret;
    x >>= 1;
  }
  return ret;
}

s64 f(string &x, s64 mod) {
  if (mod == 0)
    return 0;
  s64 ret = 0;
  rep(i, x.size()) {
    ret = ret * 2 + (x[i] - '0');
    ret %= mod;
  }
  return ret;
}

s64 calc(s64 x) {
  if (x == 0)
    return 0;
  if (ans.find(x) != ans.end())
    return ans[x];

  s64 pcnt = popcount(x);
  s64 ret = 0;
  ret += calc(x % pcnt) + 1;
  return ans[x] = ret;
}

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

  int n;
  cin >> n;

  string x;
  cin >> x;

  s64 pcnt = popcount(x);
  s64 xmod_plus, xmod_minus;
  if (pcnt == 0) {
    xmod_plus = f(x, pcnt + 1);
  } else if (pcnt == n) {
    xmod_minus = f(x, pcnt - 1);
  } else {
    xmod_plus = f(x, pcnt + 1);
    xmod_minus = f(x, pcnt - 1);
  }

  vs64 num_plus(n);
  vs64 num_minus(n);
  if (pcnt != n)
    num_plus[0] = 1 % (pcnt + 1);
  if (pcnt != 0)
    num_minus[0] = 1 % (pcnt - 1);
  rep(i, n - 1) {
    if (pcnt != n)
      num_plus[i + 1] = num_plus[i] * 2 % (pcnt + 1);
    if (pcnt != 0 && pcnt - 1 != 0)
      num_minus[i + 1] = num_plus[i] * 2 % (pcnt - 1);
  }

  vs64 cnt(n);
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '0') {
      s64 temp = xmod_plus + num_plus[n - i - 1];
      temp %= (pcnt + 1);
      cnt[i] = calc(temp) + 1;
    } else {
      if (pcnt - 1 == 0) {
        cnt[i] = 0;
        continue;
      }
      s64 temp = xmod_minus + (pcnt - 1) - num_minus[n - i - 1];
      temp %= (pcnt - 1);
      cnt[i] = calc(temp) + 1;
    }
  }

  rep(i, n) { cout << cnt[i] << "\\n"; }
  return 0;
}