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

#include <bits/stdc++.h>

using namespace std;

// ゼロになる時が例外

int main() {
  int n;
  cin >> n;
  vector<int> A;
  string S;
  cin >> S;
  for (const auto &s : S)
    A.push_back(s - '0');
  int cnt = 0;
  for (const auto &a : A) {
    if (a == 1)
      cnt++;
  }

  vector<int> D(n);
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1)
        cout << 0 << endl;
      else if (i == n - 1)
        cout << 2 << endl;
      else
        cout << 1 << endl;
    }
    return 0;
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }

  vector<vector<int>> B(3, vector<int>(n));
  for (int j = -1; j < 2; j++) {
    int m = 1;
    for (int i = 0; i < n; i++) {
      B[j + 1][n - i - 1] = m;
      m *= 2;
      m %= (cnt + j);
    }
  }

  vector<int> C(3);
  for (int j = 0; j <= 2; j++) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1) {
        C[j] += B[j][i];
        C[j] %= (cnt + j - 1);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (A[i] == 1)
      D[i] = (C[0] - B[0][i] + cnt - 1) % (cnt - 1);
    else
      D[i] = (C[2] + B[2][i]) % (cnt + 1);
  }

  map<int, int> ans;
  auto f = [&](int d, auto self) -> int {
    if (ans.count(d))
      return ans[d];
    if (d == 0)
      return 0;
    return ans[d] = self(d % __builtin_popcount(d), self) + 1;
  };

  for (int i = 0; i < n; i++) {
    f(D[i], f);
  }

  for (int i = 0; i < n; i++)
    cout << ans[D[i]] + 1 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// ゼロになる時が例外

int main() {
  int n;
  cin >> n;
  vector<int> A;
  string S;
  cin >> S;
  for (const auto &s : S)
    A.push_back(s - '0');
  int cnt = 0;
  for (const auto &a : A) {
    if (a == 1)
      cnt++;
  }

  vector<int> D(n);
  if (cnt == 1) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1)
        cout << 0 << endl;
      else if (i == n - 1)
        cout << 2 << endl;
      else
        cout << 1 << endl;
    }
    return 0;
  }

  vector<vector<int>> B(3, vector<int>(n));
  for (int j = -1; j < 2; j++) {
    int m = 1;
    for (int i = 0; i < n; i++) {
      B[j + 1][n - i - 1] = m;
      m *= 2;
      m %= (cnt + j);
    }
  }

  vector<int> C(3);
  for (int j = 0; j <= 2; j++) {
    for (int i = 0; i < n; i++) {
      if (A[i] == 1) {
        C[j] += B[j][i];
        C[j] %= (cnt + j - 1);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    if (A[i] == 1)
      D[i] = (C[0] - B[0][i] + cnt - 1) % (cnt - 1);
    else
      D[i] = (C[2] + B[2][i]) % (cnt + 1);
  }

  map<int, int> ans;
  auto f = [&](int d, auto self) -> int {
    if (ans.count(d))
      return ans[d];
    if (d == 0)
      return 0;
    return ans[d] = self(d % __builtin_popcount(d), self) + 1;
  };

  for (int i = 0; i < n; i++) {
    f(D[i], f);
  }

  for (int i = 0; i < n; i++)
    cout << ans[D[i]] + 1 << endl;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using ll = long long;

ll modpow(ll m, ll n, ll mod) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modpow(m * m % mod, n / 2, mod);
  else {
    return modpow(m, n - 1, mod) * m % mod;
  }
}

signed main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  ll num = 0, now = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num++;
  }
  ll plus = 0, minus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (num == 1)
        continue;
      (plus += modpow(2, n - i - 1, num + 1)) %= (num + 1);
      (minus += modpow(2, n - i - 1, num - 1)) %= (num - 1);
    }
  }
  std::vector<ll> data(n);
  std::vector<ll> ans(n, 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (num == 1)
        data[i] = 0, ans[i] = 0;
      else {
        data[i] = minus - modpow(2, n - i - 1, num - 1) + num - 1;
        data[i] %= (num - 1);
      }
    }
    if (s[i] == '0') {
      data[i] = plus + modpow(2, n - i - 1, num + 1);
      data[i] %= (num + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    ll ans1 = 0;
    while (data[i] > 0) {
      int p = __builtin_popcount(data[i]);
      data[i] %= p;
      ans1++;
    }
    ans[i] += ans1;
  }
  for (int i = 0; i < n; i++) {
    std::cout << ans[i] << '\\n';
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;

ll modpow(ll m, ll n, ll mod) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return modpow(m * m % mod, n / 2, mod);
  else {
    return modpow(m, n - 1, mod) * m % mod;
  }
}

signed main() {
  int n;
  std::cin >> n;
  std::string s;
  std::cin >> s;
  ll num = 0, now = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      num++;
  }
  ll plus = 0, minus = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      (plus += modpow(2, n - i - 1, num + 1)) %= (num + 1);
      (minus += modpow(2, n - i - 1, num - 1)) %= (num - 1);
    }
  }
  std::vector<ll> data(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      data[i] = minus - modpow(2, n - i - 1, num - 1) + num - 1;
      data[i] %= (num - 1);
    } else {
      data[i] = plus + modpow(2, n - i - 1, num + 1);
      data[i] %= (num + 1);
    }
  }
  std::vector<ll> ans(n, 1);
  for (int i = 0; i < n; i++) {
    ll ans1 = 0;
    while (data[i] > 0) {
      int p = __builtin_popcount(data[i]);
      data[i] %= p;
      ans1++;
    }
    ans[i] += ans1;
  }
  for (int i = 0; i < n; i++) {
    std::cout << ans[i] << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int main() {
  string s;
  ll i, n, a = 0, b = 0, x = 0;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      x++;
    }
  }
  reverse(s.begin(), s.end());
  for (i = 0; i < n; i++) {
    if (x - 1 == 0) {
      if (i == 0) {
        b += 1 % (x + 1);
      }
      b = b + modpow(2, i, x + 1);
      continue;
    }
    if (s[i] == '1') {
      if (i == 0) {
        a += 1 % (x - 1);
        b += 1 % (x + 1);
        continue;
      }
      a = a + modpow(2, i, x - 1);
      b = b + modpow(2, i, x + 1);
    }
  }
  if (x - 1 == 0) {
    b = b % (x + 1);
  } else {
    a = a % (x - 1), b = b % (x + 1);
  }
  reverse(s.begin(), s.end());
  for (i = 0; i < n; i++) {
    ll c, ans = 1;
    if (s[i] == '1') {
      if (x - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      c = a - modpow(2, n - i - 1, x - 1);
      if (c < 0) {
        c += (x - 1);
      }
    } else {
      c = b + modpow(2, n - i - 1, x + 1);
      c = c % (x + 1);
    }
    while (c != 0) {
      ans++;
      c = c % __builtin_popcount(c);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int main() {
  string s;
  ll i, n, a = 0, b = 0, x = 0;
  cin >> n >> s;
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      x++;
    }
  }
  reverse(s.begin(), s.end());
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (i == 0) {
        a += 1 % (x - 1);
        b += 1 % (x + 1);
        continue;
      }
      a = a + modpow(2, i, x - 1);
      b = b + modpow(2, i, x + 1);
    }
  }
  a = a % (x - 1), b = b % (x + 1);
  reverse(s.begin(), s.end());
  for (i = 0; i < n; i++) {
    ll c, ans = 1;
    if (s[i] == '1') {
      if (x - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      c = a - modpow(2, n - i - 1, x - 1);
      if (c < 0) {
        c += (x - 1);
      }
    } else {
      c = b + modpow(2, n - i - 1, x + 1);
      c = c % (x + 1);
    }
    while (c != 0) {
      ans++;
      c = c % __builtin_popcount(c);
    }
    cout << ans << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

// Copyright 2020 yaito3014
#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::string s;
  std::cin >> s;
  std::reverse(begin(s), end(s));
  int p = std::count(cbegin(s), cend(s), '1');
  std::vector<int> m1(N, 1), m2(N, 1);
  for (int i = 0; i < N - 1; ++i) {
    if (p > 1)
      m1[i + 1] = m1[i] * 2 % (p - 1);
    m2[i + 1] = m2[i] * 2 % (p + 1);
  }
  int b1 = 0, b2 = 0;
  for (int i = 0; i < N; ++i) {
    if (s[i] != '1')
      continue;
    if (p > 1)
      b1 = (b1 + m1[i]) % (p - 1);
    b2 = (b2 + m2[i]) % (p + 1);
  }
  std::vector<int> table(N + 1, 0);
  for (int i = 1, x = i; i <= N; x = ++i)
    do
      ++table[i];
    while (x %= std::bitset<32>(x).count());

  for (int i = 0; i < N; ++i) {
    int ans = s[N - i - 1] == '1'
                  ? p > 1 ? table[(b1 - m1[N - i - 1] + p - 1) % (p - 1)] : -1
                  : table[(b2 + m2[N - i - 1] + p + 1) % (p + 1)];
    std::cout << ++ans << '\\n';
  }
}


Error: TLE

Fixed submission:

// Copyright 2020 yaito3014
#include <boost/dynamic_bitset.hpp>
#include <iostream>

int main() {
  int N;
  std::cin >> N;
  std::string s;
  std::cin >> s;
  using bs_t = boost::dynamic_bitset<>;
  const bs_t zero(N, 0ul);
  bs_t a(5, 2), b(5, 1);
  bs_t bs(s);
  for (int i = 0; i < N; ++i) {
    bs_t b = bs;
    b.flip(N - i - 1);
    int ans = 0;
    while (b > zero) {
      bs_t c(N, b.count());
      while (b >= c) {
        bs_t ca = c;
        while (ca != zero) {
          bs_t borrow = (~b) & ca;
          b ^= ca;
          ca = borrow << 1;
        }
      }
      ++ans;
    }
    std::cout << ans << '\\n';
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

#define pb push_back
#define sz size()
#define ll long long

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  string s;
  cin >> n;
  cin >> s;

  n = s.size();

  int i;
  int nuli = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      nuli++;
    }

  int n1 = nuli - 1;
  int n2 = nuli + 1;

  ll presm1 = 0;
  ll presm2 = 0;

  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      if (n1 > 0)
        presm1 += binpow(2, n - i - 1, n1);
      presm2 += binpow(2, n - i - 1, n2);
    }

  if (n1 > 0)
    presm1 %= n1;
  if (n2 > 0)
    presm2 %= n2;

  int powBuck[200001];
  memset(powBuck, -1, sizeof(powBuck));

  for (i = 0; i < n; i++) {

    int curr = 0;

    if (s[i] == '1') {

      if (nuli == 1) {
        cout << "0\\n";
        continue;
      }
      if (n1 > 0) {
        curr = presm1;
        ll kolku = binpow(2, n - i - 1, n1);
        curr = (curr + n1 - kolku) % n1;
      }
    } else {
      curr = presm2;
      ll kolku = binpow(2, n - i - 1, n2);
      curr = (curr + kolku) % n2;
    }

    int br = 1;
    while (curr) {

      if (powBuck[curr] == -1)
        powBuck[curr] = __builtin_popcount(curr);

      curr = curr % powBuck[curr];
      br++;
    }
    cout << br << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define pb push_back
#define sz size()
#define ll long long

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

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  string s;
  cin >> n;
  cin >> s;

  n = s.size();

  int i;
  int nuli = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      nuli++;
    }

  int n1 = nuli - 1;
  int n2 = nuli + 1;

  ll presm1 = 0;
  ll presm2 = 0;

  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      if (n1 > 0)
        presm1 += binpow(2, n - i - 1, n1);
      presm2 += binpow(2, n - i - 1, n2);
    }

  if (n1 > 0)
    presm1 %= n1;
  if (n2 > 0)
    presm2 %= n2;

  int powBuck[20001];
  memset(powBuck, -1, sizeof(powBuck));

  for (i = 0; i < n; i++) {

    int curr = 0;

    if (s[i] == '1') {

      if (nuli == 1) {
        cout << "0\\n";
        continue;
      }
      if (n1 > 0) {
        curr = presm1;
        ll kolku = binpow(2, n - i - 1, n1);
        curr = (curr + n1 - kolku) % n1;
      }
    } else {
      curr = presm2;
      ll kolku = binpow(2, n - i - 1, n2);
      curr = (curr + kolku) % n2;
    }

    int br = 1;
    while (curr) {

      if (powBuck[curr] == -1)
        powBuck[curr] = __builtin_popcount(curr);

      curr = curr % powBuck[curr];
      br++;
    }
    cout << br << "\\n";
  }

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

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

#define pb push_back
#define sz size()
#define ll long long

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

int main() {

  int n;
  string s;
  cin >> n;
  cin >> s;

  n = s.size();

  int i;
  int nuli = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      nuli++;
    }

  int n1 = nuli - 1;
  int n2 = nuli + 1;

  ll presm1 = 0;
  ll presm2 = 0;

  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      if (n1 > 0)
        presm1 += binpow(2, n - i - 1, n1);
      presm2 += binpow(2, n - i - 1, n2);
    }

  if (n1 > 0)
    presm1 %= n1;
  if (n2 > 0)
    presm2 %= n2;

  for (i = 0; i < n; i++) {

    int curr = 0;

    if (s[i] == '1') {
      if (n1 > 0) {
        curr = presm1;
        ll kolku = binpow(2, n - i - 1, n1);
        curr = (curr + n1 - kolku) % n1;
      }

      if (nuli == 1) {
        cout << "0\\n";
        continue;
      }

    } else {
      curr = presm2;
      ll kolku = binpow(2, n - i - 1, n2);
      curr = (curr + kolku) % n2;
    }

    int br = 1;
    while (curr) {
      curr = curr % __builtin_popcount(curr);
      br++;
    }
    cout << br << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

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

#define pb push_back
#define sz size()
#define ll long long

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

int main() {

  int n;
  string s;
  cin >> n;
  cin >> s;

  int i;
  int nuli = 0;
  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      nuli++;
    }

  int n1 = nuli - 1;
  int n2 = nuli + 1;

  int presm1 = 0;
  int presm2 = 0;

  for (i = 0; i < n; i++)
    if (s[i] == '1') {
      if (nuli > 0)
        presm1 += binpow(2, n - i - 1, n1);
      presm2 += binpow(2, n - i - 1, n2);
    }

  if (nuli > 0)
    presm1 %= n1;
  presm2 %= n2;

  for (i = 0; i < n; i++) {

    int curr = 0;

    if (s[i] == '1' && nuli > 1) {
      curr = presm1;
      int kolku = binpow(2, n - i - 1, n1);
      curr = (curr + n1 - kolku) % n1;
    } else {
      curr = presm2;
      int kolku = binpow(2, n - i - 1, n2);
      curr = (curr + kolku) % n2;
    }

    int br = 1;
    while (curr) {
      curr = curr % __builtin_popcount(curr);
      br++;
    }
    cout << br << "\\n";
  }

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

#include <bits/stdc++.h>

using namespace std;
const int N = 200100;

int n, len;
char s[N];

int Ans[N], total, A, B, C;

int popcount(int x) {
  int res = 0;
  while (x) {
    x -= x & -x;
    res++;
  }
  return res;
}

int Calc(int x) {
  if (Ans[x])
    return Ans[x];
  if (!x)
    return 0;
  return Ans[x] = Calc(x % popcount(x)) + 1;
}

int Pow(int tmp) {
  int res = 0;
  if (tmp == 0)
    return 0;
  for (int i = 0; i < len; ++i) {
    if (s[i] == '0')
      res = res * 2 % tmp;
    if (s[i] == '1')
      res = (res * 2 + 1) % tmp;
  }
  return res;
}

long long PowMod(long long a, long long b, long long tmp) {
  long long res = 1 % tmp;
  while (b) {
    if (b & 1)
      res = res * a % tmp;
    b >>= 1;
    a = a * a % tmp;
  }
  return res;
}

int main() {
  cin >> n;
  scanf("%s", s);
  len = strlen(s);
  for (int i = 1; i < N; ++i)
    Ans[i] = Calc(i);
  // for(int i = 1;i <= 20; ++ i)
  //     cout << "Ans[" << i << "] = " << Ans[i] << endl;
  for (int i = 0; i < len; ++i)
    if (s[i] == '1')
      total++;
  A = Pow(total - 1);
  B = Pow(total);
  C = Pow(total + 1);
  for (int i = 0; i < len; ++i) {
    int tmp;
    if (s[i] == '0') {
      tmp = (C + PowMod(2, len - i - 1, total + 1)) % (total + 1);
      cout << Ans[tmp] + 1 << endl;
    } else {
      if (total == 1)
        cout << 0 << endl;
      else {
        tmp = (A - PowMod(2, len - i - 1, total - 1) + total - 1) % (total - 1);
        cout << Ans[tmp] + 1 << endl;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int N = 200100;

int n, len;
char s[N];

int Ans[N], total, A, B, C;

int popcount(int x) {
  int res = 0;
  while (x) {
    x -= x & -x;
    res++;
  }
  return res;
}

int Calc(int x) {
  if (Ans[x])
    return Ans[x];
  if (!x)
    return 0;
  return Ans[x] = Calc(x % popcount(x)) + 1;
}

int Pow(int tmp) {
  int res = 0;
  if (tmp == 0)
    return 0;
  for (int i = 0; i < len; ++i) {
    if (s[i] == '0')
      res = res * 2 % tmp;
    if (s[i] == '1')
      res = (res * 2 + 1) % tmp;
  }
  return res;
}

int PowMod(int a, int b, int tmp) {
  int res = 1 % tmp;
  while (b) {
    if (b & 1)
      res = res * a % tmp;
    b >>= 1;
    a = a * a % tmp;
  }
  return res;
}

int main() {
  cin >> n;
  scanf("%s", s);
  len = strlen(s);
  for (int i = 1; i < N; ++i)
    Ans[i] = Calc(i);
  // for(int i = 1;i <= 20; ++ i)
  //     cout << "Ans[" << i << "] = " << Ans[i] << endl;
  for (int i = 0; i < len; ++i)
    if (s[i] == '1')
      total++;
  A = Pow(total - 1);
  B = Pow(total);
  C = Pow(total + 1);
  if (total == 1)
    A = 0;
  for (int i = 0; i < len; ++i) {
    int tmp;
    if (s[i] == '0') {
      tmp = (C + PowMod(2, len - i - 1, total + 1)) % (total + 1);
      cout << Ans[tmp] + 1 << endl;
    } else {
      if (total == 1)
        cout << 0 << endl;
      else {
        tmp = (A - PowMod(2, len - i - 1, total - 1) + total - 1) % (total - 1);
        cout << Ans[tmp] + 1 << endl;
      }
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int f(int x) {
  if (x == 0)
    return 0;
  int p = 0;
  rep(bit, 24) {
    if (x >> bit & 1) {
      p++;
    }
  }
  return f(x % p) + 1;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int count = 0;
  rep(i, n) if (x[i] == '1') count++;
  vector<vector<int>> res(n, vector<int>(2));
  int p = 1, q = 1;
  vector<int> t(2);
  rep(i, n) {
    res[n - 1 - i][0] = p;
    p = p * 2 % (count + 1);
  }
  rep(i, n) if (x[i] == '1') t[0] = (t[0] + res[i][0]) % (count + 1);

  if (count - 1) {
    rep(i, n) {
      res[n - 1 - i][1] = q;
      q = q * 2 % (count - 1);
    }
    rep(i, n) if (x[i] == '1') t[1] = (t[1] + res[i][1]) % (count - 1);
  }

  rep(i, n) {
    int j = x[i] - '0';
    int y;
    if (j == 0) {
      y = (t[j] + res[i][j]) % (count + 1);
      cout << f(y) + 1 << endl;
    }
    if (j == 1) {
      if (count - 1) {
        y = (t[j] - res[i][j] + count - 1) % (count - 1);
        cout << f(y) + 1 << endl;
      } else
        cout << 0 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;

int f(int x) {
  if (x == 0)
    return 0;
  int p = 0;
  rep(bit, 24) {
    if (x >> bit & 1) {
      p++;
    }
  }
  return f(x % p) + 1;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  vector<int> count(2);
  rep(i, n) if (x[i] == '1') {
    count[0]++;
    count[1]++;
  }
  count[0]++;
  count[1]--;
  vector<vector<int>> res(n, vector<int>(2));
  int p = 1;
  int q = 1;
  vector<int> t(2);
  rep(i, n) {
    res[n - 1 - i][0] = p;
    res[n - 1 - i][1] = q;
    p = p * 2 % count[0];
    q = q * 2 % count[1];
  }
  rep(i, n) {
    if (x[i] == '1') {
      t[0] = (t[0] + res[i][0]) % count[0];
      t[1] = (t[1] + res[i][1]) % count[1];
    }
  }
  rep(i, n) {
    int j = x[i] - '0';
    int y;
    if (j == 0)
      y = (t[j] + res[i][j]) % (count[j]);
    if (j == 1)
      y = (t[j] - res[i][j] + count[j]) % (count[j]);
    cout << f(y) + 1 << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll p1[2000001], p3[2000001], a[2000001];
char s[2000001];
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ll cnt = 0;
  ll n;
  cin >> n;
  cin >> s;
  ll c = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      c++;
  }
  if (c > 1)
    p1[0] = 1 % (c - 1);
  p3[0] = 1 % (c + 1);
  for (int i = 1; i < n; i++) {
    if (c > 1)
      p1[i] = p1[i - 1] * (2 % (c - 1));
    if (c > 1)
      p1[i] %= (c - 1);
    p3[i] = p3[i - 1] * (2 % (c + 1));
    p3[i] %= (c + 1);
  }
  ll r1 = 0, r2 = 0, r3 = 0;
  int i = 0, j = n - 1;
  while (i < n) {
    if (s[j] == '1') {
      if (c > 1)
        r1 += (p1[i]);
      if (c > 1)
        r1 %= (c - 1);
      r3 += (p3[i]);
      r3 %= (c + 1);
    }
    i++;
    j--;
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll aa = (r3 + p3[n - i - 1]) % (c + 1);
      int cn = 1;
      while (aa > 0) {
        ll bb = __builtin_popcount(aa);
        aa %= bb;
        cn++;
      }
      a[i] = cn;
    } else {

      ll aa;
      int cn = 1;
      if (c > 1)
        aa = (r1 - p1[n - i - 1] + c - 1) % (c - 1);
      else {
        cn = 0;
        aa = 0;
      }
      while (aa > 0) {
        ll bb = __builtin_popcount(aa);
        aa %= bb;
        cn++;
      }
      a[i] = cn;
    }
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll p1[2000001], p3[2000001], a[2000001];
char s[2000001];
int main() {
  cin.tie(NULL);
  cout.tie(NULL);
  ll cnt = 0;
  ll n;
  cin >> n;
  cin >> s;
  ll c = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      c++;
  }
  p1[0] = 1 % (c - 1);
  p3[0] = 1 % (c + 1);
  for (int i = 1; i < n; i++) {
    p1[i] = p1[i - 1] * (2 % (c - 1));
    p1[i] %= (c - 1);
    p3[i] = p3[i - 1] * (2 % (c + 1));
    p3[i] %= (c + 1);
  }
  ll r1 = 0, r2 = 0, r3 = 0;
  int i = 0, j = n - 1;
  while (i < n) {
    if (s[j] == '1') {
      r1 += (p1[i]);
      r1 %= (c - 1);
      r3 += (p3[i]);
      r3 %= (c + 1);
    }
    i++;
    j--;
  }
  for (ll i = 0; i < n; i++) {
    if (s[i] == '0') {
      ll aa = (r3 + p3[n - i - 1]) % (c + 1);
      int cn = 1;
      while (aa > 0) {
        ll bb = __builtin_popcount(aa);
        aa %= bb;
        cn++;
      }
      a[i] = cn;
    } else {
      ll aa = (r1 - p1[n - i - 1] + c - 1) % (c - 1);
      int cn = 1;
      while (aa > 0) {
        ll bb = __builtin_popcount(aa);
        aa %= bb;
        cn++;
      }
      a[i] = cn;
    }
  }
  for (int i = 0; i < n; i++)
    cout << a[i] << endl;
  return 0;
}