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

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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (auto i = 0; i < n; i++)
#define Rep1(i, n) for (auto i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

vector<int> MEM(200001, -1);

int F(const int &S) {
  if (MEM.at(S) >= 0)
    return MEM.at(S);
  if (0 == S) {
    MEM.at(S) = 0;
  } else {
    int S0 = S;
    int B = 0;
    while (S0 > 0) {
      if (1 == S0 % 2)
        B++;
      S0 /= 2;
    }
    MEM.at(S) = F(S % B) + 1;
  }
  return MEM.at(S);
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  Rev(X);
  int M = 0;
  Rep0(i, N) if ('1' == X.at(i)) M++;
  int I0 = 1;
  int I1 = 1;
  int D0 = 0;
  int D1 = 0;
  Rep0(i, N) {
    if ('1' == X.at(i)) {
      if (1 != M) {
        D0 += I0;
        D0 %= M - 1;
      }
      D1 += I1;
      D1 %= M + 1;
    }
    if (1 != M) {
      I0 *= 2;
      I0 %= M - 1;
    }
    I1 *= 2;
    I1 %= M + 1;
  }
  int DD0 = D0;
  int DD1 = D1;
  I0 = 1;
  I1 = 1;
  vector<int> A(N + 1, -1);
  Rep0(i, N) {
    int SS = 0;
    int SSS = 0;
    if ('1' == X.at(i)) {
      if (1 != M) {
        SS = DD0;
        SS += -I0 + M - 1;
        SS %= M - 1;
        SSS++;
      } else { // 1==M
        A.at(N - i) = 0;
      }
    } else {
      SS = DD1;
      SS += I1;
      SS %= M + 1;
      SSS++;
    }
    if (-1 == A.at(N - i))
      A.at(N - i) = F(SS) + SSS;
    //
    if (1 != M) {
      I0 *= 2;
      I0 %= M - 1;
    }
    I1 *= 2;
    I1 %= M + 1;
  }
  Rep1(i, N) cout << A.at(i) << endl;
  return 0;
}


Error: 0

Fixed submission:

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

// int/long:		-2,147,483,648 - 2,147,483,647 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807
// (-2^63 <= long < 2^63)

#define INF (1 << 30)
// 1,073,741,824
//= 536,870,912 *2

#define MOD 1000000007

#define Rep0(i, n) for (auto i = 0; i < n; i++)
#define Rep1(i, n) for (auto i = 1; i <= n; i++)
#define Sort(P) sort(P.begin(), P.end())
#define Rev(P) reverse(P.begin(), P.end())

vector<int> MEM(200001, -1);

int F(const int &S) {
  if (MEM.at(S) >= 0)
    return MEM.at(S);
  if (0 == S) {
    MEM.at(S) = 0;
  } else {
    int S0 = S;
    int B = 0;
    while (S0 > 0) {
      if (1 == S0 % 2)
        B++;
      S0 /= 2;
    }
    MEM.at(S) = F(S % B) + 1;
  }
  return MEM.at(S);
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  Rev(X);
  int M = 0;
  Rep0(i, N) if ('1' == X.at(i)) M++;
  int I0 = 1;
  int I1 = 1;
  int D0 = 0;
  int D1 = 0;
  Rep0(i, N) {
    if ('1' == X.at(i)) {
      D0 += I0;
      D0 %= M - 1;
      D1 += I1;
      D1 %= M + 1;
    }
    I0 *= 2;
    I0 %= M - 1;
    I1 *= 2;
    I1 %= M + 1;
  }
  int DD0 = D0;
  int DD1 = D1;
  I0 = 1;
  I1 = 1;
  vector<int> A(N + 1, 0);
  Rep0(i, N) {
    int SS = 0;
    if ('1' == X.at(i)) {
      SS = DD0;
      SS += -I0 + M - 1;
      SS %= M - 1;
    } else {
      SS = DD1;
      SS += I1;
      SS %= M + 1;
    }
    A.at(N - i) = F(SS);
    //
    I0 *= 2;
    I0 %= M - 1;
    I1 *= 2;
    I1 %= M + 1;
  }
  Rep1(i, N) cout << A.at(i) + 1 << endl;
  return 0;
}