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

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

int getPopcount(const string &X) {
  int cnt = 0;
  for (char ch : X)
    cnt += (ch == '1') ? 1 : 0;
  return cnt;
}

int getPopcount(int X) {
  int cnt = 0;
  while (X > 0) {
    cnt += (X & 1);
    X = X >> 1;
  }
  return cnt;
}

int pow(long long base, long long p, long long rem) {
  if (p == 0)
    return (long long)1;
  if (p % 2 == 1) {
    long long val = base * pow(base, p - 1, rem);
    return (int)(val % rem);
  }
  long long val = pow(base, p / 2, rem);
  val %= rem;
  long long ans = ((val * val) % rem);
  return (int)ans;
}

int getModValue(const string &X, int pop) {
  int rem = 0;
  for (int i = X.size() - 1, j = 0; i >= 0; --i, ++j) {
    if (X[i] == '1') {
      rem += pow(2, j, pop);
      rem %= pop;
    }
  }
  return rem;
}

int compute(int num) {
  if (num == 0)
    return 0;
  int pc = getPopcount(num);
  return 1 + compute(num % pc);
}

void solve() {
  int N;
  string X;
  cin >> N >> X;

  int pop = getPopcount(X);
  int modPlus = getModValue(X, pop + 1);
  int modMinus = pop - 1 > 0 ? getModValue(X, pop - 1) : 0;

  for (int i = 0, j = N - 1; i < N; ++i, --j) {
    bool add = (X[i] == '0');
    int popChild = add ? pop + 1 : pop - 1;
    if (popChild <= 0) {
      cout << 0 << "\\n";
    } else {
      int powChild = pow(2, j, popChild);
      int modChild =
          add ? (modPlus + powChild) : (modMinus - powChild + popChild);
      modChild = (modChild) % popChild;
      cout << 1 + compute(modChild) << "\\n";
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
  return 0;
}


Error: 0

Fixed submission:

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

int getPopcount(const string &X) {
  int cnt = 0;
  for (char ch : X)
    cnt += (ch == '1') ? 1 : 0;
  return cnt;
}

int getPopcount(int X) {
  int cnt = 0;
  while (X > 0) {
    cnt += (X & 1);
    X = X >> 1;
  }
  return cnt;
}

int pow(int base, int p, int rem) {
  if (p == 0)
    return 1;
  if (p % 2 == 1) {
    int val = base * pow(base, p - 1, rem);
    return val % rem;
  }
  int val = pow(base, p / 2, rem);
  val %= rem;
  return (val * val) % rem;
}

int getModValue(const string &X, int pop) {
  int rem = 0;
  for (int i = X.size() - 1, j = 0; i >= 0; --i, ++j) {
    if (X[i] == '1') {
      rem += pow(2, j, pop);
      rem %= pop;
    }
  }
  return rem;
}

int compute(int num) {
  if (num == 0)
    return 0;
  int pc = getPopcount(num);
  // /cout << "num = " << num << " pc " << pc << "\\n";
  return 1 + compute(num % pc);
}

void solve() {
  int N;
  string X;
  cin >> N >> X;
  int pop = getPopcount(X);
  int modPlus = getModValue(X, pop + 1);
  int modMinus = (pop - 1) <= 0 ? 0 : getModValue(X, pop - 1);

  for (int i = 0, j = N - 1; i < N; ++i, --j) {
    bool add = (X[i] == '0');
    int popChild = add ? pop + 1 : pop - 1;
    if (popChild <= 0)
      popChild = 1;
    int powChild = pow(2, j, popChild);
    int modChild = add ? modPlus + powChild : (modMinus - powChild + popChild);
    modChild = (modChild + popChild) % popChild;

    cout << 1 + compute(modChild) << "\\n";
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  solve();
}