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

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

bool isprime(int n) {
  double rootn = sqrt(n);
  if (n < 2) {
    return false;
  } else if (n == 2) {
    return true;
  } else if (n % 2 == 0) {
    return false;
  } else {
    for (int i = 3; i <= rootn; i += 2) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

int popcount(int n) {
  int ans = 0;
  while (n != 0) {
    if (n % 2 == 1) {
      ans++;
    }
    n /= 2;
  }
  return ans;
}

int mod_pow(int n, int e, int mod) {
  int ans = 1;
  while (e != 0) {
    if (e % 2 == 1) {
      ans = (ans * n) % mod;
    }
    e /= 2;
    n = (n * n) % mod;
  }
  return ans % mod;
}

signed main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N);
  int pc = 0;
  for (int i = 0; i < N; i++) {
    X[i] = s[i] - '0';
    if (X[i] == 1) {
      pc++;
    }
  }

  // rem[0] が 1 引いた後
  int rem[2] = {0, 0};
  for (int j = 0; j < N; j++) {
    if (pc > 1) {
      rem[0] = (rem[0] * 2 + X[j]) % (pc - 1);
    }
    rem[1] = (rem[1] * 2 + X[j]) % (pc + 1);
  }

  for (int i = 0; i < N; i++) {
    X[i] = 1 - X[i];
    pc = pc + 2 * X[i] - 1;

    if (pc == 0) {
      cout << 0 << endl;
      pc = pc - 2 * X[i] + 1;
      X[i] = 1 - X[i];
      continue;
    }

    int r = rem[X[i]], c = 1;
    // r + (2*X[i] - 1) * 2**(N-1-i)
    int fugou = 1;
    if (X[i] == 0) {
      fugou = pc - 1;
    }
    r = (r + fugou * mod_pow(2, N - 1 - i, pc)) % pc;
    while (r != 0) {
      c++;
      r = r % popcount(r);
    }
    cout << c << endl;

    pc = pc - 2 * X[i] + 1;
    X[i] = 1 - X[i];
  }
  return 0;
}

Error: 0

Fixed submission:

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

bool isprime(int n) {
  double rootn = sqrt(n);
  if (n < 2) {
    return false;
  } else if (n == 2) {
    return true;
  } else if (n % 2 == 0) {
    return false;
  } else {
    for (int i = 3; i <= rootn; i += 2) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

int popcount(int n) {
  int ans = 0;
  while (n != 0) {
    if (n % 2 == 1) {
      ans++;
    }
    n /= 2;
  }
  return ans;
}

int mod_pow(int n, int e, int mod) {
  int ans = 1;
  while (e != 0) {
    if (e % 2 == 1) {
      ans = (ans * n) % mod;
    }
    e /= 2;
    n = (n * n) % mod;
  }
  return ans % mod;
}

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N);
  int pc = 0;
  for (int i = 0; i < N; i++) {
    X[i] = s[i] - '0';
    if (X[i] == 1) {
      pc++;
    }
  }

  // rem[0] が 1 引いた後
  int rem[2] = {0, 0};
  for (int j = 0; j < N; j++) {
    if (pc > 1) {
      rem[0] = (rem[0] * 2 + X[j]) % (pc - 1);
    }
    rem[1] = (rem[1] * 2 + X[j]) % (pc + 1);
  }

  for (int i = 0; i < N; i++) {
    X[i] = 1 - X[i];
    pc = pc + 2 * X[i] - 1;

    if (pc == 0) {
      cout << 0 << endl;
      pc = pc - 2 * X[i] + 1;
      X[i] = 1 - X[i];
      continue;
    }

    int r = rem[X[i]], c = 1;
    // r + (2*X[i] - 1) * 2**(N-1-i)
    int fugou = 1;
    if (X[i] == 0) {
      fugou = pc - 1;
    }
    r = (r + fugou * mod_pow(2, N - 1 - i, pc)) % pc;
    while (r != 0) {
      c++;
      r = r % popcount(r);
    }
    cout << c << endl;

    pc = pc - 2 * X[i] + 1;
    X[i] = 1 - X[i];
  }
  return 0;
}