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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

  string s;
  cin >> s;

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

  vector<int> am(n);
  vector<int> ap(n);
  int mw = c1 - 1;
  int pw = c1 + 1;

  bool special = false;
  if (mw == 0) {
    special = true;
    mw = 1;
  }
  am[0] = 1 % mw;
  ap[0] = 1 % pw;
  for (int i = 1; i < n; ++i) {
    am[i] = (am[i - 1] * 2) % mw;
    ap[i] = (ap[i - 1] * 2) % pw;
  }

  int bam = 0;
  int bap = 0;
  {
    int j = 0;
    for (int i = n - 1; 0 <= i; --i) {
      if (s[i] == '1') {
        bam = (bam + am[j]) % mw;
        bap = (bap + ap[j]) % pw;
      }
      ++j;
    }
  }

  {
    int j = n - 1;
    for (int i = 0; i < n; ++i) {
      int check;
      if (s[i] == '1') {
        if (special) {
          cout << 0 << endl;
          --j;
          continue;
        } else {
          check = bam - am[j];
          if (check < 0) {
            check += mw;
          }
        }
      } else {
        check = (bap + ap[j]) % pw;
      }

      int ret = 1;
      while (check != 0) {
        int temp = check;
        int count2 = 0;
        while (temp != 0) {
          if (temp % 2 == 1) {
            ++count2;
          }
          temp /= 2;
        }
        check = check % count2;
        ++ret;
      }

      cout << ret << endl;

      --j;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

  string s;
  cin >> s;

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

  vector<int> am(n);
  vector<int> ap(n);
  int mw = c1 - 1;
  int pw = c1 + 1;
  am[0] = 1 % mw;
  ap[0] = 1 % pw;
  for (int i = 1; i < n; ++i) {
    am[i] = (am[i - 1] * 2) % mw;
    ap[i] = (ap[i - 1] * 2) % pw;
  }

  int bam = 0;
  int bap = 0;
  {
    int j = 0;
    for (int i = n - 1; 0 <= i; --i) {
      if (s[i] == '1') {
        bam = (bam + am[j]) % mw;
        bap = (bap + ap[j]) % pw;
      }
      ++j;
    }
  }

  {
    int j = n - 1;
    for (int i = 0; i < n; ++i) {
      int check;
      if (s[i] == '1') {
        check = bam - am[j];
        if (check < 0) {
          check += mw;
        }
      } else {
        check = (bap + ap[j]) % pw;
      }

      int ret = 1;
      while (check != 0) {
        int temp = check;
        int count2 = 0;
        while (temp != 0) {
          if (temp % 2 == 1) {
            ++count2;
          }
          temp /= 2;
        }
        check = check % count2;
        ++ret;
      }

      cout << ret << endl;

      --j;
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

int solve(int64_t X) {
  int count = 0;
  while (X != 0) {
    X %= __builtin_popcountll(X);
    count++;
  }
  return count;
}

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

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

  vector<int64_t> remainder_plus(N, 0);
  vector<int64_t> remainder_minus(N, 0);
  {
    int64_t num = 1;
    for (int i = 0; i < N; i++) {
      remainder_plus[N - 1 - i] = num;
      num = (num << 1) % (popcount + 1);
    }
  }
  if (popcount > 1) {
    int64_t num = 1;
    for (int i = 0; i < N; i++) {
      remainder_minus[N - 1 - i] = num;
      num = (num << 1) % (popcount - 1);
    }
  }

  int64_t total_plus = 0;
  int64_t total_minus = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      total_plus += remainder_plus[i];
      total_minus += remainder_minus[i];
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      if (popcount > 1) {
        int64_t X =
            (total_minus - remainder_minus[i] + popcount - 1) % (popcount - 1);
        cout << solve(X) + 1 << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      int64_t X =
          (total_plus + remainder_plus[i] + popcount + 1) % (popcount + 1);
      cout << solve(X) + 1 << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int solve(int64_t X) {
  int count = 0;
  while (X != 0) {
    X %= __builtin_popcountll(X);
    count++;
  }
  return count;
}

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

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

  vector<int> remainder_plus(N, 0);
  vector<int> remainder_minus(N, 0);
  {
    int64_t num = 1;
    for (int i = 0; i < N; i++) {
      remainder_plus[N - 1 - i] = num;
      num = (num << 1) % (popcount + 1);
    }
  }
  if (popcount > 1) {
    int64_t num = 1;
    for (int i = 0; i < N; i++) {
      remainder_minus[N - 1 - i] = num;
      num = (num << 1) % (popcount - 1);
    }
  }

  int64_t total_plus = 0;
  int64_t total_minus = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      total_plus += remainder_plus[i];
      total_minus += remainder_minus[i];
    }
  }

  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      if (popcount > 0) {
        int64_t X =
            (total_minus - remainder_minus[i] + popcount - 1) % (popcount - 1);
        cout << solve(X) + 1 << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      int64_t X =
          (total_plus + remainder_plus[i] + popcount + 1) % (popcount + 1);
      cout << solve(X) + 1 << endl;
    }
  }

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

#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
int f(int x) {
  for (int ans = 0;; x %= __builtin_popcount(x), ans++) {
    if (x == 0)
      return ans;
  }
}
int a[200000], b[200000];
signed main() {
  int n;
  string x;
  cin >> n >> x;
  int popx = count(x.begin(), x.end(), '1');
  if (popx == 1) {
    if (x.back() == '1') {
      for (int i = 0; i < n - 1; i++)
        cout << 2 << endl;
      cout << 0 << endl;
    } else {
      for (int i = 0; i < n - 1; i++)
        cout << (x[i] == '1' ? 0 : 1) << endl;
      cout << 2 << endl;
    }
    return 0;
  }
  a[n - 1] = b[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    a[i] = a[i + 1] * 2 % (popx + 1);
    b[i] = b[i + 1] * 2 % (popx - 1);
  }
  int aa = 0, bb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      (aa += a[i]) %= popx + 1;
      (bb += b[i]) %= popx - 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cout << f((bb - b[i] + popx - 1) % (popx - 1)) + 1 << endl;
    else
      cout << f((aa + a[i]) % (popx + 1)) + 1 << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod = 1e9 + 7;
int f(int x) {
  for (int ans = 0;; x %= __builtin_popcount(x), ans++) {
    if (x == 0)
      return ans;
  }
}
int a[200000], b[200000];
signed main() {
  int n;
  string x;
  cin >> n >> x;
  int popx = count(x.begin(), x.end(), '1');
  if (popx == 1) {
    if (x.back() == '1') {
      for (int i = 0; i < n - 1; i++)
        cout << 2 << endl;
      cout << 0 << endl;
    } else {
      for (int i = 0; i < n - 1; i++)
        cout << (x[i] == '1' ? 0 : 1) << endl;
      cout << 2 << endl;
    }
  }
  a[n - 1] = b[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    a[i] = a[i + 1] * 2 % (popx + 1);
    b[i] = b[i + 1] * 2 % (popx - 1);
  }
  int aa = 0, bb = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1') {
      (aa += a[i]) %= popx + 1;
      (bb += b[i]) %= popx - 1;
    }
  }
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cout << f((bb - b[i] + popx - 1) % (popx - 1)) + 1 << endl;
    else
      cout << f((aa + a[i]) % (popx + 1)) + 1 << 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 ll = long long;
using P = pair<int, int>;

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

// 再帰関数によって操作回数をカウント
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % popcount(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  rep(i, n) x[i] = s[i] - '0';
  int pc = 0;
  rep(i, n) pc += x[i];
  vector<int> ans(n);
  // popcountが増える場合と減る場合で分けて考える
  rep(b, 2) {
    // 新しいpopcount
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    // npcが０以下の場合の例外処理
    if (npc <= 0)
      continue;

    // 元のXに対して操作を行った結果を保持
    int r0 = 0;
    rep(i, n) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    // 下の桁から順にXiに対する操作結果を調べる．
    // 各桁ごとにk=2^n%npcを計算してr0と組み合わせて答えを算出．
    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;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}

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 popcount(int x) { return __builtin_popcount(x); }

// 再帰関数によって操作回数をカウント
int f(int x) {
  if (x == 0)
    return 0;
  return f(x % popcount(x)) + 1;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  rep(i, n) x[i] = s[i] - '0';
  int pc = 0;
  rep(i, n) pc += x[i];
  vector<int> ans(n);
  // popcountが増える場合と減る場合で分けて考える
  rep(b, 2) {
    // 新しいpopcount
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    // npcが０以下の場合の例外処理
    // if (npc <= 0) continue;

    // 元のXに対して操作を行った結果を保持
    int r0 = 0;
    rep(i, n) {
      r0 = (r0 * 2) % npc;
      r0 += x[i];
    }
    // 下の桁から順にXiに対する操作結果を調べる．
    // 各桁ごとにk=2^n%npcを計算してr0と組み合わせて答えを算出．
    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;
    }
  }
  rep(i, n) cout << ans[i] << 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;

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

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  REP(i, n) x[i] = s[i] - '0';

  int pc = 0; // pc == pop_count
  REP(i, n) pc += x[i];

  vector<int> res(n);
  // 元の bit が 0 のところを1個だけ 1 にした場合と
  // 元の bit が 1 のところを1個だけ 0 にした場合とに分けて考えてる
  REP(bit, 2) {
    // 元の bit の popcount の変動をセットする
    int next_pc = pc;
    if (bit == 0)
      next_pc++;
    else
      next_pc--;
    // next_pc が 0 以下になった場合の例外処理
    if (next_pc <= 0)
      continue;
    // 元の数を next_pc で割った余り(== rem0)を求める
    // これで1段回目が終わる
    int rem0 = 0;
    REP(i, n) {
      // 2進数を前から順番に見ていって10進数にしている
      rem0 = (rem0 * 2) % next_pc;
      rem0 += x[i];
    }
    // 1段回目が終わった後は，
    // 差分を足して，
    // その数を next_pc で割った余りを求める
    int k = 1;
    for (int i = n - 1; i >= 0; i--) {
      // i 桁目をフリップしたときの割った余りを求める
      if (x[i] == bit) {
        int rem = rem0;
        // i 番目の bit が 0 の時は，
        // i 番目の bit が 1 に変わるので，
        // 2^i を [next_pc で割った余り]に足してあげて，
        // next_pc で割った余りを求める
        if (bit == 0)
          rem = (rem + k) % next_pc;
        // i 番目の bit が 1 の時は，
        // i 番目の bit が 0 に変わるので，
        // 2^i を [next_pc で割った余り]から引いてあげて，
        // next_pc で割った余りを求める
        else
          rem = (rem - k + next_pc) % next_pc;
        // そして，popcount を求める
        res[i] = f(rem) + 1;
      }
      // 2^i の値
      k = (k * 2) % next_pc;
    }
  }

  REP(i, n)
  cout << res[i] << '\\n';
  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;

uint64_t popcnt(uint64_t n) {
  uint64_t c = 0;
  c = (n & 0x5555555555555555) + ((n >> 1) & 0x5555555555555555);
  c = (c & 0x3333333333333333) + ((c >> 2) & 0x3333333333333333);
  c = (c & 0x0f0f0f0f0f0f0f0f) + ((c >> 4) & 0x0f0f0f0f0f0f0f0f);
  c = (c & 0x00ff00ff00ff00ff) + ((c >> 8) & 0x00ff00ff00ff00ff);
  c = (c & 0x0000ffff0000ffff) + ((c >> 16) & 0x0000ffff0000ffff);
  c = (c & 0x00000000ffffffff) + ((c >> 32) & 0x00000000ffffffff);
  return (c);
}

int main() {
  cin.tie(0)->sync_with_stdio(false);

  int n;
  cin >> n;
  string X;
  cin >> X;

  REP(i, n) {
    string t = X;
    if (X[i] == '0')
      t[i] = '1';
    else
      t[i] = '0';
    uint64_t x = stol(t, nullptr, 2);
    ll cnt = 0;
    while (x) {
      uint64_t cnt1 = popcnt(x);
      x %= cnt1;
      cnt++;
    }
    cout << cnt << '\\n';
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF int(1e6 + 1)
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int ans(int x, int count) {
  int t = 0;
  int c = count;
  int xi = x;
  while (xi > 0) {
    if (xi % 2)
      t++;
    xi = (xi - xi % 2) / 2;
  }
  x = x % t;
  c++;
  if (x != 0)
    return ans(x, c);
  else
    return c;
}

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

  int count = 0;
  char c;
  vector<int> a(n);
  rep(i, 0, n) {
    cin >> c;
    a[i] = c - '0';
    if (a[i] == 1)
      count++;
  }
  int npop = 0, ppop = 0;
  rep(i, 0, n) {
    if (count > 1)
      npop = (npop * 2 + a[i]) % (count - 1);
    ppop = (ppop * 2 + a[i]) % (count + 1);
  }

  vector<int> np(n), pp(n);
  np[n - 1] = 1;
  pp[n - 1] = 1;
  for (int i = n - 1; i != 0; i--) {
    if (count > 1)
      np[i - 1] = (np[i] * 2) % (count - 1);
    pp[i - 1] = (pp[i] * 2) % (count + 1);
  }
  rep(i, 0, n) {
    int x, X = 1;
    if (a[i] == 0) {
      x = (pp[i] + ppop) % (count + 1);
    } else if (count > 1) {
      x = (count - 1 - np[i] + npop) % (count - 1);
    } else {
      X = 0;
      x = 0;
    }
    if (x == 0)
      cout << X << endl;
    else
      cout << ans(x, X) << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF int(1e6 + 1)
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int ans(int x, int count) {
  int t = 0;
  int c = count;
  int xi = x;
  while (xi > 0) {
    if (xi % 2)
      t++;
    xi = (xi - xi % 2) / 2;
  }
  x = x % t;
  c++;
  if (x != 0)
    return ans(x, c);
  else
    return c;
}

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

  int count = 0;
  char c;
  vector<int> a(n);
  rep(i, 0, n) {
    cin >> c;
    a[i] = c - '0';
    if (a[i] == 1)
      count++;
  }
  int npop = 0, ppop = 0;
  rep(i, 0, n) {
    if (count > 0)
      npop = (npop * 2 + a[i]) % (count - 1);
    ppop = (ppop * 2 + a[i]) % (count + 1);
  }

  vector<int> np(n), pp(n);
  np[n - 1] = 1;
  pp[n - 1] = 1;
  for (int i = n - 1; i != 0; i--) {
    np[i - 1] = (np[i] * 2) % (count - 1);
    pp[i - 1] = (pp[i] * 2) % (count + 1);
  }
  rep(i, 0, n) {
    int x, X = 1;
    if (a[i] == 0) {
      x = (pp[i] + ppop) % (count + 1);
    } else {
      x = (count - 1 - np[i] + npop) % (count - 1);
    }
    if (x == 0)
      cout << X << endl;
    else
      cout << ans(x, X) << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
using namespace std;

ll modPow(ll a, ll n, ll MOD) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % MOD;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, MOD)) % MOD;
  ll t = modPow(a, n / 2, MOD);
  return (t * t) % MOD;
}

int main() {
  ll n;
  string x;
  cin >> n;
  cin >> x;
  ll one = 0;
  vector<ll> kt1(n), kt2(n);
  ll sum1 = 0, sum2 = 0;
  rep(i, n) if (x[i] == '1') one++;
  rep(i, n) {
    kt1[i] = modPow(2, n - i - 1, one + 1);
    if (one != 0 && one != 1)
      kt2[i] = modPow(2, n - i - 1, one - 1);
    if (x[i] == '1') {
      sum1 = (sum1 + kt1[i]) % (one + 1);
      if (one != 0 && one != 1)
        sum2 = (sum2 + kt2[i]) % (one - 1);
    }
  }

  rep(i, n) {
    ll num;
    ll ans = 1;
    if (x[i] == '0')
      num = (sum1 + kt1[i]) % (one + 1);
    else if (one == 1 && x[i] == '1') {
      num = 0;
      ans = 0;
    } else
      num = (sum2 - kt2[i] + one - 1) % (one - 1);
    while (num != 0) {
      ans++;
      ll temp = num;
      ll count = 0;
      while (temp != 0) {
        if (temp % 2 == 1)
          count++;
        temp /= 2;
      }
      num %= count;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
using namespace std;

ll modPow(ll a, ll n, ll MOD) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % MOD;
  if (n % 2 == 1)
    return (a * modPow(a, n - 1, MOD)) % MOD;
  ll t = modPow(a, n / 2, MOD);
  return (t * t) % MOD;
}

int main() {
  int n;
  string x;
  cin >> n;
  cin >> x;
  int one = 0;
  vector<int> kt1(n), kt2(n);
  int sum1 = 0, sum2 = 0;
  rep(i, n) if (x[i] == '1') one++;
  rep(i, n) {
    kt1[i] = modPow(2, n - i - 1, one + 1);
    if (one != 0)
      kt2[i] = modPow(2, n - i - 1, one - 1);
    if (x[i] == '1') {
      sum1 = (sum1 + kt1[i]) % (one + 1);
      if (one != 0)
        sum2 = (sum2 + kt2[i]) % (one - 1);
    }
  }
  rep(i, n) {
    int num;
    if (x[i] == '0')
      num = (sum1 + kt1[i]) % (one + 1);
    else
      num = (sum2 - kt2[i] + one - 1) % (one - 1);
    int ans = 1;
    while (num != 0) {
      ans++;
      int temp = num;
      int count = 0;
      while (temp != 0) {
        if (temp % 2 == 1)
          count++;
        temp /= 2;
      }
      num %= count;
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long ll;

constexpr long long INFLL = 1e18;

using namespace std;

int f(int temp) {
  if (temp == 0) {
    return 0;
  } else {
    return f(temp % __builtin_popcount(temp)) + 1;
  }
}

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  int pc = 0;
  for (int i = 0; i < n; i++) {
    x.at(i) = s.at(i) - '0';
    if (x.at(i)) {
      pc++;
    }
  }

  vector<int> ans(n);

  for (int i = 0; i < 2; i++) {
    int pc2 = pc;
    if (i == 0) {
      pc2++;
    } else {
      pc2--;
    }
    if (pc2 <= 0) {
      continue;
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum = (sum * 2) % pc2;
      sum += x.at(j);
    }
    int temp = 1;
    int temp2;
    for (int j = n - 1; j >= 0; j--) {
      if (i == 0) {
        if (x.at(j) == 0) {
          temp2 = (sum + temp) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      } else {
        if (x.at(j) == 1) {
          temp2 = (sum - temp + pc2) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      }
      temp = (temp * 2) % pc2;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans.at(i) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <vector>

typedef long long ll;

constexpr long long INFLL = 1e18;

using namespace std;

int f(int temp) {
  if (temp == 0) {
    return 0;
  } else {
    return f(temp % __builtin_popcount(temp)) + 1;
  }
}

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  int pc = 0;
  for (int i = 0; i < n; i++) {
    x.at(i) = s.at(i) - '0';
    if (x.at(i)) {
      pc++;
    }
  }

  vector<int> ans(n);

  for (int i = 0; i < 2; i++) {
    int pc2 = pc;
    if (i == 0) {
      pc2++;
    } else {
      pc2--;
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      sum = (sum * 2) % pc2;
      sum += x.at(j);
    }
    int temp = 1;
    int temp2;
    for (int j = n - 1; j >= 0; j--) {
      if (i == 0) {
        if (x.at(j) == 0) {
          temp2 = (sum + temp) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      } else {
        if (x.at(j) == 1) {
          temp2 = (sum - temp + pc2) % pc2;
          ans.at(j) = f(temp2) + 1;
        }
      }
      temp = (temp * 2) % pc2;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans.at(i) << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;
typedef long long llong;

#define MAX 200001

int n;
string s;
llong pwm[MAX], pwp[MAX];
// memo化用変数
int mem[MAX];

// カウントアップする関数
int get(int x) {
  // memo化
  if (mem[x] != -1)
    return mem[x];
  // 0なら終了
  if (x == 0)
    return 0;
  // 再起で繰り返すたびにカウントアップ
  return mem[x] = 1 + get(x % __builtin_popcount(x));
}

int main() {
  cin >> n >> s;
  int cnt = count(s.begin(), s.end(), '1');
  int m = cnt - 1, p = cnt + 1;
  // popcount が0なら
  // 反転してpopcountが１のｎ桁で必ず割り切れるので
  // 桁数分1を出して終了
  if (m == -1) {
    for (int i = 0; i < n; i++)
      cout << 1 << endl;
    return 0;
  }
  // 2^nのあまりを計算しとく
  //
  pwm[0] = 1;
  pwp[0] = 1;
  for (int i = 1; i <= n; i++) {
    pwp[i] = (2 * pwp[i - 1]) % p;
    // m = 0ならpwmは０の除算になるので０にする
    if (m)
      pwm[i] = (2 * pwm[i - 1]) % m;
    else
      pwm[i] = 0;
  }
  llong pv = 0, mv = 0;
  for (int i = 0; i < n; i++) {
    // += 2^n * bit
    pv += pwp[n - i - 1] * (s[i] == '1');
    // 余りを求める　a+b=a'+b'の性質を利用
    pv %= p;
    if (m) {
      mv += pwm[n - i - 1] * (s[i] == '1');
      mv %= m;
    }
  }
  memset(mem, -1, sizeof mem);
  for (int i = 0; i < n; i++) {
    // 1を反転するなら
    if (s[i] == '1') {
      // s[i]が１でmが０なら全桁０になるので、計算しない
      if (!m)
        cout << 0 << endl;
      else
        cout << 1 + get((mv - pwm[n - i - 1] + m) % m) << endl;
    }
    // 0を反転するなら
    else {
      cout << 1 + get((pv + pwp[n - i - 1]) % p) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <stdio.h>

typedef long long llong;

struct Data {
  llong value;
  long popcount;
};

long getPopcount(long l) {
  long popcount = 0;
  while (l > 0) {
    if (l & 1)
      popcount++;
    l = l >> 1;
  }
  return popcount;
}
long f(Data d) {
  long res = 0;
  while (d.value > 0) {
    d.value = d.value % d.popcount;
    d.popcount = getPopcount(d.value);
    res++;
  }
  return res;
}

llong calcMod(llong m, llong rem, llong bit) {
  rem = rem << 1;
  rem += bit;
  rem %= m;
  return rem;
}

int main() {
  int n;
  int A[200000];
  llong pc = 0, pc_p, pc_n;
  llong sm1 = 0, sm2 = 0;

  scanf("%d", &n);

  for (int i = 0; i < n; i++) {
    scanf("%1d", &A[i]);
    pc += A[i];
  }

  pc_n = pc + 1;
  pc_p = pc - 1;
  for (llong i = 0; i < n; i++) {
    sm1 = calcMod(pc_p, sm1, A[i]);
    sm2 = calcMod(pc_n, sm2, A[i]);
  }

  for (llong i = 0; i < n; i++) {
    llong m = n - i;
    llong sm = 0;
    if (A[i]) {
      if (n == 1 && A[0] == 0) {
        printf("0\\n");
        return 0;
      }
      for (llong j = 0; j < m; j++) {
        sm = calcMod(pc_p, sm, j == 0 ? 1 : 0);
      }
      sm = sm1 - sm;
      if (sm < 0)
        sm = -sm;
      sm %= pc_p;
    } else {
      for (llong j = 0; j < m; j++) {
        sm = calcMod(pc_n, sm, j == 0 ? 1 : 0);
      }
      sm = sm2 + sm;
      sm %= pc_n;
    }
    if (sm == 0) {
      printf("1\\n");
      continue;
    }
    Data d;
    d.value = sm;
    d.popcount = getPopcount(sm);
    printf("%d\\n", f(d) + 1);
  }

  return 0;
}