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

#include <bits/stdc++.h>
using namespace std;

//////////////////

#ifdef DEBUG
#include "debug.h"
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif

// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

//////////////////

#define int long long

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

#define YES puts("YES")
#define Yes puts("Yes")
#define NO puts("NO")
#define No puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

//*
#define mod                                                                    \\
  1000000007 /*/                                                               \\
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL << 60)

// a^k mod p
int modpow(int a, int k, int p) {
  int ans = 1;
  while (k > 0) {
    if (k % 2 == 1) {
      ans *= a;
      ans %= p;
    }
    a = (a * a) % p;
    k = k / 2;
  }
  return ans;
}

void solve() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int p = 0;
  rep(i, N) if (X[i] == '1') p++;

  // p+1になるケース：0->1に反転
  int rem_plus = 0;
  rep(i, N) {
    if (X[i] == '1')
      rem_plus = (rem_plus * 2 + 1) % (p + 1);
    else
      rem_plus = (rem_plus * 2) % (p + 1);
  }
  // p-1
  int rem_minus = 0;
  if (p > 1) {
    rep(i, N) {
      if (X[i] == '1')
        rem_minus = (rem_minus * 2 + 1) % (p - 1);
      else
        rem_minus = (rem_minus * 2) % (p - 1);
    }
  }

  rep(i, N) {
    int n;
    int ans = 1;
    if (X[i] == '0') {
      n = (rem_plus + modpow(2, N - i - 1, p + 1)) % (p + 1);
    } else {
      if (p == 1) {
        cout << 0 << endl;
        continue;
      } else
        n = (rem_minus + (p - 1) - modpow(2, N - i - 1, p - 1)) % (p - 1);
    }
    while (n > 0) {
      n %= __builtin_popcountll(n);
      ans++;
    }
    cout << ans << endl;
  }
}

signed main() {
  cout << fixed << setprecision(18);
  cerr << fixed << setprecision(18);
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

//////////////////

#ifdef DEBUG
#include "debug.h"
#define dump(...)                                                              \\
  DUMPOUT << "  " << string(#__VA_ARGS__) << ": "                              \\
          << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl  \\
          << "    ",                                                           \\
      dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif

// http://beet-aizu.hatenablog.com/entry/2018/04/08/145516
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}

//////////////////

#define int long long

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

#define YES puts("YES")
#define Yes puts("Yes")
#define NO puts("NO")
#define No puts("No")
#define ALL(v) (v).begin(), (v).end()
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
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 (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

//*
#define mod                                                                    \\
  1000000007 /*/                                                               \\
#define mod 998244353 //*/

typedef pair<int, int> P;
#define INF (1LL << 60)

// a^k mod p
int modpow(int a, int k, int p) {
  int ans = 1;
  while (k > 0) {
    if (k % 2 == 1) {
      ans *= a;
      ans %= p;
    }
    a = (a * a) % p;
    k = k / 2;
  }
  return ans;
}

void solve() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  int p = 0;
  rep(i, N) if (X[i] == '1') p++;

  // p+1になるケース：0->1に反転
  int rem_plus = 0;
  rep(i, N) {
    if (X[i] == '1')
      rem_plus = (rem_plus * 2 + 1) % (p + 1);
    else
      rem_plus = (rem_plus * 2) % (p + 1);
  }
  // p-1
  int rem_minus = 0;
  rep(i, N) {
    if (X[i] == '1')
      rem_minus = (rem_minus * 2 + 1) % (p - 1);
    else
      rem_minus = (rem_minus * 2) % (p - 1);
  }

  dump(rem_plus, rem_minus);

  rep(i, N) {
    int n;
    int ans = 1;
    if (X[i] == '0') {
      n = (rem_plus + modpow(2, N - i - 1, p + 1)) % (p + 1);
    } else
      n = (rem_minus + (p - 1) - modpow(2, N - i - 1, p - 1)) % (p - 1);

    while (n > 0) {
      if (i == 1)
        dump(n);
      n %= __builtin_popcountll(n);
      ans++;
    }
    cout << ans << endl;
  }
}

signed main() {
  cout << fixed << setprecision(18);
  cerr << fixed << setprecision(18);
  solve();
}