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

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define hii cout << "hii" << endl
#define int ll
#define endl '\\n'
#define all(s) s.begin(), s.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 4e5 + 5;
const int MAX = 250000 + 5;
const int M = 3e5;
const int mod = 998244353;
const int MOD = 998244353;
const int INF = 1e18;
const int LG = 21;

vector<int> v;
int cache[N];

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int f(int no) {
  if (no == 0)
    return 0;
  int ret = 0;
  for (int i = 0; i < v.size(); i++) {
    ret += v[i] * pow(2, v.size() - i - 1, no);
    ret %= no;
  }
  return ret;
}

int dp(int no) {
  if (no == 0) {
    return 0;
  }
  if (cache[no] != -1) {
    return cache[no];
  }
  int tot = __builtin_popcount(no);
  return cache[no] = 1 + dp(no % tot);
}

void solve() {
  int n;
  cin >> n;
  int cnt = 0;
  memset(cache, -1, sizeof(cache));
  int no = 0;
  for (int i = 0; i < n; i++) {
    char ch;
    cin >> ch;
    v.push_back(ch - '0');
    cnt += v.back();
    if (ch == '0')
      continue;
  }
  int plus = cnt + 1;
  int minus = max(cnt - 1, 0LL);
  int plusans = f(plus);
  int minusans = f(minus);
  for (int i = 0; i < n; i++) {
    if (v[i] == 0) {
      int curr = plusans + pow(2, n - i - 1, plus) + plus;
      curr %= plus;
      cout << 1 + dp(curr) << endl;
    } else {
      if (minus == 0LL) {
        cout << 0 << endl;
        continue;
      }
      int curr = minusans - pow(2, n - i - 1, minus) + minus;
      curr %= minus;
      cout << 1 + dp(curr) << endl;
    }
  }
}

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define hii cout << "hii" << endl
#define int ll
#define endl '\\n'
#define all(s) s.begin(), s.end()

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \\
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

const int N = 4e5 + 5;
const int MAX = 250000 + 5;
const int M = 3e5;
const int mod = 998244353;
const int MOD = 998244353;
const int INF = 1e18;
const int LG = 21;

vector<int> v;
int cache[N];

int pow(int a, int b, int m) {
  int ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}

int f(int no) {
  int ret = 0;
  for (int i = 0; i < v.size(); i++) {
    ret += v[i] * pow(2, v.size() - i - 1, no);
    ret %= no;
  }
  return ret;
}

int dp(int no) {
  if (no == 0) {
    return 0;
  }
  if (cache[no] != -1) {
    return cache[no];
  }
  int tot = __builtin_popcount(no);
  return cache[no] = 1 + dp(no % tot);
}

void solve() {
  int n;
  cin >> n;
  int cnt = 0;
  memset(cache, -1, sizeof(cache));
  int no = 0;
  for (int i = 0; i < n; i++) {
    char ch;
    cin >> ch;
    v.push_back(ch - '0');
    cnt += v.back();
    if (ch == '0')
      continue;
    // no += pow(2, n - i - 1);
  }
  // trace(no);
  int plus = cnt + 1;
  int minus = max(cnt - 1, 0LL);
  int plusans = f(plus);
  int minusans = f(minus);
  for (int i = 0; i < n; i++) {
    if (v[i] == 0) {
      int curr = plusans + pow(2, n - i - 1, plus) + plus;
      curr %= plus;
      // no += pow(2, n - i - 1);
      cout << 1 + dp(curr) << endl;
    } else {
      int curr = minusans - pow(2, n - i - 1, minus) + minus;
      curr %= minus;
      cout << 1 + dp(curr) << endl;
    }
  }
}

int32_t main() {

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}