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

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1e9 + 7;

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

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> f(N);
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
  }
  // choose 0 or 1
  ll r0 = 0, r1 = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == '1') {
      r0 = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        r1 = (r1 + modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  for (ll i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll val = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (val < 0)
        val += (cnt + 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    } else {
      if (cnt == 1) {
        f[i] = 0;
        continue;
      }
      ll val = (r1 - modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      if (val < 0)
        val += (cnt - 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << f[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define _GLIBCXX_DEBUG
#define all(v) (v).begin(), (v).end()

using namespace std;

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

const ll LINF = 1LL << 60;
const int INF = 1 << 29;
const ll MOD = 1e9 + 7;

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

int main() {
  int N;
  cin >> N;
  string X;
  cin >> X;
  vector<ll> f(N);
  ll cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      cnt++;
  }
  // choose 0 or 1
  ll r0 = 0, r1 = 0;
  for (ll i = 0; i < N; i++) {
    if (X[i] == '1') {
      r0 = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (cnt > 1)
        r1 = (r1 + modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
    }
  }
  for (ll i = 0; i < N; i++) {
    if (X[i] == '0') {
      ll val = (r0 + modpow(2, N - i - 1, cnt + 1)) % (cnt + 1);
      if (val < 0)
        val += (cnt + 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    } else {
      ll val = (r1 - modpow(2, N - i - 1, cnt - 1)) % (cnt - 1);
      if (val < 0)
        val += (cnt - 1);
      int res = 1;
      while (val != 0) {
        val %= __builtin_popcountll(val);
        res++;
      }
      f[i] = res;
    }
  }
  for (int i = 0; i < N; i++) {
    cout << f[i] << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
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;
}

ll cnt[200015];

ll f(ll i) {
  if (i == 0)
    return 0;
  if (cnt[i] > 0)
    return cnt[i];
  ll pc = 0;
  rep(j, 30) {
    if ((i >> j) & 1)
      ++pc;
  }
  if (pc == 0)
    return 0;
  ll res = f(i % pc) + 1;
  return res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  string s;
  cin >> n >> s;
  for (ll i = 2; i <= n + 5; ++i) {
    cnt[i] = 0;
  }
  cnt[1] = 1;
  for (ll i = 2; i <= n + 5; ++i) {
    cnt[i] = f(i);
  }
  ll p = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++p;
  }
  ll ans1 = 0, ans2 = 0, p1 = p - 1, p2 = p + 1;
  if (p <= 1)
    p1 = 1;
  for (int i = 0; i < n; ++i) {
    ans1 = (ans1 * 2) % p1;
    ans2 = (ans2 * 2) % p2;
    if (s[i] == '1') {
      ans1 = (ans1 + 1) % p1;
      ans2 = (ans2 + 1) % p2;
    }
  }
  vector<ll> ans(n);
  ll div1 = 1, div2 = 1;
  for (int i = n - 1; i >= 0; --i) {
    ll num = 0;
    if (s[i] == '0') {
      num = (ans2 + div2) % p2;
    } else {
      if (p == 1)
        num = 200014;
      else
        num = (ans1 + p1 - div1) % p1;
    }
    ans[i] = num;
    div1 = (div1 * 2) % p1;
    div2 = (div2 * 2) % p2;
  }
  cnt[200014] = -1;
  rep(i, n) cout << cnt[ans[i]] + 1 << 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;
typedef long long int ll;
typedef pair<ll, ll> P;
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;
}

ll cnt[200015];

ll f(ll i) {
  if (i == 0)
    return 0;
  if (cnt[i] != 0)
    return cnt[i];
  ll pc = 0;
  rep(j, 29) {
    if (i >> j & 1)
      ++pc;
  }
  return f(i % pc) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  string s;
  cin >> n >> s;
  for (ll i = 2; i <= n + 5; ++i) {
    cnt[i] = 0;
  }
  cnt[1] = 1;
  for (ll i = 2; i <= n + 5; ++i) {
    cnt[i] = f(i);
  }
  ll p = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++p;
  }
  ll ans1 = 0, ans2 = 0, p1 = p - 1, p2 = p + 1;
  if (p == 0)
    p1 = 1;
  for (int i = 0; i < n; ++i) {
    ans1 = (ans1 * 2) % p1;
    ans2 = (ans2 * 2) % p2;
    if (s[i] == '1') {
      ans1 = (ans1 + 1) % p1;
      ans2 = (ans2 + 1) % p2;
    }
  }
  vector<ll> ans(n);
  ll div1 = 1, div2 = 1;
  for (int i = n - 1; i >= 0; --i) {
    ll num = 0;
    if (s[i] == '0') {
      num = (ans2 + div2) % p2;
    } else {
      num = (ans1 + p1 - div1) % p1;
    }
    ans[i] = num;
    div1 = (div1 * 2) % p1;
    div2 = (div2 * 2) % p2;
  }
  rep(i, n) cout << cnt[ans[i]] + 1 << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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;
}

int memo[200005];

int calc(int x) {
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];
  int p = 0;
  int y = x;
  while (y > 0) {
    p += y % 2;
    y /= 2;
  }
  if (p == 0)
    return 0;
  return calc(x % p) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  memset(memo, -1, sizeof(memo));
  memo[0] = 0;
  for (int i = 1; i < 200005; ++i) {
    memo[i] = calc(i);
  }
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  int lCnt = cnt - 1;
  chmax(lCnt, 1);
  int uCnt = cnt + 1;
  int lNum = 0, uNum = 0;
  rep(i, n) {
    lNum = (lNum * 2 + (s[i] - '0')) % lCnt;
    uNum = (uNum * 2 + (s[i] - '0')) % uCnt;
  }
  int lTwo = 1, uTwo = 1;
  vector<int> ans(n, 0);
  for (int i = n - 1; i >= 0; --i) {
    int num = 0;
    if (s[i] == '0') {
      num = (uNum + uTwo) % uCnt;
      ans[i] = memo[num] + 1;
    } else {
      if (cnt != 1) {
        num = (lNum + lCnt - lTwo) % lCnt;
        ans[i] = memo[num] + 1;
      } else {
        ans[i] = 0;
      }
    }
    lTwo = (lTwo * 2) % lCnt;
    uTwo = (uTwo * 2) % uCnt;
  }
  rep(i, n) cout << ans[i] << 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;
typedef long long int ll;
typedef pair<int, int> P;
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;
}

int memo[200005];

int calc(int x) {
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];
  int p = 0;
  int y = x;
  while (y > 0) {
    p += y % 2;
    y /= 2;
  }
  if (p == 0)
    return 0;
  return calc(x % p) + 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  memset(memo, -1, sizeof(memo));
  memo[0] = 0;
  for (int i = 1; i < 200005; ++i) {
    memo[i] = calc(i);
  }
  int cnt = 0;
  rep(i, n) {
    if (s[i] == '1')
      ++cnt;
  }
  int lCnt = cnt - 1;
  if (cnt == 0)
    lCnt = 0;
  int uCnt = cnt + 1;
  int lNum = 0, uNum = 0;
  rep(i, n) {
    lNum = (lNum * 2 + (s[i] - '0')) % lCnt;
    uNum = (uNum * 2 + (s[i] - '0')) % uCnt;
  }
  int lTwo = 1, uTwo = 1;
  vector<int> ans(n, 0);
  for (int i = n - 1; i >= 0; --i) {
    int num = 0;
    if (s[i] == '0') {
      num = (uNum + uTwo) % uCnt;
      ans[i] = memo[num] + 1;
    } else {
      if (cnt != 1) {
        num = (lNum + lCnt - lTwo) % lCnt;
        ans[i] = memo[num] + 1;
      } else {
        ans[i] = 0;
      }
    }
    lTwo = (lTwo * 2) % lCnt;
    uTwo = (uTwo * 2) % uCnt;
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
int dfs(int n) {
  if (n == 0) {
    return 0;
  }
  int now = n;
  int co = 0;
  while (now > 0) {
    if (now % 2 == 1) {
      co++;
    }
    now /= 2;
  }
  return dfs(n % co) + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int tot = 0;
  rep(i, n) {
    if (s[i] == '1') {
      tot++;
    }
  }

  vector<int> com(n), cop(n);
  int p = 1;
  rep(i, n) {
    if (tot > 1) {
      com[i] = p % (tot - 1);
      p = p * 2 % (tot - 1);
    }
  }
  p = 1;
  rep(i, n) {

    cop[i] = p % (tot + 1);
    p = p * 2 % (tot + 1);
  }
  int tm = 0;
  int tp = 0;
  rep(i, n) {
    if (s[n - 1 - i] == '1') {
      if (tot > 1) {
        tm = (tm + com[i]) % (tot - 1);
      }
      tp = (tp + cop[i]) % (tot + 1);
    }
  }
  rep(i, n) {
    int nowm = tm;
    int nowp = tp;
    if (s[i] == '1') {
      if (tot <= 1) {
        cout << 0 << endl;
      } else {
        nowm = (nowm + tot - 1 - com[n - 1 - i]) % (tot - 1);
        cout << dfs(nowm) + 1 << endl;
      }
    } else {
      nowp = (nowp + cop[n - 1 - i]) % (tot + 1);
      cout << dfs(nowp) + 1 << endl;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
int dfs(int n) {
  if (n == 0) {
    return 0;
  }
  int now = n;
  int co = 0;
  while (now > 0) {
    if (now % 2 == 1) {
      co++;
    }
    now /= 2;
  }
  return dfs(n % co) + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int tot = 0;
  rep(i, n) {
    if (s[i] == '1') {
      tot++;
    }
  }

  vector<int> com(n), cop(n);
  int p = 1;
  rep(i, n) {
    if (tot > 1) {
      com[i] = p % (tot - 1);
      p = p * 2 % (tot - 1);
    }
  }
  p = 1;
  rep(i, n) {

    cop[i] = p % (tot + 1);
    p = p * 2 % (tot + 1);
  }
  int tm = 0;
  int tp = 0;
  rep(i, n) {
    if (s[n - 1 - i] == '1') {
      if (tot > 1) {
        tm = (tm + com[i]) % (tot - 1);
      }
      tp = (tp + cop[i]) % (tot + 1);
    }
  }
  rep(i, n) {
    int nowm = tm;
    int nowp = tp;
    if (s[i] == '1') {
      nowm = (nowm + tot - 1 - com[n - 1 - i]) % (tot - 1);
      cout << dfs(nowm) + 1 << endl;
    } else {
      nowp = (nowp + cop[n - 1 - i]) % (tot + 1);
      cout << dfs(nowp) + 1 << endl;
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  vector<ll> v(200010, 0);
  ll po = 0;
  rep(i, n) if (x[i] == '1') po++;
  vector<ll> ans(n, -1);
  vector<ll> vv(n, 0);
  vector<ll> vvv2(n, 0);
  vector<ll> vvv(n, 0);
  ll a = 1;
  rep(i, n) {
    if (x[n - i - 1] == '1')
      vv[i] = a;
    a *= 2;
    if (po - 1 == 0) {
      ans[n - i - 1] = 0;
    } else
      a %= (po - 1);
  }
  a = 1;
  rep(i, n) {
    if (x[n - i - 1] == '1')
      vvv[i] = a;
    else
      vvv2[i] = a;
    a *= 2;
    a %= (po + 1);
  }
  ll sum1 = 0, sum2 = 0;
  rep(i, n) {
    sum1 += vv[i];
    sum2 += vvv[i];
  }

  rep(ii, 200009) {
    ll i = ii + 1;
    ll cnt = 0;
    while (i != 0) {
      cnt++;
      i = i % (__builtin_popcountll(i));
    }
    v[ii + 1] = cnt;
  }
  rep(i, n) {
    ll t;
    if (x[i] == '1' && ans[i] == 0)
      continue;
    if (x[i] == '1')
      t = (sum1 + po - 1 - vv[n - 1 - i]) % (po - 1);
    else
      t = (sum2 + vvv2[n - 1 - i]) % (po + 1);
    ans[i] = v[t] + 1;
  }
  for (auto an : ans)
    cout << an << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  vector<ll> v(200010, 0);
  ll po = 0;
  rep(i, n) if (x[i] == '1') po++;
  vector<ll> ans(n, 0);
  vector<ll> vv(n, 0);
  vector<ll> vvv2(n, 0);
  vector<ll> vvv(n, 0);
  ll a = 1;
  rep(i, n) {
    if (x[n - i - 1] == '1')
      vv[i] = a;
    a *= 2;
    a %= (po - 1);
  }
  a = 1;
  rep(i, n) {
    if (x[n - i - 1] == '1')
      vvv[i] = a;
    else
      vvv2[i] = a;
    a *= 2;
    a %= (po + 1);
  }
  ll sum1 = 0, sum2 = 0;
  rep(i, n) {
    sum1 += vv[i];
    sum2 += vvv[i];
  }

  rep(ii, 200009) {
    ll i = ii + 1;
    ll cnt = 0;
    while (i != 0) {
      cnt++;
      i = i % (__builtin_popcountll(i));
    }
    v[ii + 1] = cnt;
  }
  rep(i, n) {
    ll t;
    if (x[i] == '1')
      t = (sum1 + po - 1 - vv[n - 1 - i]) % (po - 1);
    else
      t = (sum2 + vvv2[n - 1 - i]) % (po + 1);
    ans[i] = v[t] + 1;
  }
  for (auto an : ans)
    cout << an << endl;
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef double ld;

ll n;
ll x[200010];
ll a[1000010];
ll np[200010];
ll nm[200010];
ll o;

int main(void) {
  for (ll i = 1; i < 1000010; i++) {
    ll cou = 0;
    ll gap = i;

    while (gap > 0) {
      cou += gap % 2;
      gap /= 2;
    }

    a[i] = a[i % cou] + 1;
  }

  scanf("%lld", &n);

  for (ll i = 0; i < n; i++) {
    scanf("%1lld", &x[i]);
    o += x[i];
  }

  ll p = o + 1;
  ll m = o - 1;
  ll P = 0;
  ll M = 0;
  ll ff = 0;

  if (m <= 0) {
    ff = 1;
    m = 19876543210;
  }

  np[0] = 1;
  nm[0] = 1;

  for (ll i = 1; i <= n; i++) {
    np[i] = np[i - 1] * 2 % p;
    nm[i] = nm[i - 1] * 2 % m;
  }

  for (ll i = 0; i < n; i++) {
    P = (P + x[i] * np[n - 1 - i]) % p;
    M = (M + x[i] * nm[n - 1 - i]) % m;
  }

  for (ll i = 0; i < n; i++) {
    if (x[i] == 0) {
      // p

      ll pp = (P + np[n - 1 - i]) % p;

      if (pp < 0)
        pp += p;

      printf("%lld\\n", a[pp] + 1);
    }

    else {
      // m

      if (ff == 1) {
        printf("0\\n");
        continue;
      }

      ll mm = (M - nm[n - 1 - i]) % m;

      if (mm < 0)
        mm += m;

      printf("%lld\\n", a[mm] + 1);
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef double ld;

ll n;
ll x[200010];
ll a[1000010];
ll np[200010];
ll nm[200010];
ll o;

int main(void) {
  for (ll i = 1; i < 1000010; i++) {
    ll cou = 0;
    ll gap = i;

    while (gap > 0) {
      cou += gap % 2;
      gap /= 2;
    }

    a[i] = a[i % cou] + 1;
  }

  scanf("%lld", &n);

  for (ll i = 0; i < n; i++) {
    scanf("%1lld", &x[i]);
    o += x[i];
  }

  ll p = o + 1;
  ll m = o - 1;
  ll P = 0;
  ll M = 0;

  np[0] = 1;
  nm[0] = 1;

  for (ll i = 1; i <= n; i++) {
    np[i] = np[i - 1] * 2 % p;
    nm[i] = nm[i - 1] * 2 % m;
  }

  for (ll i = 0; i < n; i++) {
    P = (P + x[i] * np[n - 1 - i]) % p;
    M = (M + x[i] * nm[n - 1 - i]) % m;
  }

  for (ll i = 0; i < n; i++) {
    if (x[i] == 0) {
      // p

      ll pp = (P + np[n - 1 - i]) % p;

      if (pp < 0)
        pp += p;

      printf("%lld\\n", a[pp] + 1);
    }

    else {
      // m

      ll mm = (M - nm[n - 1 - i]) % m;

      if (mm < 0)
        mm += m;

      printf("%lld\\n", a[mm] + 1);
    }
  }

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

#include "algorithm"
#include "bits/stdc++.h"
#include <iostream>
#define ll long long
using namespace std;
int count(int a) {
  int ans = 0;
  while (a) {
    a = a & (a - 1);
    ans++;
  }
  return ans;
}
ll fastpow(int n, int modn) {
  ll b = 2;
  ll res = 1;
  while (n) {
    if (n & 1)
      res = (b * res) % modn;
    b = (b * b) % modn;
    n >>= 1;
  }
  return res % modn;
}
int main() {
  int m;
  string n;
  cin >> m >> n;
  int c = 0;
  for (int i = 0; i < m; i++) {
    if (n[i] == '1')
      c++;
  }
  int p = 0, q = 0;
  for (int i = 0; i < m; i++) {
    if (n[i] == '1') {
      p += fastpow(m - 1 - i, c + 1);
      p %= c + 1;
      if (c != 1) {
        q += fastpow(m - 1 - i, c - 1);
        q %= c - 1;
      }
    }
  }
  if (c == 1)
    q = 0;
  for (int i = 0; i < m; i++) {
    int flag = 0;
    int pp = 0;
    if (n[i] == '0')
      pp = (p + fastpow(m - 1 - i, c + 1)) % (c + 1);
    else if (c == 1)
      flag++;
    else
      pp = (q - fastpow(m - 1 - i, c - 1) + c - 1) % (c - 1);
    if (flag)
      cout << "0" << endl;
    else {
      int anss = 1;
      while (pp) {
        pp %= count(pp);
        anss++;
      }
      cout << anss << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "algorithm"
#include "bits/stdc++.h"
#include <iostream>
#define ll long long
using namespace std;
int count(int a) {
  int ans = 0;
  while (a) {
    a = a & (a - 1);
    ans++;
  }
  return ans;
}
int fastpow(int a, int n, int modn) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % modn;
  int temp = fastpow(a, n / 2, modn);
  if (n % 2 == 1)
    return temp % modn * temp % modn * a % modn;
  else
    return temp % modn * temp % modn;
}
bool b[200050];
int main() {
  int m;
  string n;
  cin >> m >> n;
  int c = 0;
  for (int i = 0; i < m; i++) {
    if (n[i] == '1')
      c++;
  }
  int p = 0, q = 0;
  for (int i = 0; i < n.length(); i++) {
    if (n[i] == '1') {
      p += fastpow(2, m - 1 - i, c + 1);
      p %= c + 1;
      if (c != 1) {
        q += fastpow(2, m - 1 - i, c - 1);
        q %= c - 1;
      }
    }
  }
  if (c == 1)
    q = 0;
  for (int i = 0; i < m; i++) {
    int flag = 0;
    int pp;
    if (n[i] == '0')
      pp = (p + fastpow(2, m - 1 - i, c + 1)) % (c + 1);
    else {
      if (c == 1) {
        pp = 0;
        b[i] = true;
        flag++;
      } else
        pp = (q - fastpow(2, m - 1 - i, c - 1) + c - 1) % (c - 1);
    }
    if (flag)
      cout << "0" << endl;
    else {
      int anss = 0;
      while (pp) {
        pp %= count(pp);
        anss++;
      }
      cout << anss + 1 << endl;
    }
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int pop_count(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int main() {
  int N;
  cin >> N;
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  vector<int> S(N);
  for (int i = 0; i < N; i++)
    S[i] = (str[i] - '0');

  int64_t cnt = accumulate(S.begin(), S.end(), 0);
  int64_t V = 1;
  for (auto a : {cnt + 1, cnt - 1})
    if (a > 0)
      V *= a;

  int64_t R = 0, B = 1;
  for (int i = 0; i < N; i++) {
    if (S[i])
      R = (R + B) % V;
    B = B * 2 % V;
  }

  auto calc = [&](auto &&calc, int v) -> int {
    if (v == 0) {
      return 0;
    } else {
      return calc(calc, v % pop_count(v)) + 1;
    }
  };

  B = 1;
  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int64_t v = cnt + (S[i] ? -1 : 1);
    if (v == 0)
      continue;
    int64_t r = (R + B % v * (S[i] % v ? v - 1 : 1)) % v;
    ans[N - 1 - i] = 1 + calc(calc, r);
    B = B * 2 % V;
  }

  for (int a : ans)
    cout << a << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int pop_count(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int main() {
  int N;
  cin >> N;
  string str;
  cin >> str;
  reverse(str.begin(), str.end());
  vector<int> S(N);
  for (int i = 0; i < N; i++)
    S[i] = (str[i] - '0');

  int64_t cnt = accumulate(S.begin(), S.end(), 0);
  int64_t V = (cnt == 0 ? 1 : (cnt + 1) * (cnt - 1));

  int64_t R = 0, B = 1;
  for (int i = 0; i < N; i++) {
    if (S[i])
      R = (R + B) % V;
    B = B * 2 % V;
  }

  auto calc = [&](auto &&calc, int v) -> int {
    if (v == 0) {
      return 0;
    } else {
      return calc(calc, v % pop_count(v)) + 1;
    }
  };

  B = 1;
  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    int64_t v = cnt + (S[i] ? -1 : 1);
    if (v == 0)
      continue;
    int64_t r = (R + B % v * (S[i] % v ? v - 1 : 1)) % v;
    ans[N - 1 - i] = 1 + calc(calc, r);
    B = B * 2 % V;
  }

  for (int a : ans)
    cout << a << endl;
  return 0;
}