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

/*The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.*/

// PRABHJOT SINGH A.K.A. PRABHI
//~~~~~conquizztador~~~~~

#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int dp[2 * maxn][2];
int pre[2];
int GET(int val) {
  if (val == 0)
    return 0;
  return GET(val % __builtin_popcount(val)) + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n = 0;
  string s;
  cin >> n;
  cin >> s;
  int ones = count(s.begin(), s.end(), '1');
  int x = ones + 1, y = ones - 1;
  if (y != 0)
    dp[n - 1][0] = 1 % y;
  dp[n - 1][1] = 1 % x;
  for (int i = n - 2; i >= 0; --i) {
    if (y != 0)
      dp[i][0] = (2 * dp[i + 1][0]) % y;
    dp[i][1] = (2 * dp[i + 1][1]) % x;
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (y != 0) {
        pre[0] += dp[i][0];
        pre[0] %= y;
      }
      pre[1] += dp[i][1];
      pre[1] %= x;
    }
  }
  for (int i = 0; i < n; ++i) {
    int z = 0;
    if (s[i] == '1') {
      z = pre[0] - dp[i][0];
      if (z < 0)
        z += y;
    } else {
      z = pre[1] + dp[i][1];
      if (z >= x)
        z -= x;
    }
    if (s[i] == '1' && ones == 1)
      cout << "0" << endl;
    else
      cout << (GET(z) + 1) << endl;
  }
}

Error: 0

Fixed submission:

/*The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.*/

// PRABHJOT SINGH A.K.A. PRABHI
//~~~~~conquizztador~~~~~

#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int dp[2 * maxn][2];
int pre[2];
int GET(int val) {
  if (val == 0)
    return 0;
  return GET(val % __builtin_popcount(val)) + 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n = 0;
  string s;
  cin >> n;
  cin >> s;
  int ones = count(s.begin(), s.end(), '1');
  int x = ones + 1, y = ones - 1;
  if (y != 1)
    dp[n - 1][0] = 1 % y;
  dp[n - 1][1] = 1 % x;
  for (int i = n - 2; i >= 0; --i) {
    if (y != 1)
      dp[i][0] = (2 * dp[i + 1][0]) % y;
    dp[i][1] = (2 * dp[i + 1][1]) % x;
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (y != 1) {
        pre[0] += dp[i][0];
        pre[0] %= y;
      }
      pre[1] += dp[i][1];
      pre[1] %= x;
    }
  }
  for (int i = 0; i < n; ++i) {
    int z = 0;
    if (s[i] == '1') {
      z = pre[0] - dp[i][0];
      if (z < 0)
        z += y;
    } else {
      z = pre[1] + dp[i][1];
      if (z >= x)
        z -= x;
    }
    if (s[i] == '1' && ones == 1)
      cout << "0" << endl;
    else
      cout << (GET(z) + 1) << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 0x3F3F3F3F;
const int maxn = 2e5 + 555;
ll xpow(ll a, ll b, const int MOD) {
  ll ret = 1;
  a %= MOD;
  assert(b >= 0);
  while (b) {
    if (b & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  int n;
  string sb;
  cin >> n >> sb;
  auto gao = [](int x) {
    int ret = 0;
    while (x) {
      ret++;
      x %= __builtin_popcount(x);
    }
    return ret;
  };
  vector<int> V;
  for (int i = 0; i <= 200000 + 55; i++) {
    V.push_back(gao(i));
  }
  int cnt = 0;
  for (auto &i : sb)
    cnt += i == '1';
  int MOD1 = cnt - 1, MOD2 = cnt + 1;
  int res1 = 0, res2 = 0;
  for (auto &i : sb) {
    if (MOD1 != 0)
      res1 = (res1 * 2 + i - '0') % MOD1;
    res2 = (res2 * 2 + i - '0') % MOD2;
  }
  for (int i = 0; i < (int)sb.size(); i++) {
    int len = (int)sb.size() - 1 - i;
    if (sb[i] == '0') {
      int add = xpow(2, len, MOD2);
      int now = (res2 + add) % MOD2;
      cout << V[now] + 1 << '\\n';
    } else {
      if (MOD1 == 0)
        cout << 0 << '\\n';
      else {
        int sub = xpow(2, len, MOD1);
        int now = ((res1 - sub) % MOD1 + MOD1) % MOD1;
        cout << V[now] + 1 << '\\n';
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 0x3F3F3F3F;
const int maxn = 2e5 + 555;
ll xpow(ll a, ll b, const int MOD) {
  ll ret = 1;
  a %= MOD;
  assert(b >= 0);
  while (b) {
    if (b & 1)
      ret = ret * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return ret;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  cout << fixed;
  int n;
  string sb;
  cin >> n >> sb;
  auto gao = [](int x) {
    int ret = 0;
    while (x) {
      ret++;
      x %= __builtin_popcount(x);
    }
    return ret;
  };
  vector<int> V;
  for (int i = 0; i <= 200000 + 55; i++) {
    V.push_back(gao(i));
  }
  int cnt = 0;
  for (auto &i : sb)
    cnt += i == '1';
  int MOD1 = cnt - 1, MOD2 = cnt + 1;
  int res1 = 0, res2 = 0;
  for (auto &i : sb) {
    res1 = (res1 * 2 + i - '0') % MOD1;
    res2 = (res2 * 2 + i - '0') % MOD2;
  }
  for (int i = 0; i < (int)sb.size(); i++) {
    int len = (int)sb.size() - 1 - i;
    if (sb[i] == '0') {
      int add = xpow(2, len, MOD2);
      int now = (res2 + add) % MOD2;
      cout << V[now] + 1 << '\\n';
    } else {
      int sub = xpow(2, len, MOD1);
      int now = ((res1 - sub) % MOD1 + MOD1) % MOD1;
      cout << V[now] + 1 << '\\n';
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
#define ll long long
#define ii pair<int, int>
#define endl '\\n'
using namespace std;
int inf = 1e9;
void q() { cout << -1 << endl; }

int pc(int n) {
  int r = 0;
  while (n) {
    r += n % 2;
    n /= 2;
  }
  return r;
}
void solve() {

  int dp[300001] = {
      0,
  };
  for (int i = 1; i <= 300000; i++)
    dp[i] = dp[i % pc(i)] + 1;

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

  int rem1 = 0;
  int rem2 = 0;
  int bits = 0;

  int ten1[n];
  int ten2[n];
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1')
      bits++;

  int div1 = bits + 1;
  int div2 = bits - 1;

  for (int i = n - 1, r = 1; i >= 0; i--) {
    ten1[i] = r;
    rem1 = (rem1 + r * (s[i] == '1')) % div1;
    r = (r * 2) % div1;
  }

  if (div2 > 0) {
    for (int i = n - 1, r = 1; i >= 0; i--) {
      ten2[i] = r;
      rem2 = (rem2 + r * (s[i] == '1')) % div2;
      r = (r * 2) % div2;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (div2 == 0)
        cout << 0 << endl;
      else {
        ll t = (rem2 - ten2[i] + div2) % div2;
        cout << dp[t] + 1 << endl;
      }
    } else {
      ll t = (rem1 + ten1[i] + div1) % div1;
      cout << dp[t] + 1 << endl;
    }
  }
}

int main() {

  /*
      int t;
      cin >> t;
      for(int i=1; i<=t; i++) */
  solve();
}

Error: 0

Fixed submission:


#include <bits/stdc++.h>
#define ll long long
#define ii pair<int, int>
#define endl '\\n'
using namespace std;
int inf = 1e9;
void q() { cout << -1 << endl; }

int pc(int n) {
  int r = 0;
  while (n) {
    r += n % 2;
    n /= 2;
  }
  return r;
}
void solve() {

  int dp[300001] = {
      0,
  };
  for (int i = 1; i <= 300000; i++)
    dp[i] = dp[i % pc(i)] + 1;

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

  int rem1 = 0;
  int rem2 = 0;
  int bits = 0;

  int ten1[n];
  int ten2[n];
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1')
      bits++;

  int div1 = bits + 1;
  int div2 = bits - 1;

  for (int i = n - 1, r = 1; i >= 0; i--) {
    ten1[i] = r;
    rem1 = (rem1 + r * (s[i] == '1')) % div1;
    r = (r * 2) % div1;
  }

  if (div2 > 0)
    for (int i = n - 1, r = 1; i >= 0; i--) {
      ten2[i] = r;
      rem2 = (rem2 + r * (s[i] == '1')) % div2;
      r = (r * 2) % div2;
    }

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      ll t = (rem2 - ten2[i] + div2) % div2;
      cout << dp[t] + 1 << endl;
    } else {
      ll t = (rem1 + ten1[i] + div1) % div1;
      cout << dp[t] + 1 << endl;
    }
  }
}

int main() {

  /*
      int t;
      cin >> t;
      for(int i=1; i<=t; i++) */
  solve();
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int mod = 1e9 + 7;
const int INF = 1001001001;

int f(int x) {
  if (x == 0) {
    return 0;
  }
  int p = __builtin_popcount(x);
  return f(x % p) + 1;
}

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

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

  vector<int> ans(N);
  for (int r = 0; r < 2; r++) {
    int basep = pop;
    if (r == 0) {
      basep++;
    } else {
      basep--;
    }

    if (basep <= 0) {
      continue;
    }

    int base = 0;
    int s = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] == '1') {
        (base += s) %= basep;
      }
      (s *= 2) %= basep;
    }
    s = 1;
    int start = 0;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] - '0' == r) {
        if (r == 0) {
          (start = base + s) %= basep;
        } else if (r == 1) {
          (start = base - s + basep) %= basep;
        }
        ans[i] = f(start) + 1;
      }
      (s *= 2) %= basep;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int mod = 1e9 + 7;
const int INF = 1001001001;

int f(int x) {
  if (x == 0) {
    return 0;
  }
  int p = __builtin_popcount(x);
  return f(x % p) + 1;
}

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

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

  vector<int> ans(N);
  for (int r = 0; r < 2; r++) {
    int basep = pop;
    if (r == 0) {
      basep++;
    } else {
      basep--;
    }

    int base = 0;
    int s = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] == '1') {
        (base += s) %= basep;
      }
      (s *= 2) %= basep;
    }
    s = 1;
    int start = 0;
    for (int i = N - 1; i >= 0; i--) {
      if (S[i] - '0' == r) {
        if (r == 0) {
          (start = base + s) %= basep;
        } else if (r == 1) {
          (start = base - s + basep) %= basep;
        }
        ans[i] = f(start) + 1;
      }
      (s *= 2) %= basep;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

void solve0(int N) {
  for (int i = 0; i < N; i++)
    cout << "1\\n";
  return;
}

void solve1(int N, string &S) {
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      cout << "0\\n";
    } else {
      if (S[N - 1] == '1' || i == N - 1)
        cout << "2\\n";
      else
        cout << "1\\n";
    }
  }
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string S;
  cin >> S;

  int cnt = 0;
  for (auto c : S)
    if (c == '1')
      cnt++;
  if (cnt == 0) {
    solve0(N);
    return 0;
  }
  if (cnt == 1) {
    solve1(N, S);
    return 0;
  }
  int umod = cnt + 1;
  int lmod = cnt - 1;
  // cout << umod << " " << lmod << endl;

  vector<int> u(N), l(N);
  u[N - 1] = 1;
  l[N - 1] = 1;
  for (int i = N - 2; i >= 0; i--) {
    u[i] = (u[i + 1] * 2) % umod;
    l[i] = (l[i + 1] * 2) % lmod;
    // cout << N-i << ", " << u[i] << ", " << l[i] << endl;
  }

  int ust = 0;
  int lst = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      ust = (ust + u[i]) % umod;
      lst = (lst + l[i]) % lmod;
    }
  }

  for (int i = 0; i < N; i++) {
    int ans = 1;
    if (S[i] == '0') {
      int num = (ust + u[i]) % umod;
      // cout << "0 " << num << ", " << ust << ", " << u[i] << endl;
      while (num) {
        num %= __popcount<int>(num);
        ans++;
      }
    } else {
      int num = (lst - l[i] + lmod) % lmod;
      // cout << "1 " << num << endl;
      while (num) {
        num %= __popcount<int>(num);
        ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string S;
  cin >> S;

  int cnt = 0;
  for (auto c : S)
    if (c == '1')
      cnt++;
  int umod = cnt + 1;
  int lmod = cnt - 1;
  // cout << umod << " " << lmod << endl;

  vector<int> u(N), l(N);
  u[N - 1] = 1;
  l[N - 1] = 1;
  for (int i = N - 2; i >= 0; i--) {
    u[i] = (u[i + 1] * 2) % umod;
    l[i] = (l[i + 1] * 2) % lmod;
    // cout << N-i << ", " << u[i] << ", " << l[i] << endl;
  }

  int ust = 0;
  int lst = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      ust = (ust + u[i]) % umod;
      lst = (lst + l[i]) % lmod;
    }
  }

  for (int i = 0; i < N; i++) {
    int ans = 1;
    if (S[i] == '0') {
      int num = (ust + u[i]) % umod;
      // cout << "0 " << num << ", " << ust << ", " << u[i] << endl;
      while (num) {
        num %= __popcount<int>(num);
        ans++;
      }
    } else {
      int num = (lst - l[i] + lmod) % lmod;
      // cout << "1 " << num << endl;
      while (num) {
        num %= __popcount<int>(num);
        ans++;
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int ans[200005];
int f(int x) {
  if (x == 0)
    return 0;
  if (ans[x])
    return ans[x];
  return ans[x] = f(x % __builtin_popcount(x)) + 1;
}
int div1[200005], div2[200005];
signed main() {
  int N;
  std::string S;
  std::cin >> N >> S;
  int cnt = 0, now1 = 1, now2 = 1;
  rep(i, N) {
    if (S[i] == '1')
      cnt++;
  }
  int ans1 = 0, ans2 = 0;
  rev(i, N) {
    div1[N - 1 - i] = now1;
    div2[N - 1 - i] = now2;
    if (S[i] == '1') {
      ans1 += now1;
      ans2 += now2;
      ans1 %= cnt + 1;
      if (cnt != 1)
        ans2 %= cnt - 1;
    }
    now1 *= 2;
    now1 %= cnt + 1;
    if (cnt != 1) {
      now2 *= 2;
      now2 %= cnt - 1;
    }
  }
  rep(i, N) {
    if (cnt == 1 && S[i] == '1')
      cout << "0\\n";
    else if (S[i] == '1') {
      int j = (ans2 - div2[N - i - 1] + cnt - 1) % (cnt - 1);
      cout << f(j) + 1 << "\\n";
    } else {
      int j = (ans1 + div1[N - i - 1]) % (cnt + 1);
      cout << f(j) + 1 << "\\n";
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;

// using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr int inf = 3e18;

int ans[200005];
int f(int x) {
  if (x == 0)
    return 0;
  if (ans[x])
    return ans[x];
  return ans[x] = f(x % __builtin_popcount(x)) + 1;
}
signed main() {
  int N;
  std::string S;
  std::cin >> N >> S;
  cpp_int ans = 0;
  int cnt = 0;
  rep(i, N) {
    if (S[i] == '1') {
      cnt++;
      ans += (cpp_int)1 << (N - 1 - i);
    }
  }
  rev(i, N) {
    cpp_int j = ans ^ (cpp_int)1 << i;
    if (j == 0)
      std::cout << "0\\n";
    else if (ans >> i & 1) {
      std::cout << f((int)(j % (cnt - 1))) + 1 << "\\n";
    } else {
      std::cout << f((int)(j % (cnt + 1))) + 1 << "\\n";
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string x;
  cin >> n >> x;
  int m = 0;
  for (int i = 0; i < n; i++)
    m += (int)(x.at(i) - '0');
  int rp = 0;
  int rm = 0;
  for (int i = 0; i < n; i++) {
    rp = (rp * 2 + (int)(x.at(i) - '0')) % (m + 1);
    if (m > 1)
      rm = (rm * 2 + (int)(x.at(i) - '0')) % (m - 1);
  }
  vector<int> c(n, 0);
  int pp = 1;
  int pm = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (m == 1 && x.at(i) == '1')
      continue;
    int ci = 1;
    int b =
        (x.at(i) == '0' ? (rp + pp) % (m + 1) : (rm - pm + m - 1) % (m - 1));
    while (b > 0) {
      ci++;
      int a = b;
      int pc = 0;
      while (a > 0) {
        pc += a % 2;
        a /= 2;
      }
      b %= pc;
    }
    c.at(i) = ci;
    pp = pp * 2 % (m + 1);
    if (m > 1)
      pm = pm * 2 % (m - 1);
  }
  for (int i = 0; i < n; i++)
    cout << c.at(i) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  string x;
  cin >> n >> x;
  int m = 0;
  for (int i = 0; i < n; i++)
    m += (int)(x.at(i) - '0');
  int rp = 0;
  int rm = 0;
  for (int i = 0; i < n; i++) {
    rp = (rp * 2 + (int)(x.at(i) - '0')) % (m + 1);
    rm = (rm * 2 + (int)(x.at(i) - '0')) % (m - 1);
  }
  vector<int> c(n);
  int pp = 1;
  int pm = 1;
  for (int i = n - 1; i >= 0; i--) {
    int ci = 1;
    int b =
        (x.at(i) == '0' ? (rp + pp) % (m + 1) : (rm - pm + m - 1) % (m - 1));
    while (b > 0) {
      ci++;
      int a = b;
      int pc = 0;
      while (a > 0) {
        pc += a % 2;
        a /= 2;
      }
      b %= pc;
    }
    c.at(i) = ci;
    pp = pp * 2 % (m + 1);
    pm = pm * 2 % (m - 1);
  }
  for (int i = 0; i < n; i++)
    cout << c.at(i) << endl;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int pcnt(int x) { return __builtin_popcount(x); }

int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  for (int i = 0; i < n; ++i)
    x[i] = s[i] - '0';
  int pc = 0;
  for (int i = 0; i < n; ++i)
    pc += x[i];
  vector<int> ans(n);
  for (int b = 0; b < 2; ++b) {
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;

    if (npc <= 0)
      continue;
    int r0 = 0;
    for (int i = 0; i < n; ++i) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (x[i] == b) {
        int r = r0;
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[i] = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int pcnt(int x) { return __builtin_popcount(x); }

int f(int x) {
  if (x == 0)
    return 0;
  return f(x % pcnt(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  for (int i = 0; i < n; ++i)
    x[i] = s[i] - '0';
  int pc = 0;
  for (int i = 0; i < n; ++i)
    pc += x[i];
  vector<int> ans(n);
  for (int b = 0; b < 2; ++b) {
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    int r0 = 0;
    for (int i = 0; i < n; ++i) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    int k = 1;
    for (int i = n - 1; i >= 0; --i) {
      if (x[i] == b) {
        int r = r0;
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans[i] = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\\|/__\\|/__\\|/__\\|/__\\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200002;

int n;

bool a[N_MAX];

int dp[N_MAX];

int ppc(int x) {
  int ans = 0;
  while (x > 0) {
    if (x & 1)
      ans++;
    x >>= 1;
  }
  return ans;
}

int ppx0, ppx1, ppx2;

int pw0[N_MAX], pw2[N_MAX];

int md0, md2;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s = " " + s;
  for (int i = 1; i <= n; i++)
    a[i] = (s[i] == '1');
  for (int i = 1; i <= n; i++)
    dp[i] = dp[i % ppc(i)] + 1;
  for (int i = 1; i <= n; i++)
    ppx1 += a[i];
  ppx0 = max(1, ppx1 - 1);
  ppx2 = ppx1 + 1;
  pw0[0] = pw2[0] = 1;
  for (int i = 1; i <= n; i++) {
    pw0[i] = 1LL * pw0[i - 1] * 2 % ppx0;
    pw2[i] = 1LL * pw2[i - 1] * 2 % ppx2;
  }
  for (int i = 1; i <= n; i++) {
    md0 = (md0 + a[i] * pw0[i - 1]) % ppx0;
    md2 = (md2 + a[i] * pw2[i - 1]) % ppx2;
  }
  for (int i = n; i >= 1; i--) {
    if (a[i] == 1) {
      if (ppx1 == 1)
        cout << "0\\n";
      else {
        int val = (md0 - pw0[i - 1] + ppx0) % ppx0;
        cout << dp[val] + 1 << "\\n";
      }
    } else {
      int val = (md2 + pw2[i - 1]) % ppx2;
      cout << dp[val] + 1 << "\\n";
    }
  }
  return 0;
}


Error: 0

Fixed submission:

/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\\|/__\\|/__\\|/__\\|/__\\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N_MAX = 200002;

int n;

bool a[N_MAX];

int dp[N_MAX];

int ppc(int x) {
  int ans = 0;
  while (x > 0) {
    if (x & 1)
      ans++;
    x >>= 1;
  }
  return ans;
}

int ppx0, ppx1, ppx2;

int pw0[N_MAX], pw1[N_MAX], pw2[N_MAX];

int md0, md1, md2;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  s = " " + s;
  for (int i = 1; i <= n; i++)
    a[i] = (s[i] == '1');
  for (int i = 1; i <= n; i++)
    dp[i] = dp[i % ppc(i)] + 1;
  for (int i = 1; i <= n; i++)
    ppx1 += a[i];
  ppx0 = ppx1 - 1;
  ppx2 = ppx1 + 1;
  pw0[0] = pw1[0] = pw2[0] = 1;
  for (int i = 1; i <= n; i++) {
    pw0[i] = 1LL * pw0[i - 1] * 2 % ppx0;
    pw1[i] = 1LL * pw1[i - 1] * 2 % ppx1;
    pw2[i] = 1LL * pw2[i - 1] * 2 % ppx2;
  }
  for (int i = 1; i <= n; i++) {
    md0 = (md0 + a[i] * pw0[i - 1]) % ppx0;
    md1 = (md1 + a[i] * pw1[i - 1]) % ppx1;
    md2 = (md2 + a[i] * pw2[i - 1]) % ppx2;
  }
  for (int i = n; i >= 1; i--) {
    if (a[i] == 1) {
      int val = (md0 - pw0[i - 1] + ppx0) % ppx0;
      cout << dp[val] + 1 << "\\n";
    } else {
      int val = (md2 + pw2[i - 1]) % ppx2;
      cout << dp[val] + 1 << "\\n";
    }
  }
  return 0;
}