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

#include <bits/stdc++.h>
using namespace std;
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

const int INF = 1000000000;
const int MOD = 1000000007;

ll powMod(ll n, ll p, ll m) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n % m;
  ll e = powMod(n, p / 2, m);
  e = (e * e) % m;
  if (p & 1)
    e = (e * n) % m;
  return e;
}

ll fn(ll i, vll &ns) {
  if (ns[i] != -1)
    return ns[i];
  return 1 + fn(i % __builtin_popcount(i), ns);
}

int main() {
  FIN

      ll n,
      m = 0, i, p, d;
  string s;
  cin >> n;
  vll ns(n + 1, -1);
  vvll md(n + 1, vll(2));
  cin >> s;
  for (i = 0; i < n; i++)
    if (s[i] == '1')
      m++;

  ns[0] = 0;
  for (i = 1; i <= n; i++) {
    ns[i] = fn(i, ns);
  }

  if (m - 1 > 0) {
    p = 1;
    for (i = n - 1; i >= 0; i--) {
      if (s[i] == '1')
        md[i][0] = (md[i + 1][0] + p) % (m - 1);
      else
        md[i][0] = md[i + 1][0];
      p = (2 * p) % (m - 1);
    }
  }
  if (m + 1 <= n) {
    p = 1;
    for (i = n - 1; i >= 0; i--) {
      if (s[i] == '1')
        md[i][1] = (md[i + 1][1] + p) % (m + 1);
      else
        md[i][1] = md[i + 1][1];
      p = (2 * p) % (m + 1);
    }
  }

  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      d = m > 1 ? (1 + ns[((md[i + 1][0] +
                            (((md[0][0] - md[i][0]) + (m - 1)) % (m - 1))) %
                           (m - 1))])
                : 0;
    } else {
      d = 1 +
          ns[((((md[i + 1][1] + (((md[0][1] - md[i][1]) + (m + 1)) % (m + 1))) %
                (m + 1)) +
               powMod(2, n - i - 1, m + 1)) %
              (m + 1))];
    }
    cout << d << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FIN                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

const int INF = 1000000000;
const int MOD = 1000000007;

ll powMod(ll n, ll p, ll m) {
  if (p == 0)
    return 1;
  if (p == 1)
    return n % m;
  ll e = powMod(n, p / 2, m);
  e = (e * e) % m;
  if (p & 1)
    e = (e * n) % m;
  return e;
}

ll fn(ll i, vll &ns) {
  if (ns[i] != -1)
    return ns[i];
  return 1 + fn(i % __builtin_popcount(i), ns);
}

int main() {
  FIN

      ll n,
      m = 0, i, p, d;
  string s;
  cin >> n;
  vll ns(n + 1, -1);
  vvll md(n + 1, vll(2));
  cin >> s;
  for (i = 0; i < n; i++)
    if (s[i] == '1')
      m++;

  ns[0] = 0;
  for (i = 1; i <= n; i++) {
    ns[i] = fn(i, ns);
  }

  if (m - 1 > 0) {
    p = 1;
    for (i = n - 1; i >= 0; i--) {
      if (s[i] == '1')
        md[i][0] = (md[i + 1][0] + p) % (m - 1);
      else
        md[i][0] = md[i + 1][0];
      p = (2 * p) % (m - 1);
    }
  }
  if (m + 1 <= n) {
    p = 1;
    for (i = n - 1; i >= 0; i--) {
      if (s[i] == '1')
        md[i][1] = (md[i + 1][1] + p) % (m + 1);
      else
        md[i][1] = md[i + 1][1];
      p = (2 * p) % (m + 1);
    }
  }

  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      d = 1 +
          ns[((md[i + 1][0] + (((md[0][0] - md[i][0]) + (m - 1)) % (m - 1))) %
              (m - 1))];
    } else {
      d = 1 +
          ns[((((md[i + 1][1] + (((md[0][1] - md[i][1]) + (m + 1)) % (m + 1))) %
                (m + 1)) +
               powMod(2, n - i - 1, m + 1)) %
              (m + 1))];
    }
    cout << d << "\\n";
  }

  return 0;
}