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

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const deque<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const unordered_set<T> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << '\\n';
  else
    cout << "NO" << '\\n';
}
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << '\\n';
  else
    cout << "No" << '\\n';
}
template <class T> T gcd(T a, T b) {
  while (b) {
    T tmp = b;
    b = a % b;
    a = tmp;
  }
  return a;
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

long long qpow(long long a, long long b, int MOD) {
  if (b == 0)
    return 1;
  long long res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

const int MOD = 1e9 + 7;
const int maxn = 2e6;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
  fac[0] = 1;
  for (int i = 1; i <= maxn; i++)
    fac[i] = 1LL * fac[i - 1] * i % MOD;
  invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
  for (int i = maxn - 1; i >= 0; i--)
    invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
  assert(0 <= k && n >= k);
  int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
  return (int)(1LL * fac[n] * tmp % MOD);
}
// vector<int> fa(n);
// vector<int> sz(n, 1);
// iota(fa.begin(), fa.end(), 0);
// function<int(int)> Find = [&](int x) {
//     return x == fa[x] ? x : fa[x] = Find(fa[x]);
// };
// function<bool(int, int)> Union = [&](int x, int y) {
//     int px = Find(x), py = Find(y);
//     if (px == py) return false;
//     if (sz[px] > sz[py]) swap(px, py);
//     fa[px] = py;
//     sz[py] += sz[px];
//     return true;
// };

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n;
  string s;
  cin >> n >> s;
  vector<int> dp(n + 20);
  int cnt = 0;
  for (int i = 1; i <= n + 10; i++) {
    dp[i] = 1 + dp[i % (__builtin_popcount(i))];
    if (i <= n)
      cnt += s[i - 1] == '1';
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << '\\n';
    return 0;
  }

  // trace(dp);

  LL small = 0, big = 0;
  for (int i = n - 1, j = 0; ~i; i--, j++) {
    if (s[i] == '1') {
      if (cnt > 1)
        small += qpow(2, j, cnt - 1);
      big += qpow(2, j, cnt + 1);
      if (cnt > 1)
        small %= (cnt - 1);
      big %= (cnt + 1);
    }
  }
  // trace(cnt, small, big);
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    if (s[i] == '0') {
      // trace(big, 2, j, cnt + 1, qpow(2, j, cnt+ 1));
      LL now = big + qpow(2, j, cnt + 1);
      now %= (cnt + 1);
      cout << 1 + dp[now] << '\\n';
      // trace(big, now);
    } else {
      if (cnt == 1) {
        cout << 0 << '\\n';
        continue;
      }
      LL now = small - qpow(2, j, cnt - 1);
      now = (now + cnt - 1) % (cnt - 1);
      cout << 1 + dp[now] << '\\n';
      // trace(small, now);
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using LD = long double;
using PII = pair<int, int>;
using PLI = pair<long long, int>;
using PLL = pair<long long, long long>;
using TI = tuple<int, int, int>;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
  return out << "(" << p.first << ", " << p.second << ")";
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const deque<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, const unordered_set<T> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << '\\n';
  else
    cout << "NO" << '\\n';
}
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << '\\n';
  else
    cout << "No" << '\\n';
}
template <class T> T gcd(T a, T b) {
  while (b) {
    T tmp = b;
    b = a % b;
    a = tmp;
  }
  return a;
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

long long qpow(long long a, long long b, int MOD) {
  if (b == 0)
    return 1;
  long long res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

const int MOD = 1e9 + 7;
const int maxn = 2e6;
int fac[maxn + 5];
int invfac[maxn + 5];
void init() {
  fac[0] = 1;
  for (int i = 1; i <= maxn; i++)
    fac[i] = 1LL * fac[i - 1] * i % MOD;
  invfac[maxn] = (int)(qpow(fac[maxn], MOD - 2, MOD));
  for (int i = maxn - 1; i >= 0; i--)
    invfac[i] = 1LL * invfac[i + 1] * (i + 1) % MOD;
}
int C(int n, int k) {
  assert(0 <= k && n >= k);
  int tmp = (int)(1LL * invfac[k] * invfac[n - k] % MOD);
  return (int)(1LL * fac[n] * tmp % MOD);
}
// vector<int> fa(n);
// vector<int> sz(n, 1);
// iota(fa.begin(), fa.end(), 0);
// function<int(int)> Find = [&](int x) {
//     return x == fa[x] ? x : fa[x] = Find(fa[x]);
// };
// function<bool(int, int)> Union = [&](int x, int y) {
//     int px = Find(x), py = Find(y);
//     if (px == py) return false;
//     if (sz[px] > sz[py]) swap(px, py);
//     fa[px] = py;
//     sz[py] += sz[px];
//     return true;
// };

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n;
  string s;
  cin >> n >> s;
  vector<int> dp(n + 1);
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = 1 + dp[i % (__builtin_popcount(i))];
    cnt += s[i - 1] == '1';
  }
  // trace(dp);

  LL small = 0, big = 0;
  for (int i = n - 1, j = 0; ~i; i--, j++) {
    if (s[i] == '1') {
      small += qpow(2, j, cnt - 1);
      big += qpow(2, j, cnt + 1);
      small %= (cnt - 1);
      big %= (cnt + 1);
    }
  }
  // trace(cnt, small, big);
  for (int i = 0, j = n - 1; i < n; i++, j--) {
    if (s[i] == '0') {
      // trace(big, 2, j, cnt + 1, qpow(2, j, cnt+ 1));
      LL now = big + qpow(2, j, cnt + 1);
      now %= (cnt + 1);
      cout << 1 + dp[now] << '\\n';
      // trace(big, now);
    } else {
      LL now = small - qpow(2, j, cnt - 1);
      now = (now + cnt - 1) % (cnt - 1);
      cout << 1 + dp[now] << '\\n';
      // trace(small, now);
    }
  }

  return 0;
}