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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N;
std::string X;

void showBin(ll x) {
  std::vector<ll> vs;

  while (x > 0) {
    vs.emplace_back(x & 1);
    x >>= 1;
  }

  repd(i, vs.size()) std::cout << vs[i];

  std::cout << std::endl;
}

void sim(ll x) {
  while (x > 0) {
    showBin(x);
    x %= __builtin_popcountll(x);
  }

  return;
}

ll mod2(const std::string &x, ll mod) {
  ll ret = 0;

  rep(i, x.size()) {
    ret *= 2;
    ret %= mod;
    ret += x[i] - '0';
    ret %= mod;
  }

  return ret;
}

ll powMod[10];
ll two[200010][3];

int main() {
  std::cin >> N >> X;

  ll pop = 0;

  rep(i, N) pop += X[i] == '1';

  // std::cout << pop << std::endl;

  for (ll s = -1; s <= 1; ++s)
    if (pop + s) {
      powMod[s + 1] = mod2(X, pop + s);
      // std::cout << powMod[s+1] << std::endl;
    }

  two[0][0] = two[0][1] = two[0][2] = 1;

  rep(i, N) for (ll s = -1; s <= 1; ++s) if (pop + s) {
    two[i + 1][s + 1] = two[i][s + 1] * 2 % (pop + s);
  }

  rep(i, N) {
    ll pop2 = pop;
    ll add = 0;

    if (X[i] == '1')
      X[i] = '0', --pop2;
    else
      X[i] = '1', ++pop2;

    ll ans = 0;

    if (!pop2) {
      ans = 0;
      std::cout << ans << std::endl;

      if (X[i] == '1')
        X[i] = '0';
      else
        X[i] = '1';

      continue;
    }

    ll m = (powMod[pop2 - pop + 1] +
            (pop2 - pop) * two[N - i - 1][pop2 - pop + 1] + pop2) %
           pop2;
    ans = 1;

    /*std::cout << "YEs" << std::endl;
    std::cout << m << std::endl;
    std::cout << pop2 << std::endl;
    std::cout << two[21][1] << std::endl;*/

    while (m > 0) {
      m %= __builtin_popcount(m);
      ++ans;
    }

    std::cout << ans << std::endl;

    if (X[i] == '1')
      X[i] = '0';
    else
      X[i] = '1';
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, a) repi(i, 0, a)
#define repdi(i, a, b) for (ll i = (a)-1; i >= (b); --i)
#define repd(i, a) repdi(i, a, 0)
#define itr(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

using ll = long long;
using P = std::pair<ll, ll>;

constexpr ll INF = 1ll << 60;

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

template <class S, class T>
std::ostream &operator<<(std::ostream &out, const std::pair<S, T> &a) {
  std::cout << '(' << a.first << ", " << a.second << ')';
  return out;
}

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &a) {
  std::cout << '[';
  rep(i, a.size()) {
    std::cout << a[i];
    if (i != a.size() - 1)
      std::cout << ", ";
  }
  std::cout << ']';
  return out;
}

ll N;
std::string X;

void showBin(ll x) {
  std::vector<ll> vs;

  while (x > 0) {
    vs.emplace_back(x & 1);
    x >>= 1;
  }

  repd(i, vs.size()) std::cout << vs[i];

  std::cout << std::endl;
}

void sim(ll x) {
  while (x > 0) {
    showBin(x);
    x %= __builtin_popcountll(x);
  }

  return;
}

ll mod2(const std::string &x, ll mod) {
  ll ret = 0;

  rep(i, x.size()) {
    ret *= 2;
    ret %= mod;
    ret += x[i] - '0';
    ret %= mod;
  }

  return ret;
}

ll powMod[10];
ll two[200010][3];

int main() {
  std::cin >> N >> X;

  ll pop = 0;

  rep(i, N) pop += X[i] == '1';

  // std::cout << pop << std::endl;

  for (ll s = -1; s <= 1; ++s) {
    powMod[s + 1] = mod2(X, pop + s);
    // std::cout << powMod[s+1] << std::endl;
  }

  two[0][0] = two[0][1] = two[0][2] = 1;

  rep(i, N) for (ll s = -1; s <= 1; ++s) if (pop + s) {
    two[i + 1][s + 1] = two[i][s + 1] * 2 % (pop + s);
  }

  rep(i, N) {
    ll pop2 = pop;
    ll add = 0;

    if (X[i] == '1')
      X[i] = '0', --pop2;
    else
      X[i] = '1', ++pop2;

    ll ans = 0;

    if (!pop2) {
      ans = 0;
      std::cout << ans << std::endl;

      if (X[i] == '1')
        X[i] = '0';
      else
        X[i] = '1';

      continue;
    }

    ll m = (powMod[pop2 - pop + 1] +
            (pop2 - pop) * two[N - i - 1][pop2 - pop + 1] + pop2) %
           pop2;
    ans = 1;

    /*std::cout << "YEs" << std::endl;
    std::cout << m << std::endl;
    std::cout << pop2 << std::endl;
    std::cout << two[21][1] << std::endl;*/

    while (m > 0) {
      m %= __builtin_popcount(m);
      ++ans;
    }

    std::cout << ans << std::endl;

    if (X[i] == '1')
      X[i] = '0';
    else
      X[i] = '1';
  }

  return 0;
}