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

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (ll)(v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \\
  { cout << a << "\\n"; }
#define tell(a, b)                                                             \\
  { cout << a << " | " << b << "\\n"; }
#define telll(a, b, c)                                                         \\
  { cout << a << " | " << b << " | " << c << "\\n"; }
#define teln(v, n)                                                             \\
  {                                                                            \\
    cout << "v- ";                                                             \\
    rep(o, 0, n) cout << v[o] << " ";                                          \\
    cout << "\\n";                                                              \\
  }
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

#define M 300010
ll ex(ll a, ll b, ll c) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % c;
    a = (a * a) % c, b /= 2;
  }
  return ans;
}

// ll dp[M];
ll f(ll n) {
  if (!n)
    return 0;
  // if(dp[n]!=-1)return dp[n];
  ll x = __builtin_popcount(n);
  return 1 + f(n % x);
}
int main() {
  IOS;

  // memset(dp,-1,sizeof(dp));
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  rep(i, 0, n) if (s[i] == '1') cnt++;
  ll t[2] = {0}, temp;
  rep(i, 0, n) {
    if (s[i] != '1')
      continue;
    if (cnt > 1)
      t[0] += ex(2, n - 1 - i, cnt - 1);
    t[1] += ex(2, n - 1 - i, cnt + 1);
  }
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (cnt == 1) {
        cout << "0\\n";
        continue;
      }
      temp = t[0] - ex(2, n - 1 - i, cnt - 1), (temp += (cnt - 1)) %= (cnt - 1);
      cout << f(temp) + 1 << "\\n";
    } else {
      temp = t[1] + ex(2, n - 1 - i, cnt + 1), (temp) %= (cnt + 1);
      cout << f(temp) + 1 << "\\n";
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (ll)(v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define tel(a)                                                                 \\
  { cout << a << "\\n"; }
#define tell(a, b)                                                             \\
  { cout << a << " | " << b << "\\n"; }
#define telll(a, b, c)                                                         \\
  { cout << a << " | " << b << " | " << c << "\\n"; }
#define teln(v, n)                                                             \\
  {                                                                            \\
    cout << "v- ";                                                             \\
    rep(o, 0, n) cout << v[o] << " ";                                          \\
    cout << "\\n";                                                              \\
  }
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;

#define M 300010
ll ex(ll a, ll b, ll c) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % c;
    a = (a * a) % c, b /= 2;
  }
  return ans;
}

ll dp[M];
ll f(ll n) {
  if (!n)
    return 0;
  if (dp[n] != -1)
    return dp[n];
  ll x = __builtin_popcount(n);
  return dp[n] = 1 + f(n % x);
}
int main() {
  IOS;

  memset(dp, -1, sizeof(dp));
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  rep(i, 0, n) if (s[i] == '1') cnt++;
  ll t[2] = {0}, temp;
  rep(i, 0, n) {
    if (s[i] != '1')
      continue;
    t[0] += ex(2, n - 1 - i, cnt - 1);
    t[1] += ex(2, n - 1 - i, cnt + 1);
  }
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (cnt == 1) {
        cout << "0\\n";
        continue;
      }
      temp = t[0] - ex(2, n - 1 - i, cnt - 1), (temp += (cnt - 1)) %= (cnt - 1);
      cout << f(temp) + 1 << "\\n";
    } else {
      temp = t[1] + ex(2, n - 1 - i, cnt + 1), (temp) %= (cnt + 1);
      cout << f(temp) + 1 << "\\n";
    }
  }
  return 0;
}