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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define F first
#define S second

const int mxn = 2e5 + 5;
int n, ps[mxn], pb[mxn];
string s;

int sim(int a) {
  int ans = 0;
  // int t = a;
  while (a > 0) {
    // cout << a << '\\n';
    a = a % __builtin_popcount(a);
    ++ans;
  }
  // cout << t << ": " << ans << '\\n';
  return ans;
}

void solve() {
  cin >> n >> s;

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

  int modsmall = pc - 1, modbig = pc + 1;
  int anss, ansb;

  if (modsmall >= 1) {
    ps[0] = 1 % modsmall;
    for (int i = 1; i <= n; ++i) {
      ps[i] = (2 * ps[i - 1]) % modsmall;
    }

    anss = 0;
    for (int i = 0; i < n; ++i) {
      if (s[n - 1 - i] == '1') {
        anss = (anss + ps[i]) % modsmall;
      }
    }
  }

  pb[0] = 1 % modbig;
  for (int i = 1; i <= n; ++i) {
    pb[i] = (2 * pb[i - 1]) % modbig;
  }

  ansb = 0;
  for (int i = 0; i < n; ++i) {
    if (s[n - 1 - i] == '1') {
      ansb = (ansb + pb[i]) % modbig;
    }
  }

  // cout << modsmall << " " << modbig << '\\n';
  // cout << anss << " " << ansb << '\\n';

  // int test = 0b00110111001011011001110;
  // cout << anss << " " << test%modsmall << '\\n';
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      // small
      if (modsmall < 1) {
        cout << "0\\n";
      } else {
        int ansn = ((anss - ps[n - 1 - i]) % modsmall + modsmall) % modsmall;
        // cout << ansn << '\\n';
        // int temp = test ^ (1<<(n-1-i));
        // cout << temp%modsmall << " " << ansn%modsmall << '\\n';
        // assert(temp%modsmall == ansn%modsmall);
        cout << sim(ansn) + 1 << '\\n';
        // cout << "-1\\n";
      }
    } else {
      // big
      int ansn = (ansb + pb[n - 1 - i]) % modbig;
      // int temp = test ^ (1<<(n-1-i));
      // assert(temp%modbig == ansn%modbig);
      // cout << ansn << '\\n';
      cout << sim(ansn) + 1 << '\\n';
      // cout << "-1\\n";
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define F first
#define S second

const int mxn = 2e5 + 5;
int n, ps[mxn], pb[mxn];
string s;

int sim(int a) {
  int ans = 0;
  // int t = a;
  while (a > 0) {
    // cout << a << '\\n';
    a = a % __builtin_popcount(a);
    ++ans;
  }
  // cout << t << ": " << ans << '\\n';
  return ans;
}

void solve() {
  cin >> n >> s;

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

  int modsmall = pc - 1, modbig = pc + 1;

  ps[0] = 1 % modsmall, pb[0] = 1 % modbig;
  for (int i = 1; i <= n; ++i) {
    ps[i] = (2 * ps[i - 1]) % modsmall;
    pb[i] = (2 * pb[i - 1]) % modbig;
  }

  int anss = 0, ansb = 0;
  for (int i = 0; i < n; ++i) {
    if (s[n - 1 - i] == '1') {
      anss = (anss + ps[i]) % modsmall;
      ansb = (ansb + pb[i]) % modbig;
    }
  }

  // cout << modsmall << " " << modbig << '\\n';
  // cout << anss << " " << ansb << '\\n';

  // int test = 0b00110111001011011001110;
  // cout << anss << " " << test%modsmall << '\\n';
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      // small
      int ansn = ((anss - ps[n - 1 - i]) % modsmall + modsmall) % modsmall;
      // cout << ansn << '\\n';
      // int temp = test ^ (1<<(n-1-i));
      // cout << temp%modsmall << " " << ansn%modsmall << '\\n';
      // assert(temp%modsmall == ansn%modsmall);
      cout << sim(ansn) + 1 << '\\n';
      // cout << "-1\\n";
    } else {
      // big
      int ansn = (ansb + pb[n - 1 - i]) % modbig;
      // int temp = test ^ (1<<(n-1-i));
      // assert(temp%modbig == ansn%modbig);
      // cout << ansn << '\\n';
      cout << sim(ansn) + 1 << '\\n';
      // cout << "-1\\n";
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}