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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define int ll

ll f(ll l) {
  int cnt = 0;
  while (l != 0) {
    ll u = __builtin_popcount(l);
    l %= u;
    cnt++;
  }
  return cnt;
}

void solve(long long N, std::string X) {
  reverse(X.begin(), X.end());
  int cnt = count(X.begin(), X.end(), '1');
  if (cnt == 1) {
    int plus = cnt + 1;
    vector<int> vplus;
    int p = 1;
    int sum = 0;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        sum += p;
        sum %= plus;
      }
      vplus.push_back(p);
      p *= 2;
      p %= plus;
    }
    sum %= plus;
    vector<int> ans;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        ans.push_back(0);
      } else {
        ans.push_back(f((sum + vplus[i]) % plus) + 1);
      }
    }
    reverse(ans.begin(), ans.end());
    for (auto a : ans) {
      cout << a << endl;
    }
  } else {
    int plus = cnt + 1;
    int minus = cnt - 1;
    vector<int> vplus;
    vector<int> vminus;
    int p = 1;
    int m = 1;
    int sump = 0;
    int summ = 0;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        sump += p;
        summ += m;
        summ %= minus;
        sump %= plus;
      }
      vplus.push_back(p);
      vminus.push_back(m);
      p *= 2;
      m *= 2;
      p %= plus;
      m %= minus;
    }
    sump %= plus;
    summ %= minus;
    vector<int> ans;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        ans.push_back(f((summ - vminus[i] + minus) % minus) + 1);
      } else {
        ans.push_back(f((sump + vplus[i]) % plus) + 1);
      }
    }
    reverse(ans.begin(), ans.end());
    for (auto a : ans) {
      cout << a << endl;
    }
  }
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  solve(N, X);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define int ll

ll f(ll l) {
  int cnt = 0;
  while (l != 0) {
    ll u = __builtin_popcount(l);
    l %= u;
    cnt++;
  }
  return cnt;
}

void solve(long long N, std::string X) {
  reverse(X.begin(), X.end());
  int cnt = count(X.begin(), X.end(), '1');
  if (cnt == 1) {
    exit(1);
    int plus = cnt + 1;
    vector<int> vplus;
    int p = 1;
    int sum = 0;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        sum += p;
        sum %= plus;
      }
      vplus.push_back(p);
      p *= 2;
      p %= plus;
    }
    sum %= plus;
    vector<int> ans;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        ans.push_back(0);
      } else {
        ans.push_back(f((sum + vplus[i]) % plus) + 1);
      }
    }
  } else {
    int plus = cnt + 1;
    int minus = cnt - 1;
    vector<int> vplus;
    vector<int> vminus;
    int p = 1;
    int m = 1;
    int sump = 0;
    int summ = 0;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        sump += p;
        summ += m;
        summ %= minus;
        sump %= plus;
      }
      vplus.push_back(p);
      vminus.push_back(m);
      p *= 2;
      m *= 2;
      p %= plus;
      m %= minus;
    }
    sump %= plus;
    summ %= minus;
    vector<int> ans;
    for (int i = 0; i < N; i++) {
      if (X[i] == '1') {
        ans.push_back(f((summ - vminus[i] + minus) % minus) + 1);
      } else {
        ans.push_back(f((sump + vplus[i]) % plus) + 1);
      }
    }
    reverse(ans.begin(), ans.end());
    for (auto a : ans) {
      cout << a << endl;
    }
  }
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  solve(N, X);
  return 0;
}