alexjercan/bugnet huggingface dataset:
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

template <typename T>
int pow(int x, T n, int mod = std::numeric_limits<int>::max()) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<int64_t>(res) * static_cast<int64_t>(x) % mod;
    x = static_cast<int64_t>(x) * static_cast<int64_t>(x) % mod;
    n >>= 1;
  }
  return res;
}

template <typename T>
int64_t pow(int64_t x, T n, int64_t mod = std::numeric_limits<int64_t>::max()) {
  int64_t res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<__int128>(res) * static_cast<__int128>(x) % mod;
    x = static_cast<__int128>(x) * static_cast<__int128>(x) % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string S;
  cin >> S;
  reverse(S.begin(), S.end());
  int b = 0;
  for (auto s : S) {
    if (s == '1')
      b++;
  }
  int x = 0, y = 0;
  for (int i = 0; i < N; i++) {
    if (b - 1 > 0) {
      x += (S[i] - '0') * pow(2, i, b - 1);
      x %= b - 1;
    }
    y += (S[i] - '0') * pow(2, i, b + 1);
    y %= b + 1;
  }
  function<int(int)> rec = [&](int x) {
    if (x == 0)
      return 0;
    return rec(x % __builtin_popcount(x)) + 1;
  };
  for (int i = N - 1; i > -1; i--) {
    if (S[i] == '1') {
      if (b == 1) {
        cout << 0 << '\\n';
      } else {
        cout << rec((x - pow(2, i, b - 1) + (b - 1)) % (b - 1)) + 1 << '\\n';
      }
    } else {
      cout << rec((y + pow(2, i, b + 1)) % (b + 1)) + 1 << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

template <typename T>
int pow(int x, T n, int mod = std::numeric_limits<int>::max()) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<int64_t>(res) * static_cast<int64_t>(x) % mod;
    x = static_cast<int64_t>(x) * static_cast<int64_t>(x) % mod;
    n >>= 1;
  }
  return res;
}

template <typename T>
int64_t pow(int64_t x, T n, int64_t mod = std::numeric_limits<int64_t>::max()) {
  int64_t res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<__int128>(res) * static_cast<__int128>(x) % mod;
    x = static_cast<__int128>(x) * static_cast<__int128>(x) % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  string S;
  cin >> N >> S;
  const int M = 23;
  reverse(S.begin(), S.end());
  for (int i = N - 1; i > -1; i--) {
    if (S[i] == '0') {
      S[i] = '1';
    } else {
      S[i] = '0';
    }

    int X = 0;
    for (int k = 0; k < min(M, (int)S.size()); k++) {
      if (k < M) {
        X += (S[k] - '0') * pow(2, k);
      }
    }

    int cnt = 0;
    for (auto s : S) {
      if (s == '1')
        cnt++;
    }

    int res = 0;
    if (X >= cnt)
      res = 1;
    X %= cnt;

    function<int(int)> rec = [&](int x) {
      if (x == 0) {
        return 0;
      }
      return rec(x % __builtin_popcount(x)) + 1;
    };

    // cout << X << ' ' << cnt << '\\n';
    cout << rec(X) + res << '\\n';

    if (S[i] == '0') {
      S[i] = '1';
    } else {
      S[i] = '0';
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

template <typename T>
int pow(int x, T n, int mod = std::numeric_limits<int>::max()) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<int64_t>(res) * static_cast<int64_t>(x) % mod;
    x = static_cast<int64_t>(x) * static_cast<int64_t>(x) % mod;
    n >>= 1;
  }
  return res;
}

template <typename T>
int64_t pow(int64_t x, T n, int64_t mod = std::numeric_limits<int64_t>::max()) {
  int64_t res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<__int128>(res) * static_cast<__int128>(x) % mod;
    x = static_cast<__int128>(x) * static_cast<__int128>(x) % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string S;
  cin >> S;
  int c = 0;
  for (auto s : S) {
    if (s == '1')
      c++;
  }
  int x = 0, y = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1')
      x += pow(2, N - i - 1, c + 1), x %= c + 1;
    if (S[i] == '1' && c - 1 > 0)
      y += pow(2, N - i - 1, c - 1), y %= c - 1;
  }
  function<int(int)> rec = [&](int x) {
    if (x == 0)
      return 0;
    return rec(x % __builtin_popcount(x)) + 1;
  };
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      if (c - 1 == 0) {
        cout << 0 << '\\n';
      } else {
        cout << rec((y - pow(2, N - i - 1, c - 1) + (c - 1)) % (c - 1)) + 1
             << '\\n';
      }
    } else {
      cout << rec((x + pow(2, N - i - 1, c + 1)) % (c + 1)) + 1 << '\\n';
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

template <typename T>
int pow(int x, T n, int mod = std::numeric_limits<int>::max()) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<int64_t>(res) * static_cast<int64_t>(x) % mod;
    x = static_cast<int64_t>(x) * static_cast<int64_t>(x) % mod;
    n >>= 1;
  }
  return res;
}

template <typename T>
int64_t pow(int64_t x, T n, int64_t mod = std::numeric_limits<int64_t>::max()) {
  int64_t res = 1;
  while (n > 0) {
    if (n & 1)
      res = static_cast<__int128>(res) * static_cast<__int128>(x) % mod;
    x = static_cast<__int128>(x) * static_cast<__int128>(x) % mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string S;
  cin >> S;
  int c = 0;
  for (auto s : S) {
    if (s == '1')
      c++;
  }
  int x = 0, y = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1')
      x += pow(2, N - i - 1, c + 1);
    if (S[i] == '1' && c - 1 > 0)
      y += pow(2, N - i - 1, c - 1);
  }
  function<int(int)> rec = [&](int x) {
    if (x == 0)
      return 0;
    return rec(x % __builtin_popcount(x)) + 1;
  };
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      if (c - 1 == 0) {
        cout << 0 << '\\n';
      } else {
        cout << rec((y - pow(2, N - i - 1, c - 1) + (c - 1)) % (c - 1)) + 1
             << '\\n';
      }
    } else {
      cout << rec((x + pow(2, N - i - 1, c + 1)) % (c + 1)) + 1 << '\\n';
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll fastpower(ll x, ll y, ll MOD) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % MOD;
    y >>= 1;
    x = (ll)x * x % MOD;
  }
  return res;
}

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

  ll n, ans1 = 0, ans2 = 0, cnt = 0;
  cin >> n;
  string str;
  cin >> str;
  for (char ch : str)
    if (ch == '1')
      cnt++;
  for (ll i = 0; i < n; i++) {
    if (str[i] == '1') {
      ans1 = (ans1 + fastpower(2, n - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        ans2 = (ans2 + fastpower(2, n - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  for (ll i = 0; i < n; i++) {
    if (str[i] == '0') {
      ll tmp = (ans1 + fastpower(2, n - i - 1, cnt + 1)) % (cnt + 1), c = 1;
      while (tmp > 0) {
        tmp = tmp % (__builtin_popcount(tmp));
        c++;
      }
      cout << c << endl;
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
      ll tmp =
             (ans2 + (cnt - 1) - fastpower(2, n - i - 1, cnt - 1)) % (cnt - 1),
         c = 1;
      while (tmp > 0) {
        tmp = tmp % (__builtin_popcount(tmp));
        c++;
      }
      cout << c << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll fastpower(ll x, ll y, ll MOD) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = res * x % MOD;
    y >>= 1;
    x = (ll)x * x % MOD;
  }
  return res;
}

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

  ll n, ans1 = 0, ans2 = 0, cnt = 0;
  cin >> n;
  string str;
  cin >> str;
  for (char ch : str)
    if (ch == '1')
      cnt++;
  for (ll i = 0; i < n; i++) {
    if (str[i] == '1') {
      ans1 = (ans1 + fastpower(2, n - i - 1, cnt + 1)) % (cnt + 1);
      ans2 = (ans2 + fastpower(2, n - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  for (ll i = 0; i < n; i++) {
    if (str[i] == '0') {
      ll tmp = (ans1 + fastpower(2, n - i - 1, cnt + 1)) % (cnt + 1), c = 1;
      while (tmp > 0) {
        tmp = tmp % (__builtin_popcount(tmp));
        c++;
      }
      cout << c << endl;
    } else {
      ll tmp =
             (ans2 + (cnt - 1) - fastpower(2, n - i - 1, cnt - 1)) % (cnt - 1),
         c = 1;
      while (tmp > 0) {
        tmp = tmp % (__builtin_popcount(tmp));
        c++;
      }
      cout << c << endl;
    }
  }

  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error


// D - Anything Goes to Zero

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

ll rem[2][200000];
ll rem_sum[2];

// aのn乗をmodで割った余りを計算
ll mod_pow(ll a, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1)
      result = result * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return result;
}

int popcount(int n) {
  int result = 0;
  while (n) {
    if (n % 2 == 1)
      result++;
    n /= 2;
  }
  return result;
}

ll f(int n, int k) {
  if (n == 0)
    return k;

  return f(n % popcount(n), k + 1);
}

int main() {
  int N;
  string X;
  cin >> N >> X;

  int pop_cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      pop_cnt++;
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      rem_sum[0] += mod_pow(2, N - i - 1, pop_cnt + 1);
      if (pop_cnt != 1)
        rem_sum[1] += mod_pow(2, N - i - 1, pop_cnt - 1);
    }
  }

  vector<ll> ans_list;
  ans_list.reserve(N);

  for (int i = 0; i < N; i++) {
    ll r;

    if (X[i] == '0') {
      r = (rem_sum[0] + mod_pow(2, N - i - 1, pop_cnt + 1)) % (pop_cnt + 1);
    } else {
      if (pop_cnt == 1) {
        ans_list.push_back(0);
        continue;
      }

      r = (rem_sum[1] - mod_pow(2, N - i - 1, pop_cnt - 1)) % (pop_cnt - 1);
    }

    ans_list.push_back(f(r, 1));
  }

  for (ll ans : ans_list) {
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:


// D - Anything Goes to Zero

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

ll rem[2][200000];
ll rem_sum[2];

// aのn乗をmodで割った余りを計算
ll mod_pow(ll a, ll n, ll mod) {
  ll result = 1;
  while (n > 0) {
    if (n & 1)
      result = result * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return result;
}

int popcount(int n) {
  int result = 0;
  while (n) {
    if (n % 2 == 1)
      result++;
    n /= 2;
  }
  return result;
}

ll f(int n, int k) {
  if (n == 0)
    return k;

  return f(n % popcount(n), k + 1);
}

int main() {
  int N;
  string X;
  cin >> N >> X;

  int pop_cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      pop_cnt++;
  }

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      rem_sum[0] += mod_pow(2, N - i - 1, pop_cnt + 1);
      rem_sum[1] += mod_pow(2, N - i - 1, pop_cnt - 1);
    }
  }

  vector<ll> ans_list;
  ans_list.reserve(N);

  for (int i = 0; i < N; i++) {
    ll r;

    if (X[i] == '0') {
      r = (rem_sum[0] + mod_pow(2, N - i - 1, pop_cnt + 1)) % (pop_cnt + 1);
    } else {
      if (pop_cnt == 1) {
        ans_list.push_back(0);
        continue;
      }

      r = (rem_sum[1] - mod_pow(2, N - i - 1, pop_cnt - 1)) % (pop_cnt - 1);
    }

    ans_list.push_back(f(r, 1));
  }

  for (ll ans : ans_list) {
    cout << ans << endl;
  }

  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

long modpow(long a, long n, long m) {
  long r = 1;
  while (n) {
    if (n & 1)
      r = r * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int orig = 0;
  for (int i = 0; i < x.size(); i++)
    if (x[i] == '1')
      orig++;
  long upmod = 0, downmod = 0, twou = 1, twod = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      upmod = (upmod + twou) % (orig + 1);
      if (orig > 1)
        downmod = (downmod + twod) % (orig - 1);
    }
    twou = twou * 2 % (orig + 1);
    if (orig > 1)
      twod = twod * 2 % (orig - 1);
  }

  vector<int> popnum(orig + 22, 0);
  for (int i = 1; i <= orig + 10; i++) {
    int cnt = 0;
    for (int b = 1; b <= i; b <<= 1)
      if (i & b)
        cnt++;
    popnum[i] = popnum[i % cnt] + 1;
  }

  long twos = 1;
  for (long i = 0; i < n; i++) {
    long ans = 0;
    if (x[i] == '1') {
      if (orig == 1)
        ans = 0;
      else
        ans = popnum[(downmod - modpow(2, n - 1 - i, orig - 1) + orig - 1) %
                     (orig - 1)] +
              1;
    } else {
      ans = popnum[(upmod + modpow(2, n - 1 - i, orig + 1) + orig + 1) %
                   (orig + 1)] +
            1;
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long modpow(long a, long n, long m) {
  long r = 1;
  while (n) {
    if (n & 1)
      r = r * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int orig = 0;
  for (int i = 0; i < x.size(); i++)
    if (x[i] == '1')
      orig++;
  long upmod = 0, downmod = 0, twou = 1, twod = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      upmod = (upmod + twou) % (orig + 1);
      downmod = (downmod + twod);
      if (orig >= 2)
        downmod %= (orig - 1);
    }
    twou = twou * 2 % (orig + 1);
    twod = twod * 2;
    if (orig >= 2)
      twod %= (orig - 1);
  }

  vector<int> popnum(orig + 22, 0);
  for (int i = 1; i <= orig + 10; i++) {
    int cnt = 0;
    for (int b = 1; b <= i; b <<= 1)
      if (i & b)
        cnt++;
    popnum[i] = popnum[i % cnt] + 1;
  }

  long twos = 1;
  for (long i = 0; i < n; i++) {
    long ans = 0;
    if (x[i] == '1') {
      ans = popnum[(downmod - modpow(2, n - 1 - i, orig - 1) + orig - 1) %
                   (orig - 1)] +
            1;
    } else {
      ans = popnum[(upmod + modpow(2, n - 1 - i, orig + 1) + orig + 1) %
                   (orig + 1)] +
            1;
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int popcount(int n) {
  int r = 0;
  while (n > 0) {
    r += n & 1;
    n >>= 1;
  }
  return r;
}
int popcount(string n) {
  int r = 0;
  for (auto c : n) {
    r += c == '1';
  }
  return r;
}
int f(int n) {
  int k = 0;
  while (n != 0) {
    n %= popcount(n);
    k++;
  }
  return k;
}
int pow_mod(int a, int b, int mod) {
  if (b == 0)
    return 1 % mod;
  ll k = pow_mod(a, b / 2, mod);
  k *= k;
  k %= mod;
  if (b % 2 == 0)
    return k;
  return (k * a) % mod;
}
int main() {
  int N;
  string X;
  cin >> N >> X;
  reverse(X.begin(), X.end());
  int s = popcount(X);
  int A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      A += pow_mod(2, i, s + 1);
      A %= s + 1;
      if (s - 1 > 0) {
        B += pow_mod(2, i, s - 1);
        B %= s - 1;
      }
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      if (s - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      int b = B;
      b -= pow_mod(2, i, s - 1);
      b += s - 1;
      b %= s - 1;
      cout << 1 + f(b) << endl;
    } else {
      int a = A;
      a += pow_mod(2, i, s + 1);
      a %= s + 1;
      cout << 1 + f(a) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int popcount(int n) {
  int r = 0;
  while (n > 0) {
    r += n & 1;
    n >>= 1;
  }
  return r;
}
int popcount(string n) {
  int r = 0;
  for (auto c : n) {
    r += c == '1';
  }
  return r;
}
int f(int n) {
  int k = 0;
  while (n != 0) {
    n %= popcount(n);
    k++;
  }
  return k;
}
int pow_mod(int a, int b, int mod) {
  if (b == 0)
    return 1 % mod;
  int k = pow_mod(a, b / 2, mod);
  k *= k;
  k %= mod;
  if (b % 2 == 0)
    return k;
  return (k * a) % mod;
}
int main() {
  int N;
  string X;
  cin >> N >> X;
  reverse(X.begin(), X.end());
  int s = popcount(X);
  int A = 0, B = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      A += pow_mod(2, i, s + 1);
      A %= s + 1;
      if (s - 1 > 0) {
        B += pow_mod(2, i, s - 1);
        B %= s - 1;
      }
    }
  }
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      if (s - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      int b = B;
      b -= pow_mod(2, i, s - 1);
      b += s - 1;
      b %= s - 1;
      cout << 1 + f(b) << endl;
    } else {
      int a = A;
      a += pow_mod(2, i, s + 1);
      a %= s + 1;
      cout << 1 + f(a) << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using vi = vector<int>;
using vv = vector<vi>;

int main() {
  int n;
  string x;
  cin >> n >> x;

  int popx = 0;
  rep(i, n) if (x[i] == '1') popx++;

  if (popx == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  vi popn(2, 0);

  popn[0] = popx + 1;
  popn[1] = popx - 1;

  vv modpop(2, vi(n, 0));

  modpop[0][n - 1] = 1 % popn[0];
  if (popn[1] != 0)
    modpop[1][n - 1] = 1 % popn[1];

  for (int i = n - 2; i >= 0; i--) {
    modpop[0][i] = 2 * modpop[0][i + 1] % popn[0];
    if (popn[1] != 0)
      modpop[1][i] = 2 * modpop[1][i + 1] % popn[1];
  }

  vi count(2, 0);

  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      count[0] += modpop[0][i];
      count[1] += modpop[1][i];
      count[0] %= popn[0];
      if (popn[1] != 0)
        count[1] %= popn[1];
    }
  }

  for (int j = 0; j < n; j++) {
    int c = 0;
    if (x[j] == '1')
      c = 1;

    if (popn[1] == 0 && c == 1) {
      cout << 0 << endl;
      continue;
    }

    int num = (count[c] + popn[c] + (1 - 2 * c) * modpop[c][j]) % popn[c];
    int ans = 1;

    while (num) {
      int p = 0;
      int b = num;
      while (b) {
        p += (b & 1);
        b /= 2;
      }
      num %= p;
      ans++;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using vi = vector<int>;
using vv = vector<vi>;

int main() {
  int n;
  string x;
  cin >> n >> x;

  int popx = 0;
  rep(i, n) if (x[i] == '1') popx++;

  if (popx == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  vi popn(2);
  popn[0] = popx + 1;
  popn[1] = popx - 1;

  vv modpop(2, vi(n, 0));

  modpop[0][n - 1] = 1 % popn[0];
  if (popn[1] != 0)
    modpop[1][n - 1] = 1 % popn[1];

  for (int i = n - 2; i >= 0; i--) {
    modpop[0][i] = 2 * modpop[0][i + 1] % popn[0];
    if (popn[1] != 0)
      modpop[1][i] = 2 * modpop[1][i + 1] % popn[1];
  }

  vi count(2, 0);

  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      count[0] += modpop[0][i];
      count[1] += modpop[1][i];
      count[0] %= popn[0];
      count[1] %= popn[1];
    }
  }

  for (int j = 0; j < n; j++) {
    int ans = 1;
    int c = 0;
    if (x[j] == '1')
      c = 1;

    if (popn[1] == 0 && c == 1) {
      cout << 0 << endl;
      continue;
    }

    int nx = (count[c] + popn[c] + (1 - 2 * c) * modpop[c][j]) % popn[c];
    if (nx == 0) {
      cout << 1 << endl;
      continue;
    }

    int num = nx;
    while (num) {
      int p = 0;
      int b = num;
      while (b) {
        p += (b & 1);
        b /= 2;
      }
      num %= p;
      ans++;
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long int

using namespace std;

ll solve(ll x, map<ll, ll> &memo) {
  if (x == 0LL)
    return 0LL;
  if (memo.find(x) != memo.end()) {
    return memo.at(x);
  }
  ll p = 0;
  for (ll tmp = x; tmp > 0; tmp = tmp >> 1) {
    if (tmp & 1LL != 0LL) {
      ++p;
    }
  }
  if (p == 1) {
    memo[x] = 1;
  } else {
    if (x % p == 0) {
      memo[x] = 1;
    } else {
      ll ret = solve(x % p, memo);
      memo[x] = ret + 1;
    }
  }
  return memo[x];
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;

  ll p = 0;
  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1')
      ++p;
  }
  vector<ll> modUp(n);
  vector<ll> modDown(n);

  ll pUp = p + 1;
  ll pDown = p - 1;
  ll fallUp = 0;
  ll fallDown = 0;
  ll curUp = 1 % pUp;
  ll curDown = 1;
  if (pDown == 0) {
    curDown = 0;
  }
  for (ll i = n - 1; i >= 0; --i) {
    modUp[i] = curUp;
    modDown[i] = curDown;
    if (s[i] == '1') {
      fallUp += curUp;
      fallDown += curDown;
      fallUp %= pUp;
      if (pDown != 0)
        fallDown %= pDown;
    }
    curUp *= 2;
    curUp %= pUp;
    curDown *= 2;
    if (pDown != 0)
      curDown %= pDown;
  }

  map<ll, ll> memo;
  memo[0] = 0LL;
  for (ll i = 0; i < n; ++i) {
    ll x;
    if (s[i] == '1') {
      if (pDown == 0) {
        x = 0;
        cout << 0 << endl;
        continue;
      } else {
        x = fallDown - modDown[i];
        x = (x + pDown) % pDown;
      }
    } else {
      x = fallUp + modUp[i];
      x = (x + pUp) % pUp;
    }

    ll ret = solve(x, memo);
    cout << ret + 1 << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ll long long int

using namespace std;

ll solve(ll x, map<ll, ll> &memo) {
  if (memo.find(x) != memo.end()) {
    return memo.at(x);
  }
  ll p = 0;
  for (ll tmp = x; tmp > 0; tmp = tmp >> 1) {
    if (tmp & 1LL == 1LL) {
      ++p;
    }
  }
  if (x % p == 0) {
    memo[x] = 1;
  } else {
    ll ret = solve(x % p, memo);
    memo[x] = ret + 1;
  }
  return memo[x];
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<ll> X(n);

  ll p = 0;
  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1')
      ++p;
  }
  vector<ll> modUp(n);
  vector<ll> modDown(n);

  ll pUp = p + 1;
  ll pDown = p - 1;
  ll fallUp = 0;
  ll fallDown = 0;
  ll curUp = 1 % pUp;
  ll curDown = 1 % pDown;
  for (ll i = n - 1; i >= 0; --i) {
    modUp[i] = curUp;
    modDown[i] = curDown;
    if (s[i] == '1') {
      fallUp += curUp;
      fallDown += curDown;
      fallUp %= pUp;
      fallDown %= pDown;
    }
    curUp *= 2;
    curUp %= pUp;
    curDown *= 2;
    curDown %= pDown;
  }
  for (ll i = 0; i < n; ++i) {
    if (s[i] == '1') {
      X[i] = fallDown - modDown[i];
      X[i] = (X[i] + pDown) % pDown;
    } else {
      X[i] = fallUp + modUp[i];
      X[i] = (X[i] + pUp) % pUp;
    }
  }

  map<ll, ll> memo;
  memo[0] = 0LL;

  for (ll i = 0; i < n; ++i) {
    ll ret = solve(X[i], memo);
    cout << ret + 1 << endl;
  }
}