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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
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;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

int powMod(int n, int p, int mod) {
  int res = 1;
  while (p) {
    if (p & 1)
      (res *= n) %= mod;
    (n *= n) %= mod;
    p >>= 1;
  }
  return res;
}

signed main() {
  int N;
  cin >> N;

  string s;
  cin >> s;

  vector<int> one;
  REP(i, N) if (s[i] == '1') one.emplace_back(i);

  int a = 0, b = 0;
  REP(i, N) {
    a *= 2;
    b *= 2;
    if (s[i] == '1') {
      a++, b++;
    }
    a %= one.size() + 1;
    if ((int)one.size() - 1 >= 1) {
      b %= one.size() - 1;
    }
  }

  vector<int> dp(N + 1, 1);
  FOR(i, 1, N + 1) {
    int cnt = __builtin_popcountll(i);
    dp[i] = dp[i % cnt] + 1;
  }

  REP(i, N) {
    if (s[i] == '1') {
      if (one.size() == 1)
        cout << 0 << "\\n";
      else {
        int t =
            (b - powMod(2, N - 1 - i, one.size() - 1)) % ((int)one.size() - 1);
        if (t < 0)
          t += one.size() - 1;
        cout << dp[t] << "\\n";
      }
    } else {
      int t =
          (a + powMod(2, N - 1 - i, one.size() + 1)) % ((int)one.size() + 1);
      if (t < 0)
        t += one.size() + 1;
      cout << dp[t] << "\\n";
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
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;
}

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

signed main() {
  int N;
  cin >> N;

  string s;
  cin >> s;

  int cnt = 0;
  REP(i, N) {
    if (s[i] == '1')
      cnt++;
  }

  REP(i, N) {
    /* REP(i, 1){ */
    if (s[i] == '0') {
      s[i] = '1';
      cnt++;
    } else {
      s[i] = '0';
      cnt--;
    }

    int ans = 0;
    int popcount = cnt;
    string t = s;

    while (1) {
      vector<int> amari;
      map<int, int> used;

      int sum = 0;
      int two = 1;
      int first = 0;
      bool ok = false;
      int p = 0;
      int roop = 0;

      int M = t.size();
      RREP(i, M) {
        if (!ok) {
          int now = two % popcount;
          if (used.count(now)) {
            roop = used[now] - i;
            first = M - used[now] - 1;
            ok = true;
          } else {
            amari.emplace_back(now);
            used[now] = i;
          }
        }

        if (ok && p >= first + roop)
          p = first;
        if (t[i] == '1') {
          sum += amari[p];
        }
        p++;

        two *= 2;
      }
      sum %= popcount;

      ans++;
      if (sum == 0)
        break;
      else {
        t = "";
        popcount = 0;
        while (sum > 0) {
          if (sum % 2)
            popcount++;
          t += to_string(sum % 2);
          sum /= 2;
        }
        reverse(ALL(t));
      }
    }
    cout << ans << "\\n";

    if (s[i] == '0') {
      s[i] = '1';
      cnt++;
    } else {
      s[i] = '0';
      cnt--;
    }
  }

  return 0;
}