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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> vi;
// #define endl '\\n'
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout << x << endl;
#define pret(x)                                                                \\
  cout << x << endl;                                                           \\
  return 0;
#define disp(x) cout << x << " ";
#define let(x, y) cout << x << " " << y << endl;
// ll MOD=1e9+7;
const int maxn = 2e5 + 5;

int ans[maxn];
int val[2];

ll power(ll a, ll b, ll MOD) {
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2) {
      x = (x * y) % MOD;
    }
    y = (y * y) % MOD;
    b /= 2;
  }
  return x % MOD;
}

int calc(int x) {
  int temp = __builtin_popcount(x);
  temp = x % temp;
  return 1 + ans[temp];
}

int32_t main() {
  fast_io;
  int n;
  cin >> n;
  string s;
  cin >> s;
  fo(i, 1, maxn - 1) { ans[i] = calc(i); }
  // show(ans[8]);
  vi v;
  int tot = 0;
  for (int i = 0; i < n; ++i) {
    v.emplace_back(s[i] - '0');
    tot += v[i];
  }
  if (tot == 1) {
    if (n == 1) {
      show(0);
      return 0;
    }
    for (int i = 0; i < n - 1; ++i) {
      if (v[i]) {
        show(0);
      } else {
        show(1 + v[n - 1]);
      }
    }
    if (v.back()) {
      show(0);
    } else
      show(2);
    return 0;
  }
  // show(tot);
  for (int i = 0; i < n; ++i) {
    if (v[i]) {
      val[0] += power(2, n - 1 - i, tot - 1);
      val[1] += power(2, n - 1 - i, tot + 1);
    }
  }
  // let(val[0],val[1]);
  for (int i = 0; i < n; ++i) {
    if (v[i]) {
      int temp = val[0] - power(2, n - 1 - i, tot - 1);
      temp %= (tot - 1);
      // let(i,temp);
      show(1 + ans[temp]);
    } else {
      int temp = val[1] + power(2, n - 1 - i, tot + 1);
      temp %= (tot + 1);
      // let(i,temp);
      show(1 + ans[temp]);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> vi;
// #define endl '\\n'
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fast_io ios_base::sync_with_stdio(0);
#define show(x) cout << x << endl;
#define pret(x)                                                                \\
  cout << x << endl;                                                           \\
  return 0;
#define disp(x) cout << x << " ";
#define let(x, y) cout << x << " " << y << endl;
// ll MOD=1e9+7;
const int maxn = 2e5 + 5;

int ans[maxn];
int val[2];

ll power(ll a, ll b, ll MOD) {
  if (MOD == 0)
    return 0;
  ll x = 1, y = a;
  while (b > 0) {
    if (b % 2) {
      x = (x * y) % MOD;
    }
    y = (y * y) % MOD;
    b /= 2;
  }
  return x % MOD;
}

int calc(int x) {
  int temp = __builtin_popcount(x);
  temp = x % temp;
  return 1 + ans[temp];
}

int32_t main() {
  fast_io;
  int n;
  cin >> n;
  string s;
  cin >> s;
  fo(i, 1, maxn - 1) { ans[i] = calc(i); }
  // show(ans[8]);
  vi v;
  int tot = 0;
  for (int i = 0; i < n; ++i) {
    v.emplace_back(s[i] - '0');
    tot += v[i];
  }
  if (tot == 1) {
  }
  // show(tot);
  for (int i = 0; i < n; ++i) {
    if (v[i]) {
      val[0] += power(2, n - 1 - i, tot - 1);
      val[1] += power(2, n - 1 - i, tot + 1);
    }
  }
  // let(val[0],val[1]);
  for (int i = 0; i < n; ++i) {
    if (v[i]) {
      if (tot == 1) {
        show(0);
        continue;
      }
      int temp = val[0] - power(2, n - 1 - i, tot - 1);
      // let(i,temp);
      show(1 + ans[temp]);
    } else {
      int temp = val[1] + power(2, n - 1 - i, tot + 1);
      temp %= (tot + 1);
      // let(i,temp);
      show(1 + ans[temp]);
    }
  }
  return 0;
}