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

#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1000000000000000000
#define N 200001

using namespace std;
ll table[N + 2];

ll power(ll n, ll k, ll mod) {
  ll res = 1;
  while (k > 0) {
    if (k & 1)
      res = res * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return res;
}

ll popcount(ll a) {
  ll ret = 0;
  for (int i = 0; i <= 20; i++) {
    if ((a >> i) == 0)
      break;

    if ((a >> i) & 1)
      ret++;
  }
  return ret;
}

void make_table() {
  for (int i = 1; i <= N; i++) {
    ll t = i;
    ll num = 0;
    while (t != 0) {
      t = t % popcount(t);
      num++;
    }
    table[i] = num;
  }
}

int main(void) {
  make_table();

  ll n;
  cin >> n;

  string x;
  cin >> x;

  ll pc = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      pc++;
  }
  if (pc != 1 && pc != 0) {
    ll rem[2] = {0, 0};
    for (int j = 0; j < n; j++) {
      if (x[j] == '1') {
        rem[0] = (rem[0] + power(2, n - j - 1, pc - 1)) % (pc - 1);
        rem[1] = (rem[1] + power(2, n - j - 1, pc + 1)) % (pc + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        ll t = (rem[0] - power(2, n - i - 1, pc - 1) + pc - 1) % (pc - 1);
        cout << table[t] + 1 << endl;
      } else {
        ll t = (rem[1] + power(2, n - i - 1, pc + 1) + pc + 1) % (pc + 1);
        cout << table[t] + 1 << endl;
      }
    }
  } else if (pc == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
  } else if (pc == 1) {
    ll rem[1] = {0};
    for (int j = 0; j < n; j++) {
      if (x[j] == '1') {
        rem[1] = (rem[1] + power(2, n - j - 1, pc + 1)) % (pc + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        ll t = (rem[1] + power(2, n - i - 1, pc + 1) + pc + 1) % (pc + 1);
        cout << table[t] + 1 << endl;
      }
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define MOD 1000000007
#define INF 1000000000000000000
#define N 200001

using namespace std;
ll table[N + 2];

ll power(ll n, ll k, ll mod) {
  ll res = 1;
  while (k > 0) {
    if (k & 1)
      res = res * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return res;
}

ll popcount(ll a) {
  ll ret = 0;
  for (int i = 0; i <= 20; i++) {
    if ((a >> i) == 0)
      break;

    if ((a >> i) & 1)
      ret++;
  }
  return ret;
}

void make_table() {
  for (int i = 1; i <= N; i++) {
    ll t = i;
    ll num = 0;
    while (t != 0) {
      t = t % popcount(t);
      num++;
    }
    table[i] = num;
  }
}

int main(void) {
  make_table();

  ll n;
  cin >> n;

  string x;
  cin >> x;

  ll pc = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      pc++;
  }
  ll rem[2] = {};
  for (int j = 0; j < n; j++) {
    if (x[j] == '1') {
      rem[0] = (rem[0] + power(2, n - j - 1, pc - 1)) % (pc - 1);
      rem[1] = (rem[1] + power(2, n - j - 1, pc + 1)) % (pc + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      ll t = (rem[0] - power(2, n - i - 1, pc - 1) + pc - 1) % (pc - 1);
      cout << table[t] + 1 << endl;
    } else {
      ll t = (rem[1] + power(2, n - i - 1, pc + 1) + pc + 1) % (pc + 1);
      cout << table[t] + 1 << endl;
    }
  }

  return 0;
}