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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5;
int n, tot;
bool spec = false;
void solve(int &x) {
  int cnt = 0, pw = 1;
  string s = "";
  while (x) {
    if (x % 2 == 1)
      s += "1", cnt++;
    else
      s += "0";
    x /= 2;
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      x = (x + pw) % cnt;
    pw = pw * 2 % cnt;
  }
  tot += s.size() * 2;
} // no problem
int cal(int num) {
  int res = 0;
  while (num) {
    solve(num);
    res++;
  }
  return res;
}
string s;
vector<int> ANS;
int main() {
  cin >> n >> s;
  int cnt = 0, cnt1, cnt2, sum1 = 0, sum2 = 0, pw1 = 1, pw2 = 1;
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1')
      cnt++;
  cnt1 = cnt - 1;
  if (cnt1 == 0)
    spec = true, cnt1 = 1;
  cnt2 = cnt + 1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      sum1 = (sum1 + pw1) % cnt1;
      sum2 = (sum2 + pw2) % cnt2;
    }
    pw1 = pw1 * 2 % cnt1;
    pw2 = pw2 * 2 % cnt2;
  }
  pw1 = 1;
  pw2 = 1;
  //	reverse(s.begin(),s.end());
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      if (spec)
        ANS.push_back(0);
      else {
        int ans = 1;
        ans += cal(((sum1 - pw1) % cnt1 + cnt1) % cnt1);
        ANS.push_back(ans);
      }
    } else {
      int ans = 1;
      ans += cal((sum2 + pw2) % cnt2);
      ANS.push_back(ans);
    }
    pw1 = pw1 * 2 % cnt1;
    pw2 = pw2 * 2 % cnt2;
  }
  reverse(ANS.begin(), ANS.end());
  for (int i = 0; i < ANS.size(); i++)
    cout << ANS[i] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 5;
int n, tot;
void solve(int &x) {
  int cnt = 0, pw = 1;
  string s = "";
  while (x) {
    if (x % 2 == 1)
      s += "1", cnt++;
    else
      s += "0";
    x /= 2;
  }
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      x = (x + pw) % cnt;
    pw = pw * 2 % cnt;
  }
  tot += s.size() * 2;
} // no problem
int cal(int num) {
  int res = 0;
  while (num) {
    solve(num);
    res++;
  }
  return res;
}
string s;
vector<int> ANS;
int main() {
  cin >> n >> s;
  int cnt = 0, cnt1, cnt2, sum1 = 0, sum2 = 0, pw1 = 1, pw2 = 1;
  reverse(s.begin(), s.end());
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1')
      cnt++;
  cnt1 = cnt - 1;
  cnt2 = cnt + 1;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      sum1 = (sum1 + pw1) % cnt1;
      sum2 = (sum2 + pw2) % cnt2;
    }
    pw1 = pw1 * 2 % cnt1;
    pw2 = pw2 * 2 % cnt2;
  }
  pw1 = 1;
  pw2 = 1;
  //	reverse(s.begin(),s.end());
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1') {
      if (cnt1 == 0)
        ANS.push_back(0);
      else {
        int ans = 1;
        ans += cal(((sum1 - pw1) % cnt1 + cnt1) % cnt1);
        ANS.push_back(ans);
      }
    } else {
      int ans = 1;
      ans += cal((sum2 + pw2) % cnt2);
      ANS.push_back(ans);
    }
    pw1 = pw1 * 2 % cnt1;
    pw2 = pw2 * 2 % cnt2;
  }
  reverse(ANS.begin(), ANS.end());
  for (int i = 0; i < ANS.size(); i++)
    cout << ANS[i] << "\\n";
  return 0;
}