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

// 現状たぶん全然動かない。非常に桁数が大きいので、文字列のまま1度modを取らないとintとして扱えない。

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (ll i = (begin)-1; i >= (end); i--)
#define IREP(i, n) IFOR(i, n, 0)
#define SORT(a) sort(a.begin(), a.end())
#define ISORT(a) sort(a.begin(), a.end(), greater<ll>())
#define REVERSE(a) reverse(a.begin(), a.end())
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v)                                                              \\
  cout << #v << "=(" << v[0];                                                  \\
  FOR(i_debug, 1, v.size()) { cout << "," << v[i_debug]; }                     \\
  cout << ")" << endl;
#define mdebug(m)                                                              \\
  cout << #m << "=" << endl;                                                   \\
  REP(i_debug, m.size()) {                                                     \\
    REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; }    \\
    cout << endl;                                                              \\
  }
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)

#define ll long long
// #define f first
// #define s second
#define mp make_pair
#define pb push_back
#define INF 1000000000000000000

using vec = vector<ll>;
using mat = vector<vec>;
using Pii = pair<ll, ll>;
using PiP = pair<ll, Pii>;
using PPi = pair<Pii, ll>;
using bvec = vector<bool>;
using Pvec = vector<Pii>;

template <typename T> void readv(vector<T> &a) {
  REP(i, a.size())
  cin >> a[i];
}
void readv_m1(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

ll mod = 1000000007;

ll popcount(ll x) {
  ll retval = 0;
  while (x > 0) {
    if (x % 2 == 1)
      retval++;
    x /= 2;
  }
  return retval;
}

ll return_f(ll x) {
  ll retval = 1;
  while (x > 0) {
    x %= popcount(x);
    retval++;
  }
  return retval;
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;

  ll init_popcount = 0;
  REP(i, N) {
    if (X[i] == '1')
      init_popcount++;
  }

  vec modtwoinitzero(N + 1, 0);
  vec modtwoinitplus(N + 1);
  vec modtwoinitminus(N + 1, 0);
  ll current_zero = 1;
  ll current_plus = 1;
  ll current_minus = 1;
  REP(i, N + 1) {
    if (init_popcount > 0) {
      modtwoinitzero[i] = current_zero;
      current_zero *= 2;
      current_zero %= init_popcount;
    }
    modtwoinitplus[i] = current_plus;
    current_plus *= 2;
    current_plus %= init_popcount + 1;
    if (init_popcount > 1) {
      modtwoinitminus[i] = current_minus;
      current_minus *= 2;
      current_minus %= init_popcount - 1;
    }
  }

  ll x = 0;
  ll x_plus = 0;
  ll x_minus = 0;
  REP(i, N) {
    if (X[i] == '1') {
      x += modtwoinitzero[N - i - 1];
      x_plus += modtwoinitplus[N - i - 1];
      x_minus += modtwoinitminus[N - i - 1];
      if (init_popcount > 0)
        x %= init_popcount;
      x_plus %= init_popcount + 1;
      if (init_popcount > 1)
        x_minus %= init_popcount - 1;
    }
  }
  REP(i, N) {
    ll current_x = 0;
    if (X[i] == '1') {
      if (init_popcount > 1) {
        current_x = (x_minus - modtwoinitminus[N - i - 1] + init_popcount - 1) %
                    (init_popcount - 1);
        cout << return_f(current_x) << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      current_x = (x_plus + modtwoinitplus[N - i - 1] + init_popcount + 1) %
                  (init_popcount + 1);
      cout << return_f(current_x) << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

// 現状たぶん全然動かない。非常に桁数が大きいので、文字列のまま1度modを取らないとintとして扱えない。

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define IFOR(i, begin, end) for (ll i = (begin)-1; i >= (end); i--)
#define IREP(i, n) IFOR(i, n, 0)
#define SORT(a) sort(a.begin(), a.end())
#define ISORT(a) sort(a.begin(), a.end(), greater<ll>())
#define REVERSE(a) reverse(a.begin(), a.end())
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v)                                                              \\
  cout << #v << "=(" << v[0];                                                  \\
  FOR(i_debug, 1, v.size()) { cout << "," << v[i_debug]; }                     \\
  cout << ")" << endl;
#define mdebug(m)                                                              \\
  cout << #m << "=" << endl;                                                   \\
  REP(i_debug, m.size()) {                                                     \\
    REP(j_debug, m[i_debug].size()) { cout << m[i_debug][j_debug] << ","; }    \\
    cout << endl;                                                              \\
  }
#define Lower_bound(v, x)                                                      \\
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x)                                                      \\
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)

#define ll long long
// #define f first
// #define s second
#define mp make_pair
#define pb push_back
#define INF 1000000000000000000

using vec = vector<ll>;
using mat = vector<vec>;
using Pii = pair<ll, ll>;
using PiP = pair<ll, Pii>;
using PPi = pair<Pii, ll>;
using bvec = vector<bool>;
using Pvec = vector<Pii>;

template <typename T> void readv(vector<T> &a) {
  REP(i, a.size())
  cin >> a[i];
}
void readv_m1(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

ll mod = 1000000007;

ll popcount(ll x) {
  ll retval = 0;
  while (x > 0) {
    if (x % 2 == 1)
      retval++;
    x /= 2;
  }
  return retval;
}

ll return_f(ll x) {
  ll retval = 1;
  while (x > 0) {
    x %= popcount(x);
    retval++;
  }
  return retval;
}

int main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;

  ll init_popcount = 0;
  REP(i, N) {
    if (X[i] == '1')
      init_popcount++;
  }

  vec modtwoinitzero(N + 1);
  vec modtwoinitplus(N + 1);
  vec modtwoinitminus(N + 1, 0);
  ll current_zero = 1;
  ll current_plus = 1;
  ll current_minus = 1;
  REP(i, N + 1) {
    modtwoinitzero[i] = current_zero;
    current_zero *= 2;
    current_zero %= init_popcount;
    modtwoinitplus[i] = current_plus;
    current_plus *= 2;
    current_plus %= init_popcount + 1;
    if (init_popcount > 1) {
      modtwoinitminus[i] = current_minus;
      current_minus *= 2;
      current_minus %= init_popcount - 1;
    }
  }

  ll x = 0;
  ll x_plus = 0;
  ll x_minus = 0;
  REP(i, N) {
    if (X[i] == '1') {
      x += modtwoinitzero[N - i - 1];
      x_plus += modtwoinitplus[N - i - 1];
      x_minus += modtwoinitminus[N - i - 1];
      x %= init_popcount;
      x_plus %= init_popcount + 1;
      if (init_popcount > 1)
        x_minus %= init_popcount - 1;
    }
  }
  REP(i, N) {
    ll current_x = 0;
    if (X[i] == '1') {
      if (init_popcount > 1) {
        current_x = (x_minus - modtwoinitminus[N - i - 1] + init_popcount - 1) %
                    (init_popcount - 1);
        cout << return_f(current_x) << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      current_x = (x_plus + modtwoinitplus[N - i - 1] + init_popcount + 1) %
                  (init_popcount + 1);
      cout << return_f(current_x) << endl;
    }
  }
  return 0;
}