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

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef unsigned long long ll;
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;
}

void calc(int val, int div) {
  if (div == 0) {
    cout << 0 << endl;
    return;
  }
  int res = val % div, count = 1;
  while (res) {
    int tmp = __builtin_popcount(res);
    res %= tmp;
    count++;
  }
  cout << count << endl;
}

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

  int first = 0;
  vector<int> x(n);
  rep(i, n) {
    x[i] = s[i] - '0';
    if (x[i])
      first++;
  }

  int pos = 0, neg = 0;
  vector<int> pmod(n), nmod(n);
  pmod[0] = 1;
  nmod[0] = 1;
  rep(i, n) {
    pos = (pos * 2 + x[i]) % (first + 1);
    if (first > 1)
      neg = (neg * 2 + x[i]) % (first - 1);
  }
  rep(i, n - 1) {
    pmod[i + 1] = (pmod[i] * 2) % (first + 1);
    if (first > 1)
      nmod[i + 1] = (nmod[i] * 2) % (first - 1);
  }

  rep(i, n) {
    if (x[i]) {
      calc(neg - nmod[n - 1 - i] + first - 1, first - 1);
    } else {
      calc(pos + pmod[n - 1 - i], first + 1);
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef unsigned long long ll;
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;
}

int pc(string s, vector<ll> &vec) {
  int a = count(s.begin(), s.end(), '1');

  ll next = *vec.rbegin(), first = 0, ans = 0;
  for (auto itr = vec.rbegin(); itr != vec.rend(); ++itr) {
    ll tmp = next % a;
    if (itr != vec.rend() - 1)
      next = (*(itr + 1)) + (tmp << 64);
    else {
      first = tmp;
      ans++;
    }
  }

  while (first != 0) {
    int i = 0, a = 0;
    while (first >> i != 0) {
      if ((first >> i) & 1)
        a++;
      ++i;
    }
    first = first % a;
    ans++;
  }
  return ans;
}

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

  for (int i = 0; i < n; ++i) {
    string nstr(tstr);
    if (tstr[i] == '1')
      nstr[i] = '0';
    else
      nstr[i] = '1';
    int a = nstr.size() / 64;
    vector<ll> num(a + 1);

    for (int i = 0; i < a + 1; ++i) {
      stringstream sstr;
      if (i == a) {
        string tmp(nstr.rbegin() + i * 64, nstr.rend());
        sstr << string(tmp.rbegin(), tmp.rend());
        bitset<64> bs;
        sstr >> bs;
        num[i] = bs.to_ullong();
        // cout << num[i] <<endl;
      } else {
        string tmp(nstr.rbegin() + i * 64, nstr.rbegin() + (i + 1) * 64);
        sstr << string(tmp.rbegin(), tmp.rend());
        bitset<64> bs;
        sstr >> bs;
        num[i] = bs.to_ullong();
      }
    }

    int ans = pc(nstr, num);
    cout << ans << endl;
  }

  return 0;
}