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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int mod(int n, const std::string &x, const std::vector<int> &rems,
        int denominator) {
  int result = 0;
  for (int i = 0; i < n; ++i) {
    if (x.at(i) == '1') {
      result = (result + rems.at(i)) % denominator;
    }
  }
  return result;
}

int popcount(int n) {
  int result = 0;
  while (n > 0) {
    if (n & 1)
      ++result;
    n >>= 1;
  }
  return result;
}

int f(int value) {
  for (int result = 1;; ++result) {
    if (value <= 0)
      return result;
    value %= popcount(value);
  }
}

std::vector<int> solve(int n, const std::string &x) {
  const int p0 = std::count(x.begin(), x.end(), '1');
  if (p0 == 0)
    return std::vector<int>(n, 1);

  std::vector<int> rems0(n);
  std::vector<int> rems1(n);
  int r0 = 1;
  int r1 = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (p0 >= 2) {
      rems0.at(i) = r0;
      r0 = (r0 << 1) % (p0 - 1);
    }
    rems1.at(i) = r1;
    r1 = (r1 << 1) % (p0 + 1);
  }

  const auto mod0 = p0 == 1 ? 1 : mod(n, x, rems0, p0 - 1);
  const auto mod1 = mod(n, x, rems1, p0 + 1);

  std::vector<int> result(n);
  for (int i = 0; i < n; ++i) {
    if (x.at(i) == '1') {
      if (p0 == 1) {
        result.at(i) = 0;
      } else {
        auto v = mod0 - rems0.at(i);
        if (v < 0)
          v += p0 - 1;
        result.at(i) = f(v % (p0 - 1));
      }
    } else {
      result.at(i) = f((mod1 + rems1.at(i)) % (p0 + 1));
    }
  }
  return result;
}

int main() {
  int n;
  std::string x;
  std::cin >> n >> x;

  std::cout << std::setprecision(20);
  const auto values = solve(n, x);
  for (auto v : values) {
    std::cout << v << std::endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int mod(int n, const std::string &x, const std::vector<int> &rems,
        int denominator) {
  int result = 0;
  for (int i = 0; i < n; ++i) {
    if (x.at(i) == '1') {
      result = (result + rems.at(i)) % denominator;
    }
  }
  return result;
}

int popcount(int n) {
  int result = 0;
  while (n > 0) {
    if (n & 1)
      ++result;
    n >>= 1;
  }
  return result;
}

int f(int value) {
  for (int result = 1;; ++result) {
    if (value == 0)
      return result;
    value %= popcount(value);
  }
}

// int f(const std::string& x, const std::vector<int>& rems, int denominator)
//{
//   const int n = static_cast<int>(x.size());
//   int r = 0;
//   for (int i = 0; i < n; ++i) {
//     if (x.at(i) == '1') {
//       r = (r + rems.at(i)) % denominator;
//     }
//   }
//   for (int result = 1; ; ++result) {
//     if (r == 0) return result;
//     r %= popcount(r);
//   }
// }

std::vector<int> solve(int n, const std::string &x) {
  const int p0 = std::count(x.begin(), x.end(), '1');
  std::vector<int> rems0(n);
  std::vector<int> rems1(n);
  int r0 = 1;
  int r1 = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (p0 >= 2)
      rems0.at(i) = r0 % (p0 - 1);
    rems1.at(i) = r1 % (p0 + 1);
    r0 <<= 1;
    r1 <<= 1;
  }

  const auto mod0 = p0 == 1 ? 1 : mod(n, x, rems0, p0 - 1);
  const auto mod1 = mod(n, x, rems1, p0 + 1);

  std::vector<int> result(n);
  for (int i = 0; i < n; ++i) {
    if (x.at(i) == '1') {
      if (p0 == 1) {
        result.at(i) = 0;
      } else {
        auto v = mod0 - rems0.at(i);
        if (v < 0)
          v += p0 - 1;
        result.at(i) = f(v % (p0 - 1));
      }
    } else {
      result.at(i) = f((mod1 + rems1.at(i)) % (p0 + 1));
    }
  }
  return result;
}

int main() {
  int n;
  std::string x;
  std::cin >> n >> x;

  std::cout << std::setprecision(20);
  const auto values = solve(n, x);
  for (auto v : values) {
    std::cout << v << std::endl;
  }
}