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

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

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;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

// x^n(mod M)
ll mpow(ll x, ll n, ll mod) {
  ll ans = 1ll;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

int main(int argc, char const *argv[]) {
  // while (true) {
  /*
  std::random_device rnd; // 非決定的な乱数生成器を生成
  std::mt19937 mt(rnd()); //
  メルセンヌ・ツイスタの32ビット版、引数は初期シード値
  std::uniform_int_distribution<> rand100(3, 1140);
  int n = rand100(mt); //cin>>n;
  string s(n,'0'); //cin>>s;
  rep(i,n) {
      s[i] = rand()%2+'0';
  }
  cout << n << '\\n';
  cout << s << '\\n';
  */
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> a(n + 10);
  auto calc = [&](ll x) {
    ll ret = 0;
    while (x > 0) {
      ll pop = __builtin_popcount(x);
      x = x % pop;
      ret++;
    }
    return ret;
  };
  for (int i = 0; i <= n; ++i)
    a[i] = calc(i);

  ll cnt = 0;
  rep(i, n) if (s[i] == '1') cnt++;
  ll mod_1 = cnt + 1;
  ll mod_2 = cnt - 1;
  ll sum_1 = 0;
  ll sum_2 = 0;
  reverse(s.begin(), s.end());

  rep(i, n) {
    if (s[i] == '0')
      continue;
    sum_1 += mpow(2ll, i, mod_1) % mod_1;
    sum_1 %= mod_1;
    if (mod_2 == 0)
      continue;
    sum_2 += mpow(2ll, i, mod_2) % mod_2;
    sum_2 %= mod_2;
  }

  vector<ll> ans(n);
  rep(i, n) {
    if (s[i] == '0') {
      ll tmp = sum_1;
      tmp += mpow(2ll, i, mod_1);
      tmp %= mod_1;
      ans[i] = a[tmp] + 1;
    } else {
      ll tmp = sum_2;
      if (mod_2 == 0) {
        ans[i] = 0;
        continue;
      }
      tmp %= mod_2;
      tmp -= mpow(2ll, i, mod_2);
      if (tmp < 0)
        tmp += mod_2;
      tmp %= mod_2;
      if (tmp < 0)
        tmp += mod_2;
      ans[i] = a[tmp] + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans[i] << '\\n';
  //}
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9 + 7;
const long long LINF = 1e18;
#define dump(x) cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template <typename T> void print(const vector<T> &x) {
  int n = x.size();
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x) {
  int n = x.size();
  rep(i, n) {
    rep(j, x[i].size()) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

template <typename T> void print(const vector<T> &x, int n) {
  rep(i, n) {
    cout << x[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

template <typename T> void print(const vector<vector<T>> &x, int n, int m) {
  rep(i, n) {
    rep(j, m) { cout << x[i][j] << " "; }
    cout << endl;
  }
}

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;
}

void input_init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

// x^n(mod M)
ll mpow(ll x, ll n, ll mod) {
  ll ans = 1ll;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    n = n >> 1;
  }
  return ans;
}

#include <cstdint>

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    u64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> a(n + 10);
  auto calc = [&](ll x) {
    ll ret = 0;
    while (x > 0) {
      ll pop = __builtin_popcount(x);
      x = x % pop;
      ret++;
    }
    return ret;
  };
  for (int i = 0; i <= n; ++i)
    a[i] = calc(i);

  ll cnt = 0;
  rep(i, n) if (s[i] == '1') cnt++;
  ll mod_1 = cnt + 1;
  ll mod_2 = cnt - 1;
  ll sum_1 = 0;
  ll sum_2 = 0;
  reverse(s.begin(), s.end());

  rep(i, n) {
    if (s[i] == '0')
      continue;
    sum_1 += mpow(2ll, i, mod_1) % mod_1;
    sum_1 %= mod_1;
    sum_2 += mpow(2ll, i, mod_2) % mod_2;
    sum_2 %= mod_2;
  }

  vector<ll> ans(n);
  rep(i, n) {
    if (s[i] == '0') {
      ll tmp = sum_1;
      tmp += mpow(2ll, i, mod_1);
      tmp %= mod_1;
      ans[i] = a[tmp] + 1;
    } else {
      ll tmp = sum_2;
      tmp %= mod_2;
      tmp -= mpow(2ll, i, mod_2);
      if (tmp < 0)
        tmp += mod_2;
      tmp %= mod_2;
      if (tmp < 0)
        tmp += mod_2;
      ans[i] = a[tmp] + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  rep(i, n) cout << ans[i] << '\\n';
  return 0;
}