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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;
string X;

int m0_list[200001];
int m1_list[200001];

int cnt_list[200001];

int mod(string s, int n) {
  int cur = 0;
  for (int i = 0; i < s.length(); i++) {
    cur = cur << 1;
    if (s[i] == '1') {
      cur += 1;
    }
    cur %= n;
  }
  return cur;
}

void make(int list[200001], int n) {
  int cur = 1;
  for (int i = 1; i <= 200000; i++) {
    cur = cur % n;
    list[i] = cur;
    cur <<= 1;
  }
}

int get_cnt(int n) {
  int cur = n;
  int cnt = 0;
  while (cur > 0) {
    if ((cur & 1LL) != 0) {
      cnt++;
    }
    cur >>= 1;
  }
  if (cnt == 0)
    return 0;
  int a = n % cnt;
  return 1 + get_cnt(a);
}

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

  int cnt = 0;
  for (int i = 0; i < X.length(); i++) {
    if (X[i] == '1')
      cnt++;
  }

  for (int i = 1; i <= 200000; i++) {
    cnt_list[i] = get_cnt(i);
  }

  make(m0_list, cnt + 1);
  if (cnt - 1 > 0) {
    make(m1_list, cnt - 1);
  }

  int m0 = mod(X, cnt + 1);
  int m1 = cnt == 1 ? 0 : mod(X, cnt - 1);

  for (int i = 0; i < X.length(); i++) {
    int keta = (X.length() - i);
    if (X[i] == '0') {
      int m = (m0 + m0_list[keta]) % (cnt + 1);
      int a = cnt_list[m];
      cout << (a + 1) << endl;
    } else {
      if (cnt - 1 == 0) {
        cout << 0 << endl;
      } else {
        int m = (m1 - m1_list[keta]) % (cnt - 1);
        if (m < 0)
          m += (cnt - 1);
        int a = cnt_list[m];
        cout << (a + 1) << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <array>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define int long long
#define CONTAINS_VEC(v, n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a) + (size))
#define REMOVE(v, a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v, a) (reverse((v), (v) + (a)))
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
#define CONTAINS_SET(m, a) (m).find((a)) != m.end()
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

int N;
string X;

int m0_list[200001];
int m1_list[200001];

int cnt_list[200001];

int mod(string s, int n) {
  int cur = 0;
  for (int i = 0; i < s.length(); i++) {
    cur = cur << 1;
    if (s[i] == '1') {
      cur += 1;
    }
    cur %= n;
  }
  return cur;
}

void make(int list[200001], int n) {
  int cur = 1;
  for (int i = 1; i <= 200000; i++) {
    cur = cur % n;
    list[i] = cur;
    cur <<= 1;
  }
}

int get_cnt(int n) {
  int cur = n;
  int cnt = 0;
  while (cur > 0) {
    if ((cur & 1LL) != 0) {
      cnt++;
    }
    cur >>= 1;
  }
  if (cnt == 0)
    return 0;
  int a = n % cnt;
  return 1 + get_cnt(a);
}

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

  int cnt = 0;
  for (int i = 0; i < X.length(); i++) {
    if (X[i] == '1')
      cnt++;
  }

  for (int i = 1; i <= 200000; i++) {
    cnt_list[i] = get_cnt(i);
  }

  make(m0_list, cnt + 1);
  make(m1_list, cnt - 1);

  int m0 = mod(X, cnt + 1);
  int m1 = mod(X, cnt - 1);

  for (int i = 0; i < X.length(); i++) {
    int keta = (X.length() - i);
    if (X[i] == '0') {
      int m = (m0 + m0_list[keta]) % (cnt + 1);
      int a = cnt_list[m];
      cout << (a + 1) << endl;
    } else {
      int m = (m1 - m1_list[keta]) % (cnt - 1);
      if (m < 0)
        m += (cnt - 1);
      int a = cnt_list[m];
      cout << (a + 1) << endl;
    }
  }
}