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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ALL(X) X.begin(), X.end()
using lint = long long;
template <typename T> void print_container(std::vector<T> vec) {
  for (auto iter = vec.begin(); iter != vec.end(); ++iter) {
    std::cout << *iter << " ";
  }
  std::cout << std::endl;
}

int main() {
  lint n;
  scanf("%lld", &n);

  std::string s;
  std::cin >> s;

  lint s_pc = 0;

  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      s_pc++;
    }
  }

  lint p1 = s_pc + 1;
  lint m1 = s_pc - 1;

  std::vector<lint> p1_rem_vec(n, 0);
  std::vector<lint> m1_rem_vec(n, 0);

  lint p1_rem = 1 % p1;
  p1_rem_vec[n - 1] = p1_rem;
  for (int i = n - 2; i >= 0; --i) {
    p1_rem = (p1_rem * 2) % p1;
    p1_rem_vec[i] = p1_rem;
  }

  if (m1 != 0) {
    lint m1_rem = 1 % m1;
    m1_rem_vec[n - 1] = m1_rem;
    for (int i = n - 2; i >= 0; --i) {
      m1_rem = (m1_rem * 2) % m1;
      m1_rem_vec[i] = m1_rem;
    }
  }

  std::vector<lint> pop_rem(n + 2, 0);
  for (int i = 1; i <= n + 1; ++i) {
    lint pop = 0;
    lint tmp = i;
    while (tmp > 0) {
      if (tmp % 2 == 1) {
        pop++;
      }
      tmp >>= 1;
    }
    lint out = i % pop;
    pop_rem[i] = pop_rem[out] + 1;
  }

  lint base_p1 = 0;
  lint base_m1 = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      base_p1 += p1_rem_vec[i];
      base_m1 += m1_rem_vec[i];
    }
  }
  base_p1 %= p1;
  if (m1 != 0) {
    base_m1 %= m1;
  }

  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      lint cur_p1_rem = base_p1 + p1_rem_vec[i];
      cur_p1_rem %= p1;
      std::cout << pop_rem[cur_p1_rem] + 1 << "\\n";
    } else {
      lint cur_m1_rem = base_m1 - m1_rem_vec[i];
      if (cur_m1_rem < 0) {
        cur_m1_rem += m1;
      }
      if (m1 == 0) {
        std::cout << 0 << "\\n";
      } else {
        std::cout << pop_rem[cur_m1_rem] + 1 << "\\n";
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ALL(X) X.begin(), X.end()
using lint = long long;
template <typename T> void print_container(std::vector<T> vec) {
  for (auto iter = vec.begin(); iter != vec.end(); ++iter) {
    std::cout << *iter << " ";
  }
  std::cout << std::endl;
}

int main() {
  lint n;
  scanf("%lld", &n);

  std::string s;
  std::cin >> s;

  lint len = s.size();
  lint s_pc = 0;

  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      s_pc++;
    }
  }

  lint p1 = s_pc + 1;
  lint m1 = s_pc - 1;

  std::vector<lint> p1_rem_vec(n, 0);
  std::vector<lint> m1_rem_vec(n, 0);

  lint p1_rem = 1 % p1;
  p1_rem_vec[n - 1] = p1_rem;
  for (int i = n - 2; i >= 0; --i) {
    p1_rem = (p1_rem * 2) % p1;
    p1_rem_vec[i] = p1_rem;
  }

  lint m1_rem = 1 % p1;
  m1_rem_vec[n - 1] = m1_rem;
  for (int i = n - 2; i >= 0; --i) {
    m1_rem = (m1_rem * 2) % m1;
    m1_rem_vec[i] = m1_rem;
  }

  std::vector<lint> pop_rem(n + 2, 0);
  for (int i = 1; i <= n + 1; ++i) {
    lint pop = 0;
    lint tmp = i;
    while (tmp > 0) {
      if (tmp % 2 == 1) {
        pop++;
      }
      tmp >>= 1;
    }
    lint out = i % pop;
    pop_rem[i] = pop_rem[out] + 1;
  }

  lint base_p1 = 0;
  lint base_m1 = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      base_p1 += p1_rem_vec[i];
      base_m1 += m1_rem_vec[i];
    }
  }
  base_p1 %= p1;
  base_m1 %= m1;

  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      lint cur_p1_rem = base_p1 + p1_rem_vec[i];
      cur_p1_rem %= p1;
      std::cout << pop_rem[cur_p1_rem] + 1 << "\\n";
    } else {
      lint cur_m1_rem = base_m1 - m1_rem_vec[i];
      if (cur_m1_rem < 0) {
        cur_m1_rem += m1;
      }
      std::cout << pop_rem[cur_m1_rem] + 1 << "\\n";
    }
  }

  return 0;
}