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

#include <bits/stdc++.h>
using namespace std;
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 debug() { cerr << "\\n"; }
template <class T> void debug(const T &x) { cerr << x << "\\n"; }
template <class T, class... Args> void debug(const T &x, const Args &...args) {
  cerr << x << " ";
  debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
  for (const T &x : v) {
    cerr << x << " ";
  }
  cerr << "\\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

vector<ll> bfs(ll N, vector<vector<ll>> &g) {
  vector<ll> d(N + 10, LLINF);
  queue<ll> que;
  que.push(0);
  d[0] = 0;
  while (que.size()) {
    ll now = que.front();
    que.pop();
    for (auto nxt : g[now]) {
      if (d[nxt] > d[now] + 1) {
        d[nxt] = d[now] + 1;
        que.push(nxt);
      }
    }
  }
  return d;
}

ll pow_mod(ll n, ll k, ll mod) {
  if (k == 0) {
    return 1;
  } else if (k % 2 == 1) {
    return pow_mod(n, k - 1, mod) * n % mod;
  } else {
    ll t = pow_mod(n, k / 2, mod);
    return t * t % mod;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  string s;
  cin >> N >> s;
  vector<vector<ll>> g(N + 10);
  for (ll i = 1; i < N + 1; i++) {
    ll pcnt = __builtin_popcountll(i);
    g[i % pcnt].emplace_back(i);
  }
  auto cnt = bfs(N, g);
  // debugVector(cnt);
  ll pcnt = 0;
  for (ll i = 0; i < N; i++) {
    pcnt += (s[i] == '1');
  }
  if (pcnt == 0) {
    for (ll i = 0; i < N; i++) {
      cout << 1 << "\\n";
    }
    return 0;
  }
  map<ll, ll> sum;
  for (ll mod = pcnt - 1; mod <= pcnt + 1; mod++) {
    if (mod == 0) {
      sum[mod] = 0;
      continue;
    }
    ll res = 0;
    for (ll i = 0; i < N; i++) {
      if (s[i] == '1') {
        res += pow_mod(2, (N - i - 1), mod);
        res %= mod;
      }
    }
    sum[mod] = res;
  }
  for (ll i = 0; i < N; i++) {
    ll res;
    if (s[i] == '1') {
      ll mod = pcnt - 1;
      if (mod == 0) {
        cout << 0 << "\\n";
        continue;
      }
      res = sum[mod] - pow_mod(2, (N - i - 1), mod) + mod;
      res %= mod;
      assert(0 <= res && res < cnt.size());
      cout << cnt[res] + 1 << "\\n";
    } else {
      ll mod = pcnt + 1;
      res = sum[mod] + pow_mod(2, (N - i - 1), mod);
      res %= mod;
      assert(0 <= res && res < cnt.size());
      cout << cnt[res] + 1 << "\\n";
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
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 debug() { cerr << "\\n"; }
template <class T> void debug(const T &x) { cerr << x << "\\n"; }
template <class T, class... Args> void debug(const T &x, const Args &...args) {
  cerr << x << " ";
  debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
  for (const T &x : v) {
    cerr << x << " ";
  }
  cerr << "\\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

vector<ll> bfs(ll N, vector<vector<ll>> &g) {
  vector<ll> d(N + 10, LLINF);
  queue<ll> que;
  que.push(0);
  d[0] = 0;
  while (que.size()) {
    ll now = que.front();
    que.pop();
    for (auto nxt : g[now]) {
      if (d[nxt] > d[now] + 1) {
        d[nxt] = d[now] + 1;
        que.push(nxt);
      }
    }
  }
  return d;
}

ll pow_mod(ll n, ll k, ll mod) {
  if (k == 0) {
    return 1;
  } else if (k % 2 == 1) {
    return pow_mod(n, k - 1, mod) * n % mod;
  } else {
    ll t = pow_mod(n, k / 2, mod);
    return t * t % mod;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N;
  string s;
  cin >> N >> s;
  vector<vector<ll>> g(N + 10);
  for (ll i = 1; i < N + 1; i++) {
    ll pcnt = __builtin_popcountll(i);
    g[i % pcnt].emplace_back(i);
  }
  auto cnt = bfs(N, g);
  // debugVector(cnt);
  ll pcnt = 0;
  for (ll i = 0; i < N; i++) {
    pcnt += (s[i] == '1');
  }
  map<ll, ll> sum;
  for (ll mod = pcnt - 1; mod <= pcnt + 1; mod++) {
    ll res = 0;
    for (ll i = 0; i < N; i++) {
      if (s[i] == '1') {
        res += pow_mod(2, (N - i - 1), mod);
        res %= mod;
      }
    }
    sum[mod] = res;
  }
  for (ll i = 0; i < N; i++) {
    ll res;
    if (s[i] == '1') {
      ll mod = pcnt - 1;
      res = sum[mod] - pow_mod(2, (N - i - 1), mod) + mod;
      res %= mod;
      cout << cnt[res] + 1 << "\\n";
    } else {
      ll mod = pcnt + 1;
      res = sum[mod] + pow_mod(2, (N - i - 1), mod);
      res %= mod;
      cout << cnt[res] + 1 << "\\n";
    }
  }
}