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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using lint = int64_t;
using namespace std;

#define rep(i, n) for (lint i = 0; i < (n); ++i)
#define rep1(i, n) for (lint i = 1; i < (n); ++i)
#define repn(i, a, b) for (lint i = (a); i < (b); ++i)
#define Rep(i, n) for (lint i = (n); i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define cend printf("\\n")

using P = pair<int, int>;
using D = pair<double, double>;
// constexpr lint mod = 998'244'353LL;
constexpr lint mod = 1000'000'007LL;
constexpr lint inf = 3LL * mod * mod;
const double Pi = 2.0 * acos(0.0);

template <class T> using prique = priority_queue<T>;
template <class T> using prique_inv = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U>
inline istream &operator>>(istream &is, pair<T, U> &rhs) {
  return is >> rhs.first >> rhs.second;
}
template <class T, class U>
inline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
  return os << rhs.first << " " << rhs.second << "\\n";
}

constexpr bool pri(lint x) {
  for (lint i = 2; i * i <= x; ++i)
    if (x % i == 0)
      return false;
  return 1 < x;
}

double dis(D a, D b) { return hypot(a.first - b.first, a.second - b.second); }

lint fact[3000000];
void fact_init(lint n) {
  if (3000000 <= n)
    return;
  fact[0] = fact[1] = 1;
  for (lint i = 2; i <= n; ++i) {
    fact[i] = i * fact[i - 1];
    if (mod <= fact[i])
      fact[i] %= mod;
  }
}

lint modpow(lint x, lint n, lint m = mod) {
  lint res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= x;
      if (m <= res)
        res %= m;
    }
    x *= x;
    if (m <= x)
      x %= m;
    n >>= 1;
  }
  return res;
}

lint intpow(lint x, lint n) {
  lint res = 1;
  while (n > 0) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}

lint comb(lint n, lint r) {
  if (r == 0 || r == n)
    return 1;
  lint res = fact[n] * modpow(fact[n - r], mod - 2, mod) % mod *
             modpow(fact[r], mod - 2, mod) % mod;
  return res < 0 ? res + mod : res;
}

map<lint, lint> factring(lint n) {
  map<lint, lint> res;
  for (lint i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      n /= i;
      ++res[i];
    }
  }
  if (n != 1)
    ++res[n];
  return res;
}

lint n, cnt, cnt1, cnt2;
bitset<2 * 100010> x;

lint f(lint a) {
  lint res = 1;
  while (a != 0) {
    a %= bitset<18>(a).count();
    ++res;
  }
  return res;
}
int main() {
  cin >> n >> x;
  cnt = x.count();
  if (cnt != 1) {
    rep(i, n) {
      if (x[i]) {
        cnt1 += modpow(2, i, cnt - 1);
        cnt1 %= cnt - 1;
        cnt2 += modpow(2, i, cnt + 1);
        cnt2 %= cnt + 1;
      }
    }
  }
  Rep(i, n - 1) {
    if (cnt != 1) {
      lint t1 = modpow(2, i, cnt - 1), t2 = modpow(2, i, cnt + 1);
      if (x[i]) {
        cout << f((cnt1 - t1 + cnt - 1) % (cnt - 1)) << "\\n";
      } else {
        cout << f((cnt2 + t2) % (cnt + 1)) << "\\n";
      }
    } else {
      if (x[i])
        cout << "0\\n";
      else {
        if (!x[0] && (i != 0))
          cout << "1\\n";
        else
          cout << "2\\n";
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using lint = int64_t;
using namespace std;

#define rep(i, n) for (lint i = 0; i < (n); ++i)
#define rep1(i, n) for (lint i = 1; i < (n); ++i)
#define repn(i, a, b) for (lint i = (a); i < (b); ++i)
#define Rep(i, n) for (lint i = (n); i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define cend printf("\\n")

using P = pair<int, int>;
using D = pair<double, double>;
// constexpr lint mod = 998'244'353LL;
constexpr lint mod = 1000'000'007LL;
constexpr lint inf = 3LL * mod * mod;
const double Pi = 2.0 * acos(0.0);

template <class T> using prique = priority_queue<T>;
template <class T> using prique_inv = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U>
inline istream &operator>>(istream &is, pair<T, U> &rhs) {
  return is >> rhs.first >> rhs.second;
}
template <class T, class U>
inline ostream &operator<<(ostream &os, const pair<T, U> &rhs) {
  return os << rhs.first << " " << rhs.second << "\\n";
}

constexpr bool pri(lint x) {
  for (lint i = 2; i * i <= x; ++i)
    if (x % i == 0)
      return false;
  return 1 < x;
}

double dis(D a, D b) { return hypot(a.first - b.first, a.second - b.second); }

lint fact[3000000];
void fact_init(lint n) {
  if (3000000 <= n)
    return;
  fact[0] = fact[1] = 1;
  for (lint i = 2; i <= n; ++i) {
    fact[i] = i * fact[i - 1];
    if (mod <= fact[i])
      fact[i] %= mod;
  }
}

lint modpow(lint x, lint n, lint m = mod) {
  lint res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= x;
      if (m <= res)
        res %= m;
    }
    x *= x;
    if (m <= x)
      x %= m;
    n >>= 1;
  }
  return res;
}

lint intpow(lint x, lint n) {
  lint res = 1;
  while (n > 0) {
    if (n & 1)
      res *= x;
    x *= x;
    n >>= 1;
  }
  return res;
}

lint comb(lint n, lint r) {
  if (r == 0 || r == n)
    return 1;
  lint res = fact[n] * modpow(fact[n - r], mod - 2, mod) % mod *
             modpow(fact[r], mod - 2, mod) % mod;
  return res < 0 ? res + mod : res;
}

map<lint, lint> factring(lint n) {
  map<lint, lint> res;
  for (lint i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      n /= i;
      ++res[i];
    }
  }
  if (n != 1)
    ++res[n];
  return res;
}

lint n, cnt, cnt1, cnt2;
bitset<2 * 100010> x;

lint f(lint a) {
  lint res = 1, c;
  while (a != 0) {
    c = bitset<18>(a).count();
    a %= c;
    ++res;
  }
  return res;
}
int main() {
  cin >> n >> x;
  cnt = x.count();
  rep(i, n) {
    if (x[i]) {
      cnt1 += modpow(2, i, cnt - 1);
      cnt1 %= cnt - 1;
      cnt2 += modpow(2, i, cnt + 1);
      cnt2 %= cnt + 1;
    }
  }
  Rep(i, n - 1) {
    lint t1 = modpow(2, i, cnt - 1), t2 = modpow(2, i, cnt + 1);
    if (x[i]) {
      cout << f((cnt1 - t1 + cnt - 1) % (cnt - 1)) << "\\n";
    } else {
      cout << f((cnt2 + t2 + cnt + 1) % (cnt + 1)) << "\\n";
    }
  }
}