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

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;

#define inf 1e+9
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void line(vector<T> &x) {
  for (auto &&xi : x)
    cin >> xi;
}

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LINT(...)                                                              \\
  lint __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
  sort(all(x));
  x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end) {
  for (auto i = first; i < end - 1; i++)
    cout << *i << " ";
  cout << *(end - 1) << endl;
}
template <class T> void print(T a) { cout << a << endl; };
vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

lint popcount(lint n) {
  int res = 0;
  for (int i = 0; i < 32; ++i) {
    if (n & (1 << i))
      res++;
  }
  return res;
};

vlint ans(300000, -1);

int main() {
  INT(n);
  ans[0] = 0;
  ans[1] = 1;
  rep1(i, n + 1) { ans[i + 1] = ans[(i + 1) % popcount(i + 1)] + 1; }
  STR(x);
  lint pc = 0;
  rep(i, n) {
    if (x[i] == '1')
      pc++;
  }
  lint m_a = pc - 1;
  lint m_b = pc + 1;
  if (m_a == 0)
    m_a = -2;
  vlint cnt_a(n + 3, 1), cnt_b(n + 3, 1);
  cnt_a[0] = 1 % m_a;
  cnt_b[0] = 1 % m_b;
  rep1(i, n + 1) {
    cnt_a[i] = cnt_a[i - 1] * 2 % m_a;
    cnt_b[i] = cnt_b[i - 1] * 2 % m_b;
  }
  lint x_a = 0;
  lint x_b = 0;
  rep(i, n) {
    if (x[i] == '1') {
      x_a += cnt_a[n - 1 - i] % m_a;
      x_a %= m_a;
      x_b += cnt_b[n - 1 - i] % m_b;
      x_b %= m_b;
    }
  }
  rep(i, n + 1) {
    cnt_a[i] = (x_a - cnt_a[i]) % m_a;
    cnt_b[i] = (x_b + cnt_b[i]) % m_b;
  }

  lint k;
  lint ret;
  rep(i, n) {
    k = n - 1 - i;
    if (x[i] == '1') {
      ret = cnt_a[k];
      if (ret < 0)
        ret += m_a;

      if (m_a == -2) {
        print(0);
        continue;
      };
    } else {
      ret = cnt_b[k];
      if (ret > m_b)
        ret %= m_b;
    }
    print(ans[ret] + 1);
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;

#define inf 1e+9
#define endl "\\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> void line(vector<T> &x) {
  for (auto &&xi : x)
    cin >> xi;
}

inline void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

#define INT(...)                                                               \\
  int __VA_ARGS__;                                                             \\
  IN(__VA_ARGS__)
#define LINT(...)                                                              \\
  lint __VA_ARGS__;                                                            \\
  IN(__VA_ARGS__)
#define STR(...)                                                               \\
  string __VA_ARGS__;                                                          \\
  IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
  sort(all(x));
  x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end) {
  for (auto i = first; i < end - 1; i++)
    cout << *i << " ";
  cout << *(end - 1) << endl;
}
template <class T> void print(T a) { cout << a << endl; };
vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

lint popcount(lint n) {
  int res = 0;
  for (int i = 0; i < 32; ++i) {
    if (n & (1 << i))
      res++;
  }
  return res;
};

vlint ans(300000, -1);

int main() {
  INT(n);
  ans[0] = 0;
  ans[1] = 1;
  rep1(i, n + 1) { ans[i + 1] = ans[(i + 1) % popcount(i + 1)] + 1; }
  STR(x);
  lint pc = 0;
  rep(i, n) {
    if (x[i] == '1')
      pc++;
  }
  lint m_a = pc - 1;
  lint m_b = pc + 1;
  vlint cnt_a(n + 1, 1), cnt_b(n + 1, 1);
  cnt_a[0] = 1 % m_a;
  cnt_b[0] = 1 % m_b;
  rep1(i, n + 1) {
    cnt_a[i] = cnt_a[i - 1] * 2 % m_a;
    cnt_b[i] = cnt_b[i - 1] * 2 % m_b;
  }
  lint x_a = 0;
  lint x_b = 0;
  rep(i, n) {
    if (x[i] == '1') {
      x_a += cnt_a[n - 1 - i] % m_a;
      x_a %= m_a;
      x_b += cnt_b[n - 1 - i] % m_b;
      x_b %= m_b;
    }
  }
  rep(i, n + 1) {
    cnt_a[i] = (x_a - cnt_a[i]) % m_a;
    cnt_b[i] = (x_b + cnt_b[i]) % m_b;
  }
  lint k;
  lint ret;
  rep(i, n) {
    k = n - 1 - i;
    if (x[i] == '1') {
      ret = cnt_a[k];
      if (ret < 0)
        ret += m_a;

    } else {
      ret = cnt_b[k];
      if (ret > m_b)
        ret %= m_b;
    }
    print(ans[ret] + 1);
  }
}