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

/******tech_knight18_@11******/
/*I'm_not_the_only_traveller*/
/*who_has_not_repaid_his_debt*/
/*Take_me_back_to_The_Night_We_Met*/
#include <bits/stdc++.h>
using namespace std;
#define lld long long int
#define ld long double
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define F first
#define S second
#define nl '\\n'
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(c) (c).size()
#define tr(x, a) for (auto &a : x)
#define psnt(x, a) (x).find(a) != (x).end()
#define vpsnt(x, a) find(all(x), a) != (x).end()
#define lexichar(x, y)                                                         \\
  lexicographical_compare(x, x + strlen(x), y,                                 \\
                          y + strlen(y)) // modify the ranges
#define lexistr(x, y)                                                          \\
  lexicographical_compare(x.begin(), x.end(), y.begin(), y.end())
#define MOD 1000000007
#define pi 3.1415926536
#define tod 1
#define itr(i, a, b) for (lld i = a; i <= b; i++)
#define itrn(i, a, b) for (lld i = a; i >= b; i--)
#define iot(n) for (lld i = 0; i < n; i++)
#define pls(n, arr)                                                            \\
  lld arr[n];                                                                  \\
  iot(n) cin >> arr[i];
#define bye fflush(stdout)

typedef pair<lld, lld> pii;
typedef pair<string, lld> psi;
template <typename T> bool mycomp(T x, T y) {
  return (x == y); // give your condition here
}
bool paircomp(const pair<lld, lld> &x, const pair<lld, lld> &y) {
  return x.second < y.second;
}
lld BitsSetTable256[256];
void initialize() {

  BitsSetTable256[0] = 0;
  for (lld i = 0; i < 256; i++) {
    BitsSetTable256[i] = (i & 1) + BitsSetTable256[i / 2];
  }
}

lld countSetBits(lld n) {
  return (BitsSetTable256[n & 0xff] + BitsSetTable256[(n >> 8) & 0xff] +
          BitsSetTable256[(n >> 16) & 0xff] + BitsSetTable256[n >> 24]);
}
lld modexponent(lld x, lld y, lld M) // returns x^y %M
{
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return modexponent((x * x) % M, y / 2, M);
  else
    return (x * modexponent((x * x) % M, (y - 1) / 2, M)) % M;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lld t = 1; // cin>>t;
  while (t--) {
    initialize();
    lld hash[200005];
    iot(200005) hash[i] = 1;
    hash[0] = 0;
    itr(i, 2, 200004) {
      lld k = countSetBits(i);
      hash[i] += hash[i % k];
    }
    lld n;
    cin >> n;
    string s;
    cin >> s;
    lld k = 0;
    iot(n) if (s[i] == '1') k++;

    lld k1 = k + 1;
    lld re1 = 0;
    itrn(i, n - 1, 0) {
      if (s[i] == '1')
        re1 = (re1 + modexponent(2, n - i - 1, k1)) % k1;
    }
    lld k2 = k - 1;
    if (k2 == 0) {
      itr(i, 0, n - 1) {
        lld ff;
        if (s[i] == '0') {
          ff = re1;
          ff = (ff + (modexponent(2, n - i - 1, k1))) % k1;
          cout << 1 + hash[ff] << nl;
        } else {
          cout << 0 << nl;
        }
      }
      return 0;

    } else if (k2 < 0) {
      itr(i, 0, n - 1) {
        lld ff;
        if (s[i] == '0') {
          ff = re1;
          ff = (ff + (modexponent(2, n - i - 1, k1))) % k1;
          cout << 1 + hash[ff] << nl;
        }
      }
      return 0;
    }
    lld re2 = 0;
    itrn(i, n - 1, 0) {
      if (s[i] == '1')
        re2 = (re2 + modexponent(2, n - i - 1, k2)) % k2;
    }

    itr(i, 0, n - 1) {
      lld ff;
      if (s[i] == '0') {
        ff = re1;
        ff = (ff + (modexponent(2, n - i - 1, k1))) % k1;
      } else {
        ff = re2;
        ff = (ff - (modexponent(2, n - i - 1, k2)) + k2) % k2;
      }

      cout << 1 + hash[ff] << nl;
    }
  }
}


Error: 0

Fixed submission:

/******tech_knight18_@11******/
/*I'm_not_the_only_traveller*/
/*who_has_not_repaid_his_debt*/
/*Take_me_back_to_The_Night_We_Met*/
#include <bits/stdc++.h>
using namespace std;
#define lld long long int
#define ld long double
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define F first
#define S second
#define nl '\\n'
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define sz(c) (c).size()
#define tr(x, a) for (auto &a : x)
#define psnt(x, a) (x).find(a) != (x).end()
#define vpsnt(x, a) find(all(x), a) != (x).end()
#define lexichar(x, y)                                                         \\
  lexicographical_compare(x, x + strlen(x), y,                                 \\
                          y + strlen(y)) // modify the ranges
#define lexistr(x, y)                                                          \\
  lexicographical_compare(x.begin(), x.end(), y.begin(), y.end())
#define MOD 1000000007
#define pi 3.1415926536
#define tod 1
#define itr(i, a, b) for (lld i = a; i <= b; i++)
#define itrn(i, a, b) for (lld i = a; i >= b; i--)
#define iot(n) for (lld i = 0; i < n; i++)
#define pls(n, arr)                                                            \\
  lld arr[n];                                                                  \\
  iot(n) cin >> arr[i];
#define bye fflush(stdout)

typedef pair<lld, lld> pii;
typedef pair<string, lld> psi;
template <typename T> bool mycomp(T x, T y) {
  return (x == y); // give your condition here
}
bool paircomp(const pair<lld, lld> &x, const pair<lld, lld> &y) {
  return x.second < y.second;
}
lld BitsSetTable256[256];
void initialize() {

  BitsSetTable256[0] = 0;
  for (lld i = 0; i < 256; i++) {
    BitsSetTable256[i] = (i & 1) + BitsSetTable256[i / 2];
  }
}

lld countSetBits(lld n) {
  return (BitsSetTable256[n & 0xff] + BitsSetTable256[(n >> 8) & 0xff] +
          BitsSetTable256[(n >> 16) & 0xff] + BitsSetTable256[n >> 24]);
}
lld modexponent(lld x, lld y, lld M) // returns x^y %M
{
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return modexponent((x * x) % M, y / 2, M);
  else
    return (x * modexponent((x * x) % M, (y - 1) / 2, M)) % M;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lld t = 1; // cin>>t;
  while (t--) {
    initialize();
    lld hash[200005];
    iot(200005) hash[i] = 1;
    hash[0] = 0;
    itr(i, 2, 200004) {
      lld k = countSetBits(i);
      hash[i] += hash[i % k];
    }
    lld n;
    cin >> n;
    string s;
    cin >> s;
    lld k = 0;
    iot(n) if (s[i] == '1') k++;

    lld k1 = k + 1;
    lld re1 = 0;
    itrn(i, n - 1, 0) {
      if (s[i] == '1')
        re1 = (re1 + modexponent(2, n - i - 1, k1)) % k1;
    }
    lld k2 = k - 1;
    lld re2 = 0;
    itrn(i, n - 1, 0) {
      if (s[i] == '1')
        re2 = (re2 + modexponent(2, n - i - 1, k2)) % k2;
    }

    itr(i, 0, n - 1) {
      lld ff;
      if (s[i] == '0') {
        ff = re1;
        ff = (ff + (modexponent(2, n - i - 1, k1))) % k1;
      } else {
        ff = re2;
        ff = (ff - (modexponent(2, n - i - 1, k2)) + k2) % k2;
      }
      // cout<<ff<<" ";
      if (ff > 200004) {
        lld cc = 1;
        while (cc++)
          cout << 1 << nl;
      } else
        cout << 1 + hash[ff] << nl;
    }
  }
}