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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define INF 1000000007
#define pi 3.141592653589793
using namespace std;
int sdkys(int a, int b) {
  if (b == 0)
    return a;
  return sdkys(b, a % b);
}
int sskbs(int a, int b) { return a * b / sdkys(a, b); }
int n, m, cnt = 0, aa[220000], ans = 0, ab[220000], a = 0, b = 0, xx[220000];
pair<int, int> pp[220000];
string s, sa[220000];
char c;
queue<int> que;
signed main() {
  rep(i, 200000) {
    int l = i + 1;
    cnt = 0;
    rep(j, 20) {
      if (l % 2 == 1) {
        cnt++;
      }
      l /= 2;
    }
    xx[i + 1] = xx[(i + 1) % cnt] + 1;
  }
  cnt = 0;
  cin >> n >> s;
  rep(i, n) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  rep(i, n) {
    if (i == 0) {
      if (cnt == 0) {
        aa[i] = 0;
        ab[i] = 0;
      }
      elif (cnt == 1) {
        aa[i] = 1;
        ab[i] = 0;
      }
      else {
        aa[i] = 1;
        ab[i] = 1;
      }
    } else {
      aa[i] = aa[i - 1] * 2 % (cnt + 1);
      if (cnt - 1 > 0) {
        ab[i] = ab[i - 1] * 2 % (cnt - 1);
      } else {
        ab[i] = 0;
      }
    }
    a += aa[i] * (s[n - 1 - i] - '0');
    b += ab[i] * (s[n - 1 - i] - '0');
  }
  rep(i, n) {
    if (s[i] == '0') {
      cout << xx[(a + aa[n - 1 - i]) % (cnt + 1)] + 1 << endl;
    } else {
      if (cnt - 1 > 0) {
        cout << xx[(b - ab[n - 1 - i]) % (cnt - 1)] + 1 << endl;
      } else {
        if (cnt == 1) {
          cout << 0 << endl;
        } else {
          cout << 1 << endl;
        }
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define INF 1000000007
#define pi 3.141592653589793
using namespace std;
int sdkys(int a, int b) {
  if (b == 0)
    return a;
  return sdkys(b, a % b);
}
int sskbs(int a, int b) { return a * b / sdkys(a, b); }
int n, m, cnt = 0, aa[220000], ans = 0, ab[220000], a = 0, b = 0, xx[220000];
pair<int, int> pp[220000];
string s, sa[220000];
char c;
queue<int> que;
signed main() {
  rep(i, 200000) {
    int l = i + 1;
    cnt = 0;
    rep(j, 20) {
      if (l % 2 == 1) {
        cnt++;
      }
      l /= 2;
    }
    xx[i + 1] = xx[(i + 1) % cnt] + 1;
  }
  cnt = 0;
  cin >> n >> s;
  rep(i, n) {
    if (s[i] == '1') {
      cnt++;
    }
  }
  rep(i, n) {
    if (i == 0) {
      if (cnt == 0) {
        aa[i] = 0;
        ab[i] = 0;
      }
      elif (cnt == 1) {
        aa[i] = 1;
        ab[i] = 0;
      }
      else {
        aa[i] = 1;
        ab[i] = 1;
      }
    } else {
      aa[i] = aa[i - 1] * 2 % (cnt + 1);
      ab[i] = ab[i - 1] * 2 % (cnt - 1);
    }
    a += aa[i] * (s[n - 1 - i] - '0');
    b += ab[i] * (s[n - 1 - i] - '0');
  }
  rep(i, n) {
    if (s[i] == '0') {
      cout << xx[(a + aa[n - 1 - i]) % (cnt + 1)] + 1 << endl;
    } else {
      cout << xx[(b - ab[n - 1 - i]) % (cnt - 1)] + 1 << endl;
    }
  }
}