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

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

const int MAXN = 1e6 + 7;
int dp[MAXN];
// LL prefix[MAXN], suffix[MAXN];
int f[MAXN];

int N, now;
string X;
void solve(char c) {
  int mod;
  if (c == '1') {
    if (now == 0)
      return;
    mod = now - 1;
  } else {
    if (now == N)
      return;
    mod = now + 1;
  }

  if (mod == 0) {
    assert(c == '1');
    for (int i = 1; i <= N; i++) {
      if (X[i - 1] == '1') {
        f[i] = 0;
      }
    }
    return;
  }

  //    cout << c << " mod " << mod << endl;

  int total = 0;
  for (int i = N, pw = 1; i > 0; i--, pw = (pw + pw) % mod) {
    if (X[i - 1] == '1')
      total = (total + pw) % mod;
  }

  for (int i = N, pw = 1; i > 0; i--, pw = (pw + pw) % mod) {
    if (X[i - 1] != c)
      continue;
    int tmp = total;
    if (c == '1')
      tmp = (tmp + mod - pw) % mod;
    else
      tmp = (tmp + pw) % mod;
    f[i] = dp[tmp] + 1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  for (int i = 1; i < MAXN; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }

  cin >> N >> X;
  now = 0;
  for (char c : X)
    now += c == '1';

  solve('0');
  solve('1');

  for (int i = 1; i <= N; i++)
    cout << f[i] << "\\n";

  return 0;
}


Error: 0

Fixed submission:

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

const int MAXN = 2e5 + 7;
int dp[MAXN];
LL prefix[MAXN], suffix[MAXN];
int f[MAXN];

int N, now;
string X;
void solve(char c) {
  int mod;
  if (c == '1') {
    if (now == 0)
      return;
    mod = now - 1;
  } else {
    if (now == N)
      return;
    mod = now + 1;
  }

  //    cout << c << " mod " << mod << endl;

  for (int i = 1; i <= N; i++) {
    prefix[i] = (prefix[i] * 2 + X[i - 1] - '0') % mod;
  }
  for (int i = N, pw = 1; i > 0; i--, pw = (pw + pw) % mod) {
    suffix[i] = suffix[i + 1];
    if (X[i - 1] == '1')
      suffix[i] = (suffix[i] + pw) % mod;
  }

  for (int i = N, pw = 1; i > 0; i--, pw = (pw + pw) % mod) {
    if (X[i - 1] != c)
      continue;
    int cur = X[i - 1] - '0';
    cur ^= 1;
    LL tmp = suffix[i + 1];
    if (cur)
      tmp = (tmp + pw) % mod;
    tmp = (tmp + pw * 2 * prefix[i - 1]) % mod;
    //        cout << i << " -> " << tmp << endl;
    f[i] = dp[tmp] + 1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  for (int i = 1; i < MAXN; i++) {
    dp[i] = dp[i % __builtin_popcount(i)] + 1;
  }

  cin >> N >> X;
  now = 0;
  for (char c : X)
    now += c == '1';

  solve('0');
  solve('1');

  for (int i = 1; i <= N; i++)
    cout << f[i] << "\\n";

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

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

const int N = 2e5 + 10;

int dp[N];
int n, c;
char ss[N];
long long pw[N], sum, ans[N];

void solve(int c, int t) {
  if (c == 0)
    return;

  sum = 0;
  pw[0] = 1;
  for (int i = 1; i <= n; i++) {
    pw[i] = (pw[i - 1] * 2) % c;
    sum = (sum * 2 + (ss[i] - '0')) % c;
  }
  if (t == 1) {
    for (int i = 1; i <= n; i++)
      if (ss[i] == '0') {
        long long x = (sum + pw[n - i]) % c;
        ans[i] = dp[x] + 1;
      }
  } else {
    for (int i = 1; i <= n; i++)
      if (ss[i] == '1') {
        long long x = (sum - pw[n - i] + c) % c;
        ans[i] = dp[x] + 1;
      }
  }
}

int main(void) {
  for (int i = 1; i < N; i++)
    dp[i] = dp[i % __builtin_popcount(i)] + 1;

  scanf("%d", &n);
  scanf("%s", ss + 1);
  for (int i = 1; i <= n; i++)
    c += (ss[i] - '0');

  if (c == 0) {
    for (int i = 1; i <= n; i++)
      puts("1");
  } else {
    solve(c + 1, 1);
    solve(c - 1, 0);
    for (int i = 1; i <= n; i++)
      printf("%lld\\n", ans[i]);
  }
}

Error: 0

Fixed submission:

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

const int N = 2e5 + 10;

int dp[N];
int n, c;
char ss[N];
long long pw[N], sum, ans[N];

void solve(int c, int t) {
  sum = 0;
  pw[0] = 1;
  for (int i = 1; i <= n; i++) {
    pw[i] = (pw[i - 1] * 2) % c;
    sum = (sum * 2 + (ss[i] - '0')) % c;
  }
  if (t == 1) {
    for (int i = 1; i <= n; i++)
      if (ss[i] == '0') {
        long long x = (sum + pw[n - i]) % c;
        ans[i] = dp[x] + 1;
      }
  } else {
    if (c == 0)
      return;
    for (int i = 1; i <= n; i++)
      if (ss[i] == '1') {
        long long x = (sum - pw[n - i] + c) % c;
        ans[i] = dp[x] + 1;
      }
  }
}

int main(void) {
  for (int i = 1; i < N; i++)
    dp[i] = dp[i % __builtin_popcount(i)] + 1;

  scanf("%d", &n);
  scanf("%s", ss + 1);
  for (int i = 1; i <= n; i++)
    c += (ss[i] - '0');

  if (c == 0) {
    for (int i = 1; i <= n; i++)
      puts("1");
  } else {
    solve(c + 1, 1);
    solve(c - 1, 0);
    for (int i = 1; i <= n; i++)
      printf("%lld\\n", ans[i]);
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int ans[N];
int pow(ll x, int y, int mod) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = ans * x % mod;
    x = x * x % mod;
  }
  return ans;
}
ll x1[N], x2[N];
void init() {
  for (int i = 1; i < N; i++) {
    int y = i, z = 0;
    while (y) {
      if (y & 1)
        z++;
      y >>= 1;
    }
    ans[i] = ans[i % z] + 1;
  }
}

int main() {
  init();
  int n;
  string s;
  cin >> n;
  cin >> s;
  int cnt = 0;
  ll sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  x1[0] = x2[0] = 1;
  for (int i = 1; i < n; i++) {
    if (cnt - 1 > 0)
      x1[i] = x1[i - 1] * 2 % (cnt - 1);
    x2[i] = x2[i - 1] * 2 % (cnt + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt - 1 > 0)
        sum1 = (sum1 + x1[n - i - 1]) % (cnt - 1);
      sum2 = (sum2 + x2[n - i - 1]) % (cnt + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    int sum;
    if (s[i] == '1') {
      if (cnt - 1 > 0)
        sum = (sum1 - x1[n - i - 1] + cnt - 1) % (cnt - 1);
      else {
        cout << "0\\n";
        continue;
      }
    } else {
      sum = (sum2 + x2[n - i - 1]) % (cnt + 1);
    }
    cout << ans[sum] + 1 << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;
typedef long long ll;
const int N = 2e5 + 5;
int ans[N];
int pow(ll x, int y, int mod) {
  ll ans = 1;
  while (y) {
    if (y & 1)
      ans = ans * x % mod;
    x = x * x % mod;
  }
  return ans;
}
ll x1[N], x2[N];
void init() {
  for (int i = 1; i < N; i++) {
    int y = i, z = 0;
    while (y) {
      if (y & 1)
        z++;
      y >>= 1;
    }
    ans[i] = ans[i % z] + 1;
  }
}

int main() {
  init();
  int n;
  string s;
  cin >> n;
  cin >> s;
  int cnt = 0;
  ll sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  x1[0] = x2[0] = 1;
  for (int i = 1; i < n; i++) {
    x1[i] = x1[i - 1] * 2 % (cnt - 1);
    x2[i] = x2[i - 1] * 2 % (cnt + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      sum1 = (sum1 + x1[n - i - 1]) % (cnt - 1);
      sum2 = (sum2 + x2[n - i - 1]) % (cnt + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    int sum;
    if (s[i] == '1') {
      sum = (sum1 - x1[n - i - 1] + cnt - 1) % (cnt - 1);

    } else {
      sum = (sum2 + x2[n - i - 1]) % (cnt + 1);
    }
    cout << ans[sum] + 1 << "\\n";
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

vector<long long> f(200001);

long long power(long long x, long long y, long long mod) {
  long long ret = 1;
  while (y > 0) {
    if (y & 1) {
      ret = (ret * x) % mod;
    }
    x = (x * x) % mod;
    y >>= 1;
  }
  return ret;
}

long long func(long long count, long long x) {
  if (count == 0) {
    return 1;
  } else if (x % count == 0) {
    return 1;
  } else {
    return 1 + f[x % count];
  }
}

int main(void) {
  for (long long i = 1; i < 200001; i++) {
    long long count = 0;
    long long X = i;
    while (X > 0) {
      if (X & 1) {
        count++;
      }
      X >>= 1;
    }
    f[i] = func(count, i);
  }
  long long N;
  cin >> N;
  string X;
  cin >> X;
  long long count = 0;
  for (long long i = 0; i < X.size(); i++) {
    if (X[i] == '1') {
      count++;
    }
  }
  long long num1 = 0;
  for (long long j = 0; j < X.size(); j++) {
    if (X[j] == '1' && count != 1) {
      num1 += power(2, X.size() - 1 - j, count - 1);
    }
  }
  long long num0 = 0;
  for (long long j = 0; j < X.size(); j++) {
    if (X[j] == '1') {
      num0 += power(2, X.size() - 1 - j, count + 1);
    }
  }
  for (long long i = 0; i < X.size(); i++) {
    long long answer;
    if (X[i] == '1') {
      if (count - 1 == 0) {
        answer = 0;
      } else {
        answer = func(count - 1, num1 - power(2, X.size() - 1 - i, count - 1));
      }
    } else {

      answer = func(count + 1, num0 + power(2, X.size() - 1 - i, count + 1));
    }
    cout << answer << endl;
  }
}

Error: 0

Fixed submission:

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

vector<long long> f(200001);

long long power(long long x, long long y, long long mod) {
  long long ret = 1;
  while (y > 0) {
    if (y & 1) {
      ret = (ret * x) % mod;
    }
    x = (x * x) % mod;
    y >>= 1;
  }
  return ret;
}

long long func(long long count, long long x) {
  if (count == 0) {
    return 1;
  } else if (x % count == 0) {
    return 1;
  } else {
    return 1 + f[x % count];
  }
}

int main(void) {
  for (long long i = 1; i < 200001; i++) {
    long long count = 0;
    long long X = i;
    while (X > 0) {
      if (X & 1) {
        count++;
      }
      X >>= 1;
    }
    f[i] = func(count, i);
  }
  long long N;
  cin >> N;
  string X;
  cin >> X;
  long long count = 0;
  for (long long i = 0; i < X.size(); i++) {
    if (X[i] == '1') {
      count++;
    }
  }
  long long num1 = 0;
  for (long long j = 0; j < X.size(); j++) {
    if (X[j] == '1') {
      num1 += power(2, X.size() - 1 - j, count - 1);
    }
  }
  long long num0 = 0;
  for (long long j = 0; j < X.size(); j++) {
    if (X[j] == '1') {
      num0 += power(2, X.size() - 1 - j, count + 1);
    }
  }
  for (long long i = 0; i < X.size(); i++) {
    long long answer;
    if (X[i] == '1') {
      answer = func(count - 1, num1 - power(2, X.size() - 1 - i, count - 1));
    } else {

      answer = func(count + 1, num0 + power(2, X.size() - 1 - i, count + 1));
    }
    cout << answer << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  lint N;
  cin >> N;

  string X;
  cin >> X;

  lint P = count(X.begin(), X.end(), '1');
  lint pp = P + 1;
  lint pn = P - 1;

  lint XP = 0, XN = 0;
  string RX = X;
  reverse(RX.begin(), RX.end());
  lint powp = 1;
  lint pown = 1;
  for (lint i = 0; i < N; i++) {
    XP += (RX[i] - '0') * powp;
    XP %= pp;
    powp = powp * 2 % pp;

    if (pn == 0)
      continue;
    XN += (RX[i] - '0') * pown;
    XN %= pn;
    pown = pown * 2 % pn;
  }

  vector<lint> ans;
  powp = pown = 1;
  for (lint i = 0; i < N; i++) {
    lint counter = 1;
    lint xi;
    if (RX[i] == '0') {
      xi = (XP + powp) % pp;
    } else {
      if (pn == 0) {
        ans.push_back(0);
        continue;
      }
      xi = (XN - pown) % pn;
      if (xi < 0)
        xi += pn;
    }
    while (xi) {
      xi = xi % __builtin_popcount(xi);
      counter++;
    }
    ans.push_back(counter);
    powp = powp * 2 % pp;
    if (pn)
      pown = pown * 2 % pn;
  }
  reverse(ans.begin(), ans.end());

  for (auto e : ans) {
    cout << e << endl;
  }
}

Error: 0

Fixed submission:

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

int main() {
  lint N;
  cin >> N;

  string X;
  cin >> X;

  lint P = count(X.begin(), X.end(), '1');
  lint pp = P + 1;
  lint pn = P - 1;

  lint XP = 0, XN = 0;
  string RX = X;
  reverse(RX.begin(), RX.end());
  lint powp = 1;
  lint pown = 1;
  for (lint i = 0; i < N; i++) {
    XP += (RX[i] - '0') * powp;
    XP %= pp;
    powp = powp * 2 % pp;

    XN += (RX[i] - '0') * pown;
    XN %= pn;
    pown = pown * 2 % pn;
  }

  vector<lint> ans;
  powp = pown = 1;
  for (lint i = 0; i < N; i++) {
    lint counter = 1;
    lint xi;
    if (RX[i] == '0') {
      xi = (XP + powp) % pp;
    } else {
      xi = (XN - pown) % pn;
      if (xi < 0)
        xi += pn;
    }
    while (xi) {
      xi = xi % __builtin_popcount(xi);
      counter++;
    }
    ans.push_back(counter);
    powp = powp * 2 % pp;
    pown = pown * 2 % pn;
  }
  reverse(ans.begin(), ans.end());

  for (auto e : ans) {
    cout << e << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

// 繰り返し二乗法
ll EX(ll x, ll n, ll bit) {
  if (n == 0)
    return 1;
  else if (n % 2 == 1)
    return EX(x, n - 1, bit) * x % bit;
  else
    return EX(x * x % bit, n / 2, bit) % bit;
}

ll solve(ll now, ll bit, ll ans = 0) {
  now %= bit;
  bit = __builtin_popcount(now);
  if (now == 0)
    return ans + 1;
  else {
    ans++;
    return solve(now, bit, ans);
  }
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bit = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      bit++;
  }
  ll m = 0, p = 0;
  for (int i = 0; i < n; i++) {
    if (bit == 1)
      break;
    if (s[i] == '1')
      m += EX(2, n - i - 1, bit - 1);
    m %= bit - 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      p += EX(2, n - i - 1, bit + 1);
    p %= bit + 1;
  }
  vector<ll> ans(n);
  for (int i = 0; i < n; i++) {
    string t = s;
    bool mai = true;
    if (t[i] == '1') {
      t[i] = '0';
    } else {
      t[i] = '1';
      mai = false;
    }
    if (mai) {
      if (bit == 1) {
        ans[i] = 0;
      } else
        ans[i] = solve((m - EX(2, n - i - 1, bit - 1)) % (bit - 1) + bit - 1,
                       bit - 1);
    } else {
      ans[i] =
          solve((p + EX(2, n - i - 1, bit + 1)) % (bit + 1) + bit + 1, bit + 1);
    }
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
const long long INF = 1e15;

// 繰り返し二乗法
ll EX(ll x, ll n, ll bit) {
  if (n == 0)
    return 1;
  else if (n % 2 == 1)
    return EX(x, n - 1, bit) * x % bit;
  else
    return EX(x * x % bit, n / 2, bit) % bit;
}

ll solve(ll now, ll bit, ll ans = 0) {
  now %= bit;
  bit = __builtin_popcount(now);
  if (now == 0)
    return ans + 1;
  else {
    ans++;
    return solve(now, bit, ans);
  }
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int bit = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      bit++;
  }
  cout << "OK" << endl;
  ll m = 0, p = 0; // あまり
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      m += EX(2, n - i - 1, bit - 1);
    m %= bit - 1;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      p += EX(2, n - i - 1, bit + 1);
    p %= bit + 1;
  }
  // cout << "OK" << endl;
  // cout << m << " " << p << endl;
  vector<ll> ans(n);
  for (int i = 0; i < n; i++) {
    string t = s;
    bool mai = true;
    if (t[i] == '1') {
      t[i] = '0';
    } else {
      t[i] = '1';
      mai = false;
    }
    if (mai) {
      ans[i] =
          solve((m - EX(2, n - i - 1, bit - 1)) % (bit - 1) + bit - 1, bit - 1);
    } else {
      ans[i] =
          solve((p + EX(2, n - i - 1, bit + 1)) % (bit + 1) + bit + 1, bit + 1);
    }
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << endl;
  // cout << solve(7,bit+1) << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  vector<int> f(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    int c = 0;
    for (int j = 1; j <= i; j <<= 1) {
      if ((i & j) > 0) {
        c++;
      }
    }
    f[i] = f[i % c] + 1;
  }

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

  vector<int> res(n);
  if (p == 0) {
    for (int i = 0; i < n; i++) {
      res[i] = 1;
    }
  } else if (p == 1) {
    int q = 0;
    for (int i = 0, j = 1; i < n; i++, j <<= 1, j %= (p + 1)) {
      if (x[n - i - 1] == '1') {
        q = (q + j) % (p + 1);
      }
    }

    for (int i = 0, j = 1; i < n; i++, j <<= 1, j %= (p + 1)) {
      if (x[n - i - 1] == '0') {
        res[n - i - 1] = f[(q + j) % (p + 1)] + 1;
      } else {
        res[n - i - 1] = 0;
      }
    }
  } else {
    int q = 0, r = 0;
    for (int i = 0, j = 1, k = 1; i < n;
         i++, j <<= 1, j %= (p + 1), k <<= 1, k %= (p - 1)) {
      if (x[n - i - 1] == '1') {
        q = (q + j) % (p + 1);
        r = (r + k) % (p - 1);
      }
    }

    for (int i = 0, j = 1, k = 1; i < n;
         i++, j <<= 1, j %= (p + 1), k <<= 1, k %= (p - 1)) {
      if (x[n - i - 1] == '0') {
        res[n - i - 1] = f[(q + j) % (p + 1)] + 1;
      } else {
        res[n - i - 1] = f[(r - k + p - 1) % (p - 1)] + 1;
      }
    }
  }

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


Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

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

  vector<int> f(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    int c = 0;
    for (int j = 1; j <= i; j <<= 1) {
      if ((i & j) > 0) {
        c++;
      }
    }
    f[i] = f[i % c] + 1;
  }

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

  int q = 0, r = 0;
  for (int i = 0, j = 1, k = 1; i < n;
       i++, j <<= 1, j %= (p - 1), k <<= 1, k %= (p + 1)) {
    if (x[n - i - 1] == '1') {
      q = (q + j) % (p - 1);
      r = (r + k) % (p + 1);
    }
  }

  vector<int> res(n);
  for (int i = 0, j = 1, k = 1; i < n;
       i++, j <<= 1, j %= (p - 1), k <<= 1, k %= (p + 1)) {
    if (x[n - i - 1] == '1') {
      res[n - i - 1] = f[(q - j + p - 1) % (p - 1)] + 1;
    } else {
      res[n - i - 1] = f[(r + k) % (p + 1)] + 1;
    }
  }

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

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

int popcount(vector<int> const &x) { return accumulate(x.begin(), x.end(), 0); }

int popcount(int a) {
  int s = 0;
  for (int i = 0; i < 31; ++i)
    s += (a & (1u << i)) ? 1 : 0;
  return s;
}

int f(int r) {
  if (r == 0)
    return 0;
  return f(r % popcount(r)) + 1;
}

vector<int> input_x() {
  string s;
  cin >> s;
  vector<int> x0(s.size());
  for (int i = 0; i < s.size(); ++i)
    x0[i] = s[i] - '0';
  return x0;
}

int mod(std::vector<int> const &x, int d) {
  if (d <= 0)
    return 0;
  int m = 0;
  for (int i = 0; i < x.size(); ++i) {
    m = (m * 2 + x[i]) % d;
  }
  return m;
}

void print_answer(vector<int> &a) {
  for (auto x : a)
    cout << x << endl;
}

int main() {
  int n;
  cin >> n;
  const auto x0 = input_x();
  const int pc = popcount(x0);
  vector<int> ans(n);
  for (int k = 0; k < 2; ++k) {
    const int dpc = (k == 0) ? (pc + 1) : (pc - 1);
    if (dpc <= 0)
      continue;
    const int m = mod(x0, dpc);
    int p = 1;
    for (int i = n - 1; i >= 0; --i) {
      int t;
      if (x0[i] == k) {
        if (k == 0)
          t = (m + p) % dpc;
        else
          t = (m - p + dpc) % dpc;
        ans[i] = f(t) + 1;
      }
      p = (p * 2) % dpc;
    }
  }
  print_answer(ans);
  return 0;
}

Error: 0

Fixed submission:

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

int popcount(vector<int> const &x) { return accumulate(x.begin(), x.end(), 0); }

int popcount(int a) {
  int s = 0;
  for (int i = 0; i < 31; ++i)
    s += (a & (1u << i)) ? 1 : 0;
  return s;
}

int f(int r) {
  if (r == 0)
    return 0;
  return f(r % popcount(r)) + 1;
}

vector<int> input_x() {
  string s;
  cin >> s;
  vector<int> x0(s.size());
  for (int i = 0; i < s.size(); ++i)
    x0[i] = s[i] - '0';
  return x0;
}

int mod(std::vector<int> const &x, int d) {
  if (d <= 0)
    return 0;
  int m = 0;
  for (int i = 0; i < x.size(); ++i) {
    m = (m * 2 + x[i]) % d;
  }
  return m;
}

void print_answer(vector<int> &a) {
  for (auto x : a)
    cout << x << endl;
}

int main() {
  int n;
  cin >> n;
  const auto x0 = input_x();
  const int pc = popcount(x0);
  const int mi = mod(x0, pc + 1);
  const int md = mod(x0, pc - 1);
  vector<int> ans(n);
  for (int k = 0; k < 2; ++k) {
    const int dpc = (k == 0) ? (pc + 1) : (pc - 1);
    if (pc == 0)
      continue;
    int p = 1;
    for (int i = n - 1; i >= 0; --i) {
      int t;
      if (x0[i] == k) {
        if (k == 0)
          t = (mi + p) % dpc;
        else
          t = (md - p + dpc) % dpc;
        ans[i] = f(t) + 1;
      }
      p = (p * 2) % dpc;
    }
  }
  print_answer(ans);
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int v[200010], r[200010];

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  string s;
  cin >> s;

  int pcnt = 0;
  for (int i = 0; i < n; i++)
    pcnt += s[i] == '1';

  for (int i = 1; i <= n; i++) {
    r[i] = 1 + r[i % __builtin_popcount(i)];
  }

  int p = pcnt + 1, val1 = 0;
  int q = pcnt - 1, val2 = 0;
  for (int i = 0; i < n; i++) {
    val1 = val1 * 2 + (s[i] == '1');
    val1 %= p;
    if (q > 0) {
      val2 = val2 * 2 + (s[i] == '1');
      val2 %= q;
    }
  }

  int mul1 = 1, mul2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int now = (val1 + mul1) % p;
      v[i] = now % p;
    } else if (q > 0) {
      int now = (val2 + q - mul2 % q) % q;
      v[i] = now % q;
    } else {
      v[i] = -1;
    }
    mul1 = mul1 * 2 % p;
    if (q > 0)
      mul2 = mul2 * 2 % q;
  }

  for (int i = 0; i < n; i++) {
    cout << (v[i] == -1 ? 0 : 1 + r[v[i]]) << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

int v[200010], r[200010];

int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  string s;
  cin >> s;

  int pcnt = 0;
  for (int i = 0; i < n; i++)
    pcnt += s[i] == '1';

  for (int i = 1; i <= n; i++) {
    r[i] = 1 + r[i % __builtin_popcount(i)];
  }

  int p = pcnt + 1, val1 = 0;
  int q = pcnt - 1, val2 = 0;
  for (int i = 0; i < n; i++) {
    val1 = val1 * 2 + (s[i] == '1');
    val1 %= p;
    if (q > 0) {
      val2 = val2 * 2 + (s[i] == '1');
      val2 %= q;
    }
  }

  int mul1 = 1, mul2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      int now = (val1 + mul1) % p;
      v[i] = now % p;
    } else if (q > 0) {
      int now = (val2 + q - mul2 % q) % q;
      v[i] = now % q;
    } else {
      v[i] = -1;
    }
    mul1 = mul1 * 2 % p;
    mul2 = mul2 * 2 % q;
  }

  for (int i = 0; i < n; i++) {
    cout << (v[i] == -1 ? 0 : 1 + r[v[i]]) << endl;
  }
}