alexjercan/bugnet huggingface dataset:
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 ll = long long;
using P = pair<int, int>;

int popcnt(int n) {
  int n_ = n;
  int res = 0;
  while (n_ > 0) {
    if (n_ % 2 == 1)
      res++;
    n_ = n_ >> 1;
  }
  return res;
}

int func(int n) {
  int n_ = n;
  int res = 0;
  while (n_ > 0) {
    res++;
    n_ %= popcnt(n_);
  }
  return res;
}

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

  reverse(x.begin(), x.end());

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

  // 2^0...2^nをpopcount(x)+1で割ったあまり
  vector<int> r_p(n, 1);
  for (int i = 1; i < n; i++) {
    r_p[i] = (r_p[i - 1] * 2) % (x_pop + 1);
  }

  // 2^0...2^nをpopcount(x)-1で割ったあまり
  vector<int> r_m(n, 0);
  if (x_pop - 1 > 0) {
    r_m[0] = 1;
    for (int i = 1; i < n; i++) {
      r_m[i] = (r_m[i - 1] * 2) % (x_pop - 1);
    }
  }

  // x % (popcount(x) + 1)
  // x % (popcount(x) - 1)
  ll x_r_p = 0;
  ll x_r_m = 0;
  rep(i, n) {
    if (x[i] == '1') {
      x_r_p += r_p[i];
      if (r_m[i] == 0)
        continue;
      x_r_m += r_m[i];
    }
  }

  // f(x_i)
  for (int i = n - 1; i >= 0; i--) {
    int first;

    if (x[i] == '0') {
      first = (x_r_p + r_p[i]) % (x_pop + 1);
    } else if (x_pop - 1 > 0) {
      first = (x_r_m - r_m[i]) % (x_pop - 1);
    } else {
      cout << 0 << endl;
      continue;
    }

    cout << 1 + func(first) << 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 ll = long long;
using P = pair<int, int>;

int popcnt(int n) {
  int n_ = n;
  int res = 0;
  while (n_ > 0) {
    if (n_ % 2 == 1)
      res++;
    n_ = n_ >> 1;
  }
  return res;
}

int func(int n) {
  int n_ = n;
  int res = 0;
  while (n_ > 0) {
    res++;
    n_ %= popcnt(n_);
  }
  return res;
}

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

  reverse(x.begin(), x.end());

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

  // 2^0...2^nをpopcount(x)+1で割ったあまり
  vector<int> r_p(n, 1);
  for (int i = 1; i < n; i++) {
    r_p[i] = (r_p[i - 1] * 2) % (x_pop + 1);
  }

  // 2^0...2^nをpopcount(x)-1で割ったあまり
  vector<int> r_m(n, 0);
  if (x_pop - 1 > 0) {
    r_m[0] = 1;
    for (int i = 1; i < n; i++) {
      r_m[i] = (r_m[i - 1] * 2) % (x_pop - 1);
    }
  }

  // x % (popcount(x) + 1)
  // x % (popcount(x) - 1)
  int x_r_p = 0;
  int x_r_m = 0;
  rep(i, n) {
    if (x[i] == '1') {
      x_r_p += r_p[i];
      x_r_m += r_m[i];
      x_r_p %= (x_pop + 1);
      x_r_m %= (x_pop - 1);
    }
  }

  // f(x_i)
  for (int i = n - 1; i >= 0; i--) {
    int first;

    if (x[i] == '0') {
      first = (x_r_p + r_p[i]) % (x_pop + 1);
    } else if (x_pop - 1 > 0) {
      first = (x_r_m - r_m[i]) % (x_pop - 1);
    } else {
      cout << 0 << endl;
      continue;
    }

    cout << 1 + func(first) << endl;
  }

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

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

int reduce2zero(int i);

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

  int popcount = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      ++popcount;
    }
  }
  int popp = popcount + 1;
  int popm = popcount - 1;

  int modulop = 1;
  int modulom = 1;
  int sump = 0;
  int summ = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      sump += modulop;
      sump %= popp;
      if (popm > 0) {
        summ += modulom;
        summ %= popm;
      }
    }
    modulop <<= 1;
    modulop %= popp;
    if (popm > 0) {
      modulom <<= 1;
      modulom %= popm;
    }
  }
  sump %= popp;
  if (popm > 0) {
    summ %= popm;
  }

  vector<int> after1st(n);
  modulop = 1;
  modulom = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '0') {
      after1st[i] = (sump + modulop) % popp;
    } else {
      if (popm > 0) {
        after1st[i] = (summ - modulom + popm) % popm;
      } else {
        after1st[i] = -1;
      }
    }
    modulop <<= 1;
    modulop %= popp;
    if (popm > 0) {
      modulom <<= 1;
      modulom %= popm;
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << reduce2zero(after1st[i]) << endl;
  }
}

int reduce2zero(int i) {
  int count = 1;
  if (i < 0) {
    return 0;
  }
  while (i > 0) {
    i = i % __builtin_popcount(i);
    ++count;
  }
  return count;
}


Error: 0

Fixed submission:

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

int reduce2zero(int i);

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

  int popcount = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      ++popcount;
    }
  }
  int popp = popcount + 1;
  int popm = popcount - 1;

  int modulop = 1;
  int modulom = 1;
  int sump = 0;
  int summ = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '1') {
      sump += modulop;
      sump %= popp;
      summ += modulom;
      summ %= popm;
    }
    modulop <<= 1;
    modulop %= popp;
    modulom <<= 1;
    modulom %= popm;
  }
  sump %= popp;
  summ %= popm;

  vector<int> after1st(n);
  modulop = 1;
  modulom = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (x[i] == '0') {
      after1st[i] = (sump + modulop) % popp;
    } else {
      after1st[i] = (summ - modulom + popm) % popm;
    }
    modulop <<= 1;
    modulop %= popp;
    modulom <<= 1;
    modulom %= popm;
  }

  for (int i = 0; i < n; ++i) {
    cout << reduce2zero(after1st[i]) << endl;
  }
}

int reduce2zero(int i) {
  int count = 1;
  while (i > 0) {
    i = i % __builtin_popcount(i);
    ++count;
  }
  return count;
}
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

int popcount(int n) {
  int ret = 0;
  while (n > 0) {
    ret += n % 2;
    n /= 2;
  }
  return ret;
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  vector<int> f(200000, 0);
  for (int i = 1; i < 200000; i++) {
    f[i] = f[i % popcount(i)] + 1;
  }
  // rep(i, 10) cout << f[i] << "\\n";
  ll p = 0;
  rep(i, N) if (X[i] == '1') p++;
  vector<int> ans(N);
  if (p == 0) {
    rep(i, N) ans[i] = 1;
  } else if (p == 1) {
    rep(i, N) {
      if (X[i] == '0') {
        if (i == N - 1)
          ans[i] = 2;
        else
          ans[i] = 1;
      } else {
        ans[i] = 0;
      }
    }
  } else {
    ll q = (p - 1) * (p + 1), r = 0;
    rep(i, N) {
      r = r * 2 + X[i] - '0';
      r = r % q;
    }
    ll d = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (X[i] == '1') {
        ll s = (r + q - d) % (p - 1);
        ans[i] = f[s] + 1;
      } else {
        ll s = (r + d) % (p + 1);
        ans[i] = f[s] + 1;
      }
      d = (d * 2) % q;
    }
  }
  rep(i, N) cout << ans[i] << "\\n";
}

Error: TLE

Fixed submission:

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

typedef long long ll;

int popcount(int n) {
  int ret = 0;
  while (n > 0) {
    ret += n % 2;
    n /= 2;
  }
  return ret;
}

int g(string S) {
  int n = S.size(), ret = 0, p = 0;
  rep(i, n) if (S[i] == '1') p++;
  if (p == 0)
    return 0;
  rep(i, n) {
    ret = ret * 2 + S[i] - '0';
    ret = ret % p;
  }
  return ret;
}

int main() {
  int N;
  string X;
  cin >> N >> X;
  vector<int> f(200000, 0);
  for (int i = 1; i < 200000; i++) {
    f[i] = f[i % popcount(i)] + 1;
  }
  // rep(i, 10) cout << f[i] << "\\n";
  rep(i, N) {
    string Y = X;
    if (X[i] == '0')
      Y[i] = '1';
    else
      Y[i] = '0';
    int ans = 0;
    bool b = true;
    rep(i, N) if (Y[i] == '1') b = false;
    if (b) {
      cout << "0\\n";
      continue;
    }
    int x = g(Y);
    cout << f[x] + 1 << "\\n";
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, b[200009], num = 0;
ll tmp = 0, c[200009], d[200009], tmp1 = 0;
char a[200009];
ll pow1(ll a, int n, ll m) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return ans;
}
void solve(int x) {
  // cout<<x<<endl;
  int num = 1;
  while (x) {
    int tmp = x, num1 = 0;
    while (tmp) {
      num1 += tmp % 2;
      tmp /= 2;
    }
    x %= num1;
    num++;
  }
  printf("%d\\n", num);
}
int main() {
  scanf("%d%s", &n, a);
  for (int i = 0; i < n; i++)
    b[n - i - 1] = a[i] - '0';
  for (int i = 0; i < n; i++)
    num += b[i];
  for (int i = 0; i < n; i++) {
    if (num - 1 > 0)
      c[i] = pow1(2, i, num - 1);
    d[i] = pow1(2, i, num + 1);
    if (b[i])
      tmp += c[i], tmp1 += d[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    if (b[i]) {
      if (num - 1 != 0)
        solve((tmp - c[i]) % (num - 1));
      else
        printf("0\\n");
    } else
      solve((tmp1 + d[i]) % (num + 1));
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n, b[200009], num = 0;
ll tmp = 0, c[200009], d[200009], tmp1 = 0;
char a[200009];
ll pow1(ll a, int n, ll m) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return ans;
}
void solve(int x) {
  // cout<<x<<endl;
  int num = 1;
  while (x) {
    int tmp = x, num1 = 0;
    while (tmp) {
      num1 += tmp % 2;
      tmp /= 2;
    }
    x %= num1;
    num++;
  }
  printf("%d\\n", num);
}
int main() {
  scanf("%d%s", &n, a);
  for (int i = 0; i < n; i++)
    b[n - i - 1] = a[i] - '0';
  for (int i = 0; i < n; i++)
    num += b[i];
  for (int i = 0; i < n; i++) {
    c[i] = pow1(2, i, num - 1);
    d[i] = pow1(2, i, num + 1);
    if (b[i])
      tmp += c[i], tmp1 += d[i];
  }
  for (int i = n - 1; i >= 0; i--) {
    if (b[i]) {
      if (num - 1 != 0)
        solve((tmp - c[i]) % (num - 1));
      else
        printf("0\\n");
    } else
      solve((tmp1 + d[i]) % (num + 1));
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

ll N;
string X;
ll pop[200010];
ll bin[200010][3];
vector<ll> v;

ll popcount(ll n) {
  if (n == 0)
    return 0;
  else if (pop[n] > 0)
    return pop[n];
  ll d = __builtin_popcount(n);
  pop[n] = 1 + popcount(n % d);
  return pop[n];
}

signed main() {
  cin >> N >> X;
  reverse(ALL(X));
  ll B = 0;
  REP(i, N) if (X[i] == '1') B++;
  bin[0][0] = bin[0][1] = bin[0][2] = 1;
  REP(i, N) {
    if (B > 1)
      bin[i + 1][0] = (bin[i][0] * 2) % (B - 1);
    if (B > 0)
      bin[i + 1][1] = (bin[i][1] * 2) % B;
    bin[i + 1][2] = (bin[i][2] * 2) % (B + 1);
  }
  ll Y = 0, Z = 0;
  REP(i, N) {
    if (B > 1)
      Y = (Y + (X[i] - '0') * bin[i][0]) % (B - 1);
    Z = (Z + (X[i] - '0') * bin[i][2]) % (B + 1);
  }
  REP(i, N) {
    if (X[i] == '0') {
      v.push_back(popcount((Z + bin[i][2]) % (B + 1)));
    } else {
      if (B > 1)
        v.push_back(popcount((Y - bin[i][0] + B - 1) % (B - 1)));
      else
        v.push_back(-1);
    }
  }
  REP(i, N) { PRINT(v[N - i - 1] + 1); }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

ll N;
string X;
ll pop[200010];
ll bin[200010][3];
vector<ll> v;

ll popcount(ll n) {
  if (n == 0)
    return 0;
  else if (pop[n] > 0)
    return pop[n];
  ll d = __builtin_popcount(n);
  pop[n] = 1 + popcount(n % d);
  return pop[n];
}

signed main() {
  cin >> N >> X;
  reverse(ALL(X));
  ll B = 0;
  REP(i, N) if (X[i] == '1') B++;
  bin[0][0] = bin[0][1] = bin[0][2] = 1;
  REP(i, N) {
    if (B > 1)
      bin[i + 1][0] = (bin[i][0] * 2) % (B - 1);
    bin[i + 1][1] = (bin[i][1] * 2) % B;
    bin[i + 1][2] = (bin[i][2] * 2) % (B + 1);
  }
  ll Y = 0, Z = 0;
  REP(i, N) {
    if (B > 1)
      Y = (Y + (X[i] - '0') * bin[i][0]) % (B - 1);
    Z = (Z + (X[i] - '0') * bin[i][2]) % (B + 1);
  }
  REP(i, N) {
    if (X[i] == '0') {
      v.push_back(popcount((Z + bin[i][2]) % (B + 1)));
    } else {
      if (B > 1)
        v.push_back(popcount((Y - bin[i][0] + B - 1) % (B - 1)));
      else
        v.push_back(-1);
    }
  }
  REP(i, N) { PRINT(v[N - i - 1] + 1); }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define per(i, n) for (int i = (n)-1; i >= 0; --i)
#define rng(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

ll popcount(ll x) {
  ll res = 0;
  while (x > 0) {
    res += x % 2;
    x /= 2;
  }
  return res;
}

vl exe_counts;
void init(ll N = 200020) {
  exe_counts.assign(N, 0);
  rng(i, 1, N + 1) {
    ll p = popcount(i);
    exe_counts[i] = 1 + exe_counts[i % p];
  }
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  reverse(all(x));
  init();

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

  vl xm(n), xp(n);
  ll bm = 1, bp = 1;
  rep(i, n) {
    if (p > 1) {
      xm[i] = bm;
      bm = (bm * 2) % (p - 1);
    }
    xp[i] = bp;
    bp = (bp * 2) % (p + 1);
  }

  ll ansm = 0, ansp = 0;
  rep(i, n) {
    if (x[i] == '1') {
      ansm += xm[i];
      ansp += xp[i];
    }
  }
  vl ans(n);
  rep(i, n) {
    if (x[i] == '0') {
      ll idx = (ansp + xp[i]) % (p + 1);
      if (idx < 0)
        idx += p + 1;
      ans[n - i - 1] = 1 + exe_counts[idx];
    } else {
      if (p <= 1) {
        ans[n - i - 1] = 0;
        continue;
      }
      ll idx = (ansm - xm[i]) % (p - 1);
      if (idx < 0)
        idx += p - 1;
      else
        ans[n - i - 1] = 1 + exe_counts[idx];
    }
  }
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define per(i, n) for (int i = (n)-1; i >= 0; --i)
#define rng(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

int popcount(int x) {
  int res = 0;
  while (x > 0) {
    res += x % 2;
    x /= 2;
  }
  return res;
}

vl pop_counts;
void init(int N = 200020) {
  pop_counts.assign(N, 0);
  rng(i, 1, N + 1) {
    int p = popcount(i);
    pop_counts[i] = 1 + pop_counts[i % p];
  }
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  reverse(all(x));
  init();

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

  vl xm(n), xp(n);
  int bm = 1, bp = 1;
  rep(i, n) {
    xm[i] = bm;
    xp[i] = bp;
    if (p > 1)
      bm = (bm * 2) % (p - 1);
    bp = (bp * 2) % (p + 1);
  }

  int ansm = 0, ansp = 0;
  rep(i, n) {
    if (x[i] == '1') {
      ansm += xm[i];
      ansp += xp[i];
    }
  }
  vl ans(n);
  rep(i, n) {
    if (x[i] == '0') {
      ans[n - i - 1] = 1 + pop_counts[(ansp + xp[i] + p + 1) % (p + 1)];
    } else {
      if (p <= 1)
        ans[n - i - 1] = 0;
      else
        ans[n - i - 1] = 1 + pop_counts[(ansm - xm[i] + p - 1) % (p - 1)];
    }
  }
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  vector<ll> a(n, 0);
  for (ll i = 1; i < n; i++) {
    ll pc = __builtin_popcountll(i);
    a.at(i) = a.at(i % pc) + 1;
  }
  ll npc = 0;
  for (ll i = 0; i < n; i++) {
    if (x.at(i) == '1')
      npc++;
  }
  vector<ll> modlow(n, 0);
  vector<ll> modhigh(n, 0);
  for (ll i = n - 1; i >= 0; i--) {
    if (i == n - 1) {
      modlow.at(i) = 1;
      modhigh.at(i) = 1;
    } else {
      if (npc != 1)
        modlow.at(i) = (modlow.at(i + 1) * 2) % (npc - 1);
      else
        modlow.at(i) = 0;
      modhigh.at(i) = (modhigh.at(i + 1) * 2) % (npc + 1);
    }
  }
  ll lowm = 0;
  ll highm = 0;
  for (ll i = 0; i < n; i++) {
    if (x.at(i) == '1') {
      lowm += modlow.at(i);
      highm += modhigh.at(i);
    }
  }
  if (npc != 1)
    lowm %= (npc - 1);
  highm %= (npc + 1);
  for (ll i = 0; i < n; i++) {
    if (x.at(i) == '1') {
      ll lx = lowm - modlow.at(i);
      if (npc != 1)
        lx %= (npc - 1);
      else {
        cout << 0 << endl;
        continue;
      }
      if (lx < 0)
        lx += npc - 1;
      cout << a.at(lx) + 1 << endl;
    } else {
      ll hx = highm + modhigh.at(i);
      hx %= npc + 1;
      if (hx < 0)
        hx += npc + 1;
      cout << a.at(hx) + 1 << endl;
    }
  }
}

Error: 0

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  vector<int> a(n, 0);
  for (int i = 1; i < n; i++) {
    int pc = __builtin_popcountll(i);
    a.at(i) = a.at(i % pc) + 1;
  }
  int npc = 0;
  for (int i = 0; i < n; i++) {
    if (x.at(i) == '1')
      npc++;
  }
  vector<int> modlow(n);
  vector<int> modhigh(n);
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1) {
      modlow.at(i) = 1;
      modhigh.at(i) = 1;
    } else {
      if (npc != 1)
        modlow.at(i) = (modlow.at(i + 1) * 2) % (npc - 1);
      else
        modlow.at(i) = 0;
      modhigh.at(i) = (modhigh.at(i + 1) * 2) % (npc + 1);
    }
  }

  for (int i = 0; i < n; i++) {
    string xc = x;
    if (xc.at(i) == '1') {
      xc.at(i) = '0';
      ll mods = 0;
      for (int j = 0; j < n; j++) {
        if (xc.at(j) == '1')
          mods += modlow.at(j);
      }
      mods %= (npc - 1);
      cout << a.at(mods) + 1 << endl;
    } else {
      xc.at(i) = '1';
      ll mods = 0;
      for (int j = 0; j < n; j++) {
        if (xc.at(j) == '1')
          mods += modhigh.at(j);
      }
      mods %= (npc + 1);
      cout << a.at(mods) + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
using ll = long long;

ll f(ll n) {
  if (n == 0) {
    return 0;
  }
  ll popcnt = __builtin_popcount(n);
  return 1 + f(n % popcnt);
}
int main() {
  ll n;
  string S;
  cin >> n >> S;
  ll popcnt = 0;
  for (ll i = 0; i < n; i++) {
    if (S[i] == '1') {
      popcnt++;
    }
  }
  vector<ll> MOD{popcnt - 1, popcnt + 1};
  if (MOD[0] <= 0) {
    MOD[0] = 1;
  }

  vector<ll> SM(2, 0);
  vector<vector<ll>> P2(2, vector<ll>(n, 0));
  for (ll i = 0; i < MOD.size(); i++) {
    P2[i][0] = 1;
    for (ll j = 1; j < n; j++) {
      P2[i][j] = (P2[i][j - 1] * 2) % MOD[i];
    }

    ll val = 0;
    for (ll j = 0; j < n; j++) {
      val = (val + (S[n - 1 - j] == '1' ? P2[i][j] : 0)) % MOD[i];
    }
    SM[i] = val;
  }
  // cerr << "popcnt=" << popcnt << " SM[0]=" << SM[0] << " SM[1]=" << SM[1] <<
  // endl;

  vector<ll> ANS(n, 0);
  for (ll i = 0; i < n; i++) {
    ll idx = (S[n - 1 - i] == '1' ? 0 : 1);
    ll p2 = P2[idx][i];
    ll val = (SM[idx] + (idx == 0 ? -p2 : p2)) % MOD[idx];
    while (val < 0) {
      val += MOD[idx];
    }
    assert(val >= 0);
    ANS[n - 1 - i] = (S[n - 1 - i] == '1' && popcnt == 1 ? 0 : 1 + f(val));
    // cerr << "i=" << (n-1-i) << " val=" << val << " S[i]=" << S[n-1-i] << "
    // P2[i]=" << p2 << " idx=" << idx << " ANS[i]=" << ANS[n-1-i] << endl;
  }
  ostringstream out;
  for (ll i = 0; i < n; i++) {
    out << ANS[i] << endl;
  }
  cout << out.str();
}


Error: 0

Fixed submission:

#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
using ll = long long;

ll f(ll n) {
  if (n == 0) {
    return 0;
  }
  ll popcnt = __builtin_popcount(n);
  return 1 + f(n % popcnt);
}
int main() {
  ll n;
  string S;
  cin >> n >> S;
  ll popcnt = 0;
  for (ll i = 0; i < n; i++) {
    if (S[i] == '1') {
      popcnt++;
    }
  }
  vector<ll> MOD{popcnt - 1, popcnt + 1};
  vector<ll> SM(2, 0);
  vector<vector<ll>> P2(2, vector<ll>(n, 0));
  for (ll i = 0; i < MOD.size(); i++) {
    P2[i][0] = 1;
    for (ll j = 1; j < n; j++) {
      P2[i][j] = (P2[i][j - 1] * 2) % MOD[i];
    }

    ll val = 0;
    for (ll j = 0; j < n; j++) {
      val = (val + (S[n - 1 - j] == '1' ? P2[i][j] : 0)) % MOD[i];
    }
    SM[i] = val;
  }
  // cerr << "popcnt=" << popcnt << " SM[0]=" << SM[0] << " SM[1]=" << SM[1] <<
  // endl;

  vector<ll> ANS(n, 0);
  for (ll i = 0; i < n; i++) {
    ll idx = (S[n - 1 - i] == '1' ? 0 : 1);
    ll p2 = P2[idx][i];
    ll val = (SM[idx] + (idx == 0 ? -p2 : p2)) % MOD[idx];
    while (val < 0) {
      val += MOD[idx];
    }
    assert(val >= 0);
    ANS[n - 1 - i] = 1 + f(val);
    // cerr << "i=" << (n-1-i) << " val=" << val << " S[i]=" << S[n-1-i] << "
    // P2[i]=" << p2 << " idx=" << idx << " ANS[i]=" << ANS[n-1-i] << endl;
  }
  ostringstream out;
  for (ll i = 0; i < n; i++) {
    out << ANS[i] << endl;
  }
  cout << out.str();
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll t = count(X.begin(), X.end(), '1');
  if (t == 0) {
    for (int i = 0; i < N; i++)
      cout << 1 << endl;
    return 0;
  }
  vector<ll> powl(N + 1, 1), pow(N + 1, 1), powu(N + 1, 1);
  for (int i = 1; i <= N; i++) {
    if (t > 1)
      powl[i] = powl[i - 1] * 2 % (t - 1);
    pow[i] = pow[i - 1] * 2 % (t);
    powu[i] = powu[i - 1] * 2 % (t + 1);
  }
  ll allu = 0, all = 0, alll = 0;
  for (int i = 0; i <= N - 1; i++) {
    if (X[N - 1 - i] == '1') {
      alll += powl[i];
      all += pow[i];
      allu += powu[i];
      if (t > 1)
        alll %= t - 1;
      all %= t;
      allu %= t + 1;
    }
  }

  for (int i = 0; i <= N - 1; i++) {
    ll cnt = 1;
    ll now;
    if (X[i] == '0') {
      now = (allu + powu[N - 1 - i]) % (t + 1);
    } else {
      if (t == 1) {
        cout << 0 << endl;
        continue;
      }
      now = (alll + (t - 1) - powl[N - 1 - i]) % (t - 1);
    }
    while (now > 0) {
      now %= __builtin_popcount(now);
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll t = count(X.begin(), X.end(), '1');
  vector<ll> powl(N + 1, 1), pow(N + 1, 1), powu(N + 1, 1);
  for (int i = 1; i <= N; i++) {
    if (t > 1)
      powl[i] = powl[i - 1] * 2 % (t - 1);
    pow[i] = pow[i - 1] * 2 % (t);
    powu[i] = powu[i - 1] * 2 % (t + 1);
  }
  ll allu = 0, all = 0, alll = 0;
  for (int i = 0; i <= N - 1; i++) {
    if (X[N - 1 - i] == '1') {
      alll += powl[i];
      all += pow[i];
      allu += powu[i];
      if (t > 1)
        alll %= t - 1;
      all %= t;
      allu %= t + 1;
    }
  }

  for (int i = 0; i <= N - 1; i++) {
    ll cnt = 1;
    ll now;
    if (X[i] == '0') {
      now = (allu + powu[N - 1 - i]) % (t + 1);
    } else {
      if (t == 1) {
        cout << 0 << endl;
        continue;
      }
      now = (alll + (t - 1) - powl[N - 1 - i]) % (t - 1);
    }
    while (now > 0) {
      now %= __builtin_popcount(now);
      cnt++;
    }
    cout << cnt << endl;
  }

  return 0;
}