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

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

#define pb push_back
#define f1 first
#define s2 second

#define fastio                                                                 \\
  ios ::sync_with_stdio();                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(x...) cerr << "[" << #x << "]: " << x << "\\n";

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pl;

int const N = 2e5 + 7;
int dp[N];
int mod[N][2]; // 0: k-1, 1: k+1

int n;
char s[N];

inline int popcount(int n) {
  int res = 0;
  while (n) {
    res += (n & 1);
    n = (n >> 1);
  }
  return res;
}

inline int modulo(int idx, int k) {
  int res = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      res = (res + mod[i][idx]) % k;
  }
  return res;
}

int main() {
  fastio;

  dp[0] = 0;
  for (int i = 1; i < N; ++i)
    dp[i] = 1 + dp[i % popcount(i)];

  cin >> n;
  cin >> s;

  int k = 0;
  for (int i = 0; i < n; ++i)
    k += (s[i] == '1');

  int a; // S mod (k-1)
  int b; // S mod (k+1);

  if (k != 1) {
    mod[n - 1][0] = 1 % (k - 1);
    for (int i = n - 2; i >= 0; --i)
      mod[i][0] = (mod[i + 1][0] * 2) % (k - 1);

    a = modulo(0, k - 1);
  }

  if (k != n) {
    mod[n - 1][1] = 1 % (k + 1);
    for (int i = n - 2; i >= 0; --i)
      mod[i][1] = (mod[i + 1][1] * 2) % (k + 1);

    b = modulo(1, k + 1);
  }

  for (int i = 0; i < n; ++i) {
    if (k == 1 && s[i] == '1') {
      cout << 0 << '\\n';
    } else if (s[i] == '0') {
      int res = (b + mod[i][1]) % (k + 1);
      cout << dp[res] + 1 << '\\n';
    } else {
      int res = (((a - mod[i][0]) % (k - 1)) + k - 1) % (k - 1);
      cout << dp[res] + 1 << '\\n';
    }
  }

  return 0;
}


Error: 0

Fixed submission:

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

#define pb push_back
#define f1 first
#define s2 second

#define fastio                                                                 \\
  ios ::sync_with_stdio();                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(x...) cerr << "[" << #x << "]: " << x << "\\n";

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pl;

int const N = 2e5 + 7;
int dp[N];
int mod[N][2]; // 0: k-1, 1: k+1

int n;
char s[N];

inline int popcount(int n) {
  int res = 0;
  while (n) {
    res += (n & 1);
    n = (n >> 1);
  }
  return res;
}

inline int modulo(int idx, int k) {
  int res = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      res = (res + mod[i][idx]) % k;
  }
  return res;
}

int main() {
  fastio;

  dp[0] = 0;
  for (int i = 1; i < N; ++i)
    dp[i] = 1 + dp[i % popcount(i)];

  cin >> n;
  cin >> s;

  int k = 0;
  for (int i = 0; i < n; ++i)
    k += (s[i] == '1');

  int a; // S mod (k-1)
  int b; // S mod (k+1);

  if (k != 0) {
    mod[n - 1][0] = 1 % (k - 1);
    for (int i = n - 2; i >= 0; --i)
      mod[i][0] = (mod[i + 1][0] * 2) % (k - 1);

    a = modulo(0, k - 1);
  }

  if (k != n) {
    mod[n - 1][1] = 1 % (k + 1);
    for (int i = n - 2; i >= 0; --i)
      mod[i][1] = (mod[i + 1][1] * 2) % (k + 1);

    b = modulo(1, k + 1);
  }

  for (int i = 0; i < n; ++i) {
    if (k == 1 && s[i] == '1') {
      cout << 0 << '\\n';
    } else if (s[i] == '0') {
      int res = (b + mod[i][1]) % (k + 1);
      cout << dp[res] + 1 << '\\n';
    } else {
      int res = (a - mod[i][0] + k - 1) % (k - 1);
      cout << dp[res] + 1 << '\\n';
    }
  }

  return 0;
}