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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
// #define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

ll md(string &s, int m) {
  ll res = 0;
  ll cur = 1;
  for (int i = s.length() - 1; i >= 0; --i) {
    if (s[i] == '1') {
      res = res + cur;
      res %= m;
    }
    cur = cur * 2;
    cur %= m;
  }
  return res;
}

int c(string &s) {
  int ans = 0;
  for (int i = 0; i < int(s.length()); ++i) {
    ans += s[i] == '1';
  }
  return ans;
}

ll bin(ll a, ll b, ll m) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    res %= m;
    b >>= 1;
    a *= a;
    a %= m;
  }
  return res % m;
}

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

  int cnt = c(s);
  ll m1 = md(s, max(cnt - 1, 1));
  ll m2 = md(s, cnt + 1);
  for (int i = 0; i < n; ++i) {
    // cout<<i<<endl;
    if (s[i] == '1') {
      if (cnt - 1 <= 0) {
        cout << 0 << endl;
        continue;
      }
      ll temp = (m1 - bin(2, n - i - 1, (cnt - 1)) + (cnt - 1)) % (cnt - 1);
      ll res = 1;
      while (temp != 0) {
        temp %= __builtin_popcountll(temp);
        res++;
      }
      cout << res << endl;
    } else {
      ll temp = (m2 + bin(2, n - i - 1, (cnt + 1)) + (cnt + 1)) % (cnt + 1);
      ll res = 1;
      while (temp != 0) {
        // cout<<temp<<endl;
        temp %= __builtin_popcountll(temp);
        res++;
      }
      cout << res << endl;
    }
  }
}

int32_t main() {
  fastio int t;
  t = 1; // cin>>t;
  while (t--) {
    solve();
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll, ll>
#define tii tuple<ll, ll, ll>
#define all(v) (v).begin(), (v).end()
#define ll long long
#define ull unsigned long long
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
// #define endl "\\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

string conv(ll num) {
  string ans;
  while (num > 0) {
    if (num % 2)
      ans += '1';
    else
      ans += '0';
    num /= 2;
  }
  reverse(all(ans));
  return ans;
}

ll md(string &s, int m) {
  ll res = 0;
  ll cur = 1;
  for (int i = s.length() - 1; i >= 0; --i) {
    if (s[i] == '1') {
      res = res + cur;
      res %= m;
    }
    cur = cur * 2;
    cur %= m;
  }
  return res;
}

int c(string &s) {
  int ans = 0;
  for (int i = 0; i < int(s.length()); ++i) {
    ans += s[i] == '1';
  }
  return ans;
}

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

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

  int cnt = c(s);
  ll m1 = md(s, cnt - 1);
  ll m2 = md(s, cnt + 1);
  for (int i = 0; i < n; ++i) {
    // cout<<i<<endl;
    if (s[i] == '1') {
      if (cnt - 1 < 0) {
        cout << 0 << endl;
        continue;
      }
      if (cnt - 1 == 0) {
        cout << 1 << endl;
        continue;
      }
      ll temp = (m1 - bin(2, n - i - 1, (cnt - 1)) + (cnt - 1)) % (cnt - 1);
      ll res = 1;
      while (temp != 0) {
        temp %= __builtin_popcountll(temp);
        res++;
      }
      cout << res << endl;
    } else {
      ll temp = (m2 + bin(2, n - i - 1, (cnt + 1)) + (cnt + 1)) % (cnt + 1);
      ll res = 1;
      while (temp != 0) {
        // cout<<temp<<endl;
        temp %= __builtin_popcountll(temp);
        res++;
      }
      cout << res << endl;
    }
  }
}

int32_t main() {
  fastio int t;
  t = 1; // cin>>t;
  while (t--) {
    solve();
  }
}