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

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>     //abs,,,
#include <cstdint>   // int64_t, int*_t
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <limits>    //setprecision
#include <map>       // map
#include <math.h>    //pow,,,
#include <numeric>   //accumulate,,,
#include <queue>     // queue, priority_queue
#include <set>       // set,multiset
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector
#define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
// int MOD = 1000000007;
typedef pair<long long, long long> P;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M, long long MOD) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2, MOD);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long N;
  cin >> N;
  string X;
  cin >> X;
  reverse(X.begin(), X.end());
  long long MOD = 0;

  long long standard_plus = 0, standard_minus = 0;
  long long cnt = 0;
  for (long long i = 0; i < N; i++)
    if (X.at(i) == '1')
      cnt++;

  if (cnt - 1 != 0) {
    for (long long i = 0; i < N; i++) {
      cnt--;
      MOD = cnt;
      if (X.at(i) == '1')
        standard_minus += mod_pow(2, i, MOD);
      standard_minus %= MOD;
      cnt++;
    }
  }

  for (long long i = 0; i < N; i++) {
    cnt++;
    MOD = cnt;
    if (X.at(i) == '1')
      standard_plus += mod_pow(2, i, MOD);
    standard_plus %= MOD;
    cnt--;
  }

  vector<int> OUTPUT;
  for (long long i = 0; i < N; i++) {
    long long cnt1 = cnt;
    long long now;
    long long ans = 1;
    if (X.at(i) == '1') {
      now = standard_minus;
      cnt1--;
      if (cnt1 == 0) {
        OUTPUT.push_back(0);
        continue;
      }
      MOD = cnt1;
      now -= mod_pow(2, i, MOD);
      now %= MOD;
      if (now % cnt1 == 0) {
        OUTPUT.push_back(ans);
        continue;
      }
    }
    if (X.at(i) == '0') {
      now = standard_plus;
      cnt1++;
      MOD = cnt1;
      now += mod_pow(2, i, MOD);
      now %= MOD;
      if (now % cnt1 == 0) {
        OUTPUT.push_back(ans);
        continue;
      }
    }
    while (now % cnt1 != 0) {
      now %= cnt1;
      if (now < 0)
        now += cnt1;
      ans++;
      bitset<20> BIT(now);
      cnt1 = BIT.count();
    }
    OUTPUT.push_back(ans);
  }
  reverse(OUTPUT.begin(), OUTPUT.end());
  for (int V : OUTPUT)
    cout << V << endl;
}


Error: 0

Fixed submission:

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>     //abs,,,
#include <cstdint>   // int64_t, int*_t
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <limits>    //setprecision
#include <map>       // map
#include <math.h>    //pow,,,
#include <numeric>   //accumulate,,,
#include <queue>     // queue, priority_queue
#include <set>       // set,multiset
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <utility>   // pair, make_pair
#include <vector>    // vector
#define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
// int MOD = 1000000007;
typedef pair<long long, long long> P;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M, long long MOD) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2, MOD);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long N;
  cin >> N;
  string X;
  cin >> X;
  reverse(X.begin(), X.end());
  long long MOD = 0;

  long long standard_plus = 0, standard_minus = 0;
  long long cnt = 0;
  for (long long i = 0; i < N; i++)
    if (X.at(i) == '1')
      cnt++;

  for (long long i = 0; i < N; i++) {
    cnt--;
    MOD = cnt;
    if (X.at(i) == '1')
      standard_minus += mod_pow(2, i, MOD);
    standard_minus %= MOD;
    cnt++;
  }
  for (long long i = 0; i < N; i++) {
    cnt++;
    MOD = cnt;
    if (X.at(i) == '1')
      standard_plus += mod_pow(2, i, MOD);
    standard_plus %= MOD;
    cnt--;
  }

  vector<int> OUTPUT;
  for (long long i = 0; i < N; i++) {
    long long cnt1 = cnt;
    long long now;
    long long ans = 1;
    if (X.at(i) == '1') {
      now = standard_minus;
      cnt1--;
      MOD = cnt1;
      now -= mod_pow(2, i, MOD);
      now %= MOD;
      if (now % cnt1 == 0) {
        OUTPUT.push_back(ans);
        continue;
      }
    }
    if (X.at(i) == '0') {
      now = standard_plus;
      cnt1++;
      MOD = cnt1;
      now += mod_pow(2, i, MOD);
      now %= MOD;
      if (now % cnt1 == 0) {
        OUTPUT.push_back(ans);
        continue;
      }
    }
    while (now % cnt1 != 0) {
      now %= cnt1;
      if (now < 0)
        now += cnt1;
      ans++;
      bitset<20> BIT(now);
      cnt1 = BIT.count();
    }
    OUTPUT.push_back(ans);
  }
  reverse(OUTPUT.begin(), OUTPUT.end());
  for (int V : OUTPUT)
    cout << V << endl;
}