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

#include <cmath>
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ctoi(c) c - '0'
#define tos(s) to_string(s)

typedef long long ll;

int main() {
  ll n;
  string s;
  cin >> n;
  cin >> s;
  ll a = 0;
  ll p = 0;
  ll count = 0;
  ll pm = 0, pp = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      p++; // all pop count
  }

  ll tmp = 0;
  ll lastm = 1;
  ll lastp = 1;
  ll im[n], ip[n];

  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      tmp = 1;
    } else {
      tmp = 0;
    }

    ip[i] = lastp % (p + 1);
    pp += ((tmp * lastp) % (p + 1));
    lastp = (lastp * 2) % (p + 1);

    if (p > 1) {
      im[i] = lastm % (p - 1);
      pm += ((tmp * lastm) % (p - 1));
      lastm = (lastm * 2) % (p - 1);
    }
  }

  for (ll i = 0; i < n; i++) {
    count = 0;
    if (s[i] == '1') {
      if (p == 1) {
        a = 0;
      } else {
        a = (pm - im[i] + p - 1) % (p - 1);
        count++;
      }
    } else {
      a = (pp + ip[i] + p + 1) % (p + 1);
      count++;
    }

    while (a > 0) {
      a = a % __builtin_popcountll(a);
      count++;
    }
    cout << count << endl;
  }

  return 0;
}

/*
ACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACAC
                                   ******,  .,, .,, ,,,    ****.**
                                   ** . .,,,,,,,,,,,,,,,,,,,,,. .,*
                                   **,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.
                                .,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.
                                .,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.
                    ****      ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
              ***************,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
         ********************,,,,,,,,,,,,,,@@@,,,,,,,,,,,/@@@,,,,,,,,,,,,,,,
        ******************,,,,,,,,,,,,,,,,,,(,,.         ,,,,,,,,,,,,,,,,,,,
       ********************,,,,,,,,,,,*,,,,,.     %&&&     .,,,,*,,,,,,,,,,,,.
       ***,,,,,,,,,,,,,,*,,,,,,,,,,,,,,,,,,,,               ,,,,,,,,,,,,,,,,,
       ,,,,,,,,,,,,,,,,,,,**,,,,,,,,,,.......      ##(     ........,,,,,,,,,,.
     .,,,,,,,,,,,,,,,,,,**,,,,,,,,,,..............    ..............,,,,,,,,
    .,,,,,,,,,,,,,,,,,,,**,,,,,,,,,.................................,,,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,***,,,,,,,....................................,,,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,**,,,,,,,,.....................................,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,,**,,,,,,,,....................................,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.....................................,,,,,
   ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...................................,.,,,,,
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,.....................................,,,
     ,,,,,,,,,,,,,,,,,,,,,,,,,,,,....................................,,,
      ,,,,,,,,,,,,,,,,,,,,,,,,,,,,...................................,
       ..,,,,,,,,,,,,,,,,,,,,,,,,,,.................................,
       .........**.....       ,,,,,..............................
        .......*......                  ****,**,       ,..****
        .......*,....                   .......        ......
          .......*....                   .....          .....
                                         ......          .....
*/

Error: 0

Fixed submission:

#include <cmath>
#include <deque>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ctoi(c) c - '0'
#define tos(s) to_string(s)

typedef long long ll;

ll strbin2i(const std::string &s) {

  ll out = 0;

  for (ll i = 0, size = s.size(); i < size; ++i) {

    out *= 2;

    out += ((ll)s[i] == 49) ? 1 : 0;
  }

  return out;
}

int main() {
  vector<ll> nmap(2 * 10 * 10 * 10 * 10 * 10 + 2, 0);
  ll n;
  string s;
  cin >> n;
  cin >> s;
  ll a = 0;
  string ss;
  ll count = 0;
  for (ll i = 0; i < 2 * 10 * 10 * 10 * 10 * 10 + 2; i++) {
    count = 0;
    a = i;
    while (a > 0) {
      a = a % __builtin_popcount(a);
      count++;
      if (nmap[a] != 0) {
        count += nmap[a];
        break;
      }
    }
    nmap[i] = count;
  }

  for (ll i = 0; i < n; i++) {
    ss = s;
    if (ss[i] == '0') {
      ss.replace(i, 1, "1");
    } else {
      ss.replace(i, 1, "0");
    }
    a = strbin2i(ss);

    a = a % __builtin_popcount(a);
    cout << nmap[a] + 1 << endl;
  }

  return 0;
}

/*
ACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACACAC
                                   ******,  .,, .,, ,,,    ****.**
                                   ** . .,,,,,,,,,,,,,,,,,,,,,. .,*
                                   **,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.
                                .,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.
                                .,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.
                    ****      ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
              ***************,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
         ********************,,,,,,,,,,,,,,@@@,,,,,,,,,,,/@@@,,,,,,,,,,,,,,,
        ******************,,,,,,,,,,,,,,,,,,(,,.         ,,,,,,,,,,,,,,,,,,,
       ********************,,,,,,,,,,,*,,,,,.     %&&&     .,,,,*,,,,,,,,,,,,.
       ***,,,,,,,,,,,,,,*,,,,,,,,,,,,,,,,,,,,               ,,,,,,,,,,,,,,,,,
       ,,,,,,,,,,,,,,,,,,,**,,,,,,,,,,.......      ##(     ........,,,,,,,,,,.
     .,,,,,,,,,,,,,,,,,,**,,,,,,,,,,..............    ..............,,,,,,,,
    .,,,,,,,,,,,,,,,,,,,**,,,,,,,,,.................................,,,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,***,,,,,,,....................................,,,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,**,,,,,,,,.....................................,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,,**,,,,,,,,....................................,,,,,,,
   ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.....................................,,,,,
   ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,...................................,.,,,,,
    ,,,,,,,,,,,,,,,,,,,,,,,,,,,,.....................................,,,
     ,,,,,,,,,,,,,,,,,,,,,,,,,,,,....................................,,,
      ,,,,,,,,,,,,,,,,,,,,,,,,,,,,...................................,
       ..,,,,,,,,,,,,,,,,,,,,,,,,,,.................................,
       .........**.....       ,,,,,..............................
        .......*......                  ****,**,       ,..****
        .......*,....                   .......        ......
          .......*....                   .....          .....
                                         ......          .....
*/