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

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 2e5 + 5;

vector<int> f(N);

ll pwr(ll a, ll b, ll mod) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  f[0] = 0;
  f[1] = 1;
  for (int i = 2; i < N; i++) {
    f[i] = f[i % __builtin_popcount(i)] + 1;
  }

  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (s[i] == '1');

  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << '\\n';
    }
    return 0;
  }
  if (cnt == 1) {
    int b = 0;

    for (int i = 0; i < n; i++) {
      if (s[i] == '0')
        continue;
      b += pwr(2, n - i - 1, 2);
      b %= 2;
    }

    for (int i = 0; i < n; i++) {
      int ans;
      if (s[i] == '0') {
        ans = (b + pwr(2, n - i - 1, 2)) % 2;
        cout << f[ans] + 1 << '\\n';
      } else {
        cout << 0 << '\\n';
      }
    }

    return 0;
  }

  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    a += pwr(2, n - i - 1, cnt - 1);
    a %= (cnt - 1);
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    b += (pwr(2, n - i - 1, cnt + 1));
    b %= (cnt + 1);
  }

  for (int i = 0; i < n; i++) {
    int ans;
    if (s[i] == '0') {
      // use cnt + 1;
      ans = (b + pwr(2, n - i - 1, cnt + 1)) % (cnt + 1);
    } else {
      // use cnt - 1;
      ans = (a - pwr(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
    }
    cout << f[ans] + 1 << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 2e5 + 5;

vector<int> f(N);

ll pwr(ll a, ll b, ll mod) {
  if (mod < 2)
    return 0;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  f[1] = 1;
  for (int i = 2; i < N; i++) {
    f[i] = f[i % __builtin_popcount(i)] + 1;
  }

  int n;
  string s;
  cin >> n >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++)
    cnt += (s[i] == '1');

  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << 0 << '\\n';
    }
    return 0;
  }
  if (cnt == 1) {
    int b = 0;

    for (int i = 0; i < n; i++) {
      if (s[i] == '0')
        continue;
      b += pwr(2, n - i - 1, 2);
      b %= 2;
    }

    for (int i = 0; i < n; i++) {
      int ans;
      if (s[i] == '0') {
        ans = (b + pwr(2, n - i - 1, 2)) % 2;
        cout << f[ans] + 1 << '\\n';
      } else {
        cout << 0 << '\\n';
      }
    }
  }

  int a = 0, b = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    a += pwr(2, n - i - 1, cnt - 1);
    a %= (cnt - 1);
  }

  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    b += (pwr(2, n - i - 1, cnt + 1));
    b %= (cnt + 1);
  }

  for (int i = 0; i < n; i++) {
    int ans;
    if (s[i] == '0') {
      // use cnt + 1;
      ans = (b + pwr(2, n - i - 1, cnt + 1)) % (cnt + 1);
    } else {
      // use cnt - 1;
      ans = (a - pwr(2, n - i - 1, cnt - 1) + cnt - 1) % (cnt - 1);
    }
    cout << f[ans] + 1 << '\\n';
  }
}