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

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

#define ll long long
ll mod = pow(10, 9) + 7;

ll pw(ll n, ll mod) {
  ll ans = 1;

  ll x = 2, y = n;
  while (y != 0) {
    if (y % 2)
      ans = ((ans % mod) * (x % mod)) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    y /= 2;
  }
  return ans;
}

ll cunt(ll n) {
  ll cnt = 0;
  while (n != 0) {
    if (n % 2)
      cnt++;
    n /= 2;
  }
  return cnt;
}

void func(ll n) {
  ll cnt = 1;
  while (n != 0) {
    ll temp = cunt(n);
    cnt++;
    n = n % temp;
  }
  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll mn = 0, mx = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      mn++;
  }
  if (mn < n) {
    mx = mn + 1;
    mn--;
  } else {
    mn--;
    mx = mn;
  }

  if (mn == 0 || mx == 0) {
    for (ll i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        if (i == n - 1)
          cout << 2 << endl;
        else
          cout << 1 << endl;
      }
    }
    return 0;
  }

  ll chota = 0, bada = 0;

  reverse(s.begin(), s.end());
  ll temp1 = 1, temp2 = 1;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      chota = ((chota % mn) + (temp1 % mn)) % mn;
      bada = ((bada % mx) + (temp2 % mx)) % mx;
    }
    temp1 = ((temp1 % mn) * 2) % mn;
    temp2 = ((temp2 % mx) * 2) % mx;
  }
  vector<ll> ans;
  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ll temp = chota - pw(i, mn);
      temp += mn;
      temp = temp % mn;
      func(temp);
    } else {
      ll temp = bada + pw(i, mx);
      temp = temp % mx;
      func(temp);
    }
  }
}


Error: 0

Fixed submission:

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

#define ll long long
ll mod = pow(10, 9) + 7;

ll pw(ll n, ll mod) {
  ll ans = 1;

  ll x = 2, y = n;
  while (y != 0) {
    if (y % 2)
      ans = ((ans % mod) * (x % mod)) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    y /= 2;
  }
  return ans;
}

ll cunt(ll n) {
  ll cnt = 0;
  while (n != 0) {
    if (n % 2)
      cnt++;
    n /= 2;
  }
  return cnt;
}

void func(ll n) {
  ll cnt = 1;
  while (n != 0) {
    ll temp = cunt(n);
    cnt++;
    n = n % temp;
  }
  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll mn = 0, mx = 0;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1')
      mn++;
  }
  if (mn < n) {
    mx = mn + 1;
    mn--;
  } else {
    mn--;
    mx = mn;
  }

  ll chota = 0, bada = 0;

  reverse(s.begin(), s.end());
  ll temp1 = 1, temp2 = 1;
  for (ll i = 0; i < n; i++) {
    if (s[i] == '1') {
      chota = ((chota % mn) + (temp1 % mn)) % mn;
      bada = ((bada % mx) + (temp2 % mx)) % mx;
    }
    temp1 = ((temp1 % mn) * 2) % mn;
    temp2 = ((temp2 % mx) * 2) % mx;
  }
  vector<ll> ans;
  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      ll temp = chota - pw(i, mn);
      temp += mn;
      temp = temp % mn;
      func(temp);
    } else {
      ll temp = bada + pw(i, mx);
      temp = temp % mx;
      func(temp);
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long modpow(long a, long b, long mod) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return res;
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int one = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      one++;
  long plus[n + 1];
  long minus[n + 1];
  long sum_plus = 0;
  long sum_minus = 0;
  for (long i = 0; i < n; i++) {
    if (one != n) {
      plus[i] = modpow(2L, i, (long)(one + 1));
      if (s[n - 1 - i] == '1')
        sum_plus = (sum_plus + plus[i]) % ((long)(one + 1));
    }
    if (one > 1) {
      minus[i] = modpow(2L, i, (long)(one - 1));
      if (s[n - 1 - i] == '1')
        sum_minus = (sum_minus + minus[i]) % ((long)(one - 1));
    }
  }
  for (int i = 0; i < n; i++) {
    long tmp;
    int ans = 0;
    if (s[i] == '0') {
      tmp = (sum_plus + plus[n - 1 - i]) % ((long)(one + 1));
      ans++;
      while (tmp > 0) {
        tmp = tmp % __builtin_popcount((int)tmp);
        ans++;
      }
      cout << ans << endl;
    } else {
      if (one == 1)
        cout << 0 << endl;
      else {
        tmp = (sum_minus + one - 1 - minus[n - 1 - i]) % ((long)(one - 1));
        ans++;
        while (tmp > 0) {
          tmp = tmp % __builtin_popcount((int)tmp);
          ans++;
        }
        cout << ans << endl;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long modpow(long a, long b, long mod) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return res;
}
int main() {
  int n;
  string s;
  cin >> n >> s;
  int one = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      one++;
  long plus[n + 1];
  long minus[n + 1];
  long sum_plus = 0;
  long sum_minus = 0;
  for (long i = 0; i < n; i++) {
    if (one != n) {
      plus[i] = modpow(2L, i, (long)(one + 1));
      if (s[n - 1 - i] == '1')
        sum_plus = (sum_plus + plus[i]) % ((long)(one + 1));
    }
    if (one != 0) {
      minus[i] = modpow(2L, i, (long)(one - 1));
      if (s[n - 1 - i] == '1')
        sum_minus = (sum_minus + minus[i]) % ((long)(one - 1));
    }
  }
  for (int i = 0; i < n; i++) {
    long tmp;
    int ans = 0;
    if (s[i] == '0') {
      tmp = (sum_plus + plus[n - 1 - i]) % ((long)(one + 1));
      ans++;
      while (tmp > 0) {
        tmp = tmp % __builtin_popcount((int)tmp);
        ans++;
      }
      cout << ans << endl;
    } else {
      if (one == 1)
        cout << 0 << endl;
      else {
        tmp = (sum_minus + one - 1 - minus[n - 1 - i]) % ((long)(one - 1));
        ans++;
        while (tmp > 0) {
          tmp = tmp % __builtin_popcount((int)tmp);
          ans++;
        }
        cout << ans << endl;
      }
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  string X;
  cin >> N >> X;
  int pcnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      pcnt++;
  }
  ll X_minus = 0, X_plus = 0;
  vector<ll> p2_minus(N, 1), p2_plus(N, 1);
  for (int i = N - 1; i >= 0; i--) {
    X_minus += (X[i] - '0') * p2_minus[i];
    X_plus += (X[i] - '0') * p2_plus[i];
    if (i > 0) {
      p2_minus[i - 1] = p2_minus[i] * 2;
      if (pcnt != 1)
        p2_minus[i - 1] %= pcnt - 1;
      p2_plus[i - 1] = p2_plus[i] * 2;
      p2_plus[i - 1] %= pcnt + 1;
    }
  }
  vector<int> ans(N, 0);
  for (int i = 0; i < N; i++) {
    if (X[i] - '0' == 1 && pcnt == 1) {
      cout << 0 << endl;
      continue;
    }
    int pcnt_re;
    ll X_tmp;
    if (X[i] == '0') {
      X[i] = '1';
      pcnt_re = pcnt + 1;
      X_tmp = X_plus + p2_plus[i];
      X_tmp %= pcnt_re;
    } else {
      X[i] = '0';
      pcnt_re = pcnt - 1;
      X_tmp = X_minus - p2_minus[i] + pcnt_re;
      if (pcnt_re != 0)
        X_tmp %= pcnt_re;
    }
    ans[i]++;
    while (X_tmp > 0) {
      X_tmp = X_tmp % __builtin_popcount(X_tmp);
      ans[i]++;
    }
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  string X;
  cin >> N >> X;
  int pcnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      pcnt++;
  }
  ll X_minus = 0, X_plus = 0;
  vector<ll> p2_minus(N, 1), p2_plus(N, 1);
  for (int i = N - 1; i >= 0; i--) {
    X_minus += (X[i] - '0') * p2_minus[i];
    X_plus += (X[i] - '0') * p2_plus[i];
    if (i > 0) {
      p2_minus[i - 1] = p2_minus[i] * 2;
      p2_minus[i - 1] %= pcnt - 1;
      p2_plus[i - 1] = p2_plus[i] * 2;
      p2_plus[i - 1] %= pcnt + 1;
    }
  }
  vector<int> ans(N, 0);
  for (int i = 0; i < N; i++) {
    if (X[i] - '0' == 1 && pcnt == 1) {
      cout << 0 << endl;
      continue;
    }
    int pcnt_re;
    ll X_tmp;
    if (X[i] == '0') {
      X[i] = '1';
      pcnt_re = pcnt + 1;
      X_tmp = X_plus + p2_plus[i];
      X_tmp %= pcnt_re;
    } else {
      X[i] = '0';
      pcnt_re = pcnt - 1;
      X_tmp = X_minus - p2_minus[i] + pcnt_re;
      X_tmp %= pcnt_re;
    }
    ans[i]++;
    while (X_tmp > 0) {
      X_tmp = X_tmp % __builtin_popcount(X_tmp);
      ans[i]++;
    }
    cout << ans[i] << endl;
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int cnt(0);
  for (int i(0); i < N; i++) {
    if (s[i] == '1')
      cnt++;
  }
  int kouho1(cnt + 1), kouho2(cnt - 1);
  // cout << cnt << " " << endl;
  int amari1[200001] = {0}, amari2[200001] = {0};
  // int tmp1(1),tmp2(1);
  amari1[0] = 1 % kouho1;
  if (kouho2 != 0)
    amari2[0] = 1 % kouho2;

  for (int i(1); i < N; i++) {
    amari1[i] = (amari1[i - 1] * 2) % kouho1;
    if (kouho2 != 0)
      amari2[i] = (amari2[i - 1] * 2) % kouho2;
  }
  int x1(0), x2(0);
  for (int i(0); i < N; i++) {
    if (s[i] == '1') {
      x1 += amari1[N - i - 1];
      x2 += amari2[N - i - 1];
      x1 %= kouho1;
      if (kouho2 != 0)
        x2 %= kouho2;
    }
  }
  // cout << x1 << ' '<<x2 << endl;
  for (int i(0); i < N; i++) {
    // 一回余り求める
    int x;
    int t1 = x1, t2 = x2;
    int tmp(1);
    if (s[i] == '1') {
      if (kouho2 == 0) {
        cout << 0 << endl;
        continue;
      }
      t2 -= amari2[N - i - 1];
      if (t2 < 0)
        t2 += kouho2;
      x = t2;
    } else {
      t1 += amari1[N - i - 1];
      t1 %= kouho1;
      x = t1;
    }
    // cout<< "x:" << x << endl;
    while (x != 0) {
      tmp++;
      x = x % (__builtin_popcount(x));
    }
    cout << tmp << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
using namespace std;
const int INFint = 1e9 + 1;
const ll INFll = (ll)1e18 + 1;
ll MOD = 1e9 + 7;

int main() {
  int N;
  //__builtin_popcount(bit)
  cin >> N;
  string s;
  cin >> s;
  int cnt(0);
  for (int i(0); i < N; i++) {
    if (s[i] == '1')
      cnt++;
  }
  int kouho1(cnt + 1), kouho2(cnt - 1);
  // cout << cnt << " " << endl;
  int amari1[200001] = {0}, amari2[200001] = {0};
  // int tmp1(1),tmp2(1);
  amari1[0] = 1 % kouho1;
  amari2[0] = 1 % kouho2;
  for (int i(1); i < N; i++) {
    amari1[i] = (amari1[i - 1] * 2) % kouho1;
    amari2[i] = (amari2[i - 1] * 2) % kouho2;
    // tmp1 %= kouho1;
    // tmp2 %= kouho2;
  }
  int x1(0), x2(0);
  for (int i(0); i < N; i++) {
    if (s[i] == '1') {
      x1 += amari1[N - i - 1];
      x2 += amari2[N - i - 1];
      x1 %= kouho1;
      x2 %= kouho2;
    }
  }
  // cout << x1 << ' '<<x2 << endl;
  for (int i(0); i < N; i++) {
    // 一回余り求める
    int x;
    int t1 = x1, t2 = x2;
    int tmp(1);
    if (s[i] == '1') {
      t2 -= amari2[N - i - 1];
      //
      if (t2 < 0)
        t2 += kouho2;
      x = t2;
    } else {
      t1 += amari1[N - i - 1];
      t1 %= kouho1;
      x = t1;
    }
    // cout<< "x:" << x << endl;
    while (x != 0) {
      tmp++;
      x = x % (__builtin_popcount(x));
    }
    cout << tmp << endl;
  }
  return 0;
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef long long ll;

int n, cnt;
string s;

int dp[202020];
int get(int x) {
  int ret = 0;
  while (x) {
    if (x & 1)
      ret++;
    x >>= 1;
  }
  return ret;
}

int f(int x) {
  int &res = dp[x];
  if (res != -1)
    return res;
  if (x == 0)
    return res = 0;
  return res = 1 + f(x % get(x));
}

int table[2][202020];
int all[2];
void init() {
  ll a = 1, b = 1;
  for (int i = n - 1; ~i; i--) {
    if (cnt != 1)
      table[0][i] = a % (cnt - 1);
    table[1][i] = b % (cnt + 1);
    a <<= 1;
    b <<= 1;
    if (cnt != 1)
      a %= (cnt - 1);
    b %= (cnt + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1' && cnt != 1)
      all[0] = (all[0] + table[0][i]) % (cnt - 1);
    if (s[i] == '1')
      all[1] = (all[1] + table[1][i]) % (cnt + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> s;
  memset(dp, -1, sizeof dp);
  for (auto i : s)
    cnt += (i == '1');
  init();

  for (int i = 0; i < n; i++) {
    if (cnt == 1 && s[i] == '1') {
      cout << "0\\n";
      continue;
    }
    if (s[i] == '0') {
      int t = all[1];
      t = (t + table[1][i]) % (cnt + 1);
      cout << f(t) + 1 << "\\n";
    } else {
      int t = all[0];
      t = (t - table[0][i]) % (cnt - 1);
      t += cnt - 1;
      t %= (cnt - 1);
      cout << f(t) + 1 << "\\n";
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;

typedef long long ll;

int n, cnt;
string s;

int dp[202020];
int get(int x) {
  int ret = 0;
  while (x) {
    if (x & 1)
      ret++;
    x >>= 1;
  }
  return ret;
}

int f(int x) {
  int &res = dp[x];
  if (res != -1)
    return res;
  if (x == 0)
    return res = 0;
  return res = 1 + f(x % get(x));
}

int table[2][202020];
int all[2];
void init() {
  ll a = 1, b = 1;
  for (int i = n - 1; ~i; i--) {
    table[0][i] = a % (cnt - 1);
    table[1][i] = b % (cnt + 1);
    a <<= 1;
    b <<= 1;
    a %= (cnt - 1);
    b %= (cnt + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      all[0] = (all[0] + table[0][i]) % (cnt - 1);
    if (s[i] == '1')
      all[1] = (all[1] + table[1][i]) % (cnt + 1);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> s;
  memset(dp, -1, sizeof dp);
  for (auto i : s)
    cnt += (i == '1');
  init();

  for (int i = 0; i < n; i++) {
    if (cnt == 1 && s[i] == '1') {
      cout << "0\\n";
      continue;
    }
    if (s[i] == '0') {
      int t = all[1];
      t = (t + table[1][i]) % (cnt + 1);
      cout << f(t) + 1 << "\\n";
    } else {
      int t = all[0];
      t = (t - table[0][i]) % (cnt - 1);
      t += cnt - 1;
      t %= (cnt - 1);
      cout << f(t) + 1 << "\\n";
    }
  }
}
Problem p02609 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

int pcnt(int x) {
  int res = 0;
  while (x) {
    res += x & 1;
    x >>= 1;
  }
  return res;
}

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> x(n);
  for (int i = 0; i < n; i++)
    x[i] = s[i] - '0';

  int pc = 0; // 初期のポップカウント。
  for (int i = 0; i < n; i++)
    pc += x[i];

  vector<int> ans(n);
  for (int b = 0; b < 2; b++) {
    int npc = pc;
    if (b == 0)
      npc++; // b==0の時、0を1に反転する。
    else
      npc--; // b==1の時、1を0に反転する。
    if (npc <= 0)
      continue;

    // ビットを反転していないxをnpcで割った余りを求める。
    int r0 = 0;
    for (int i = 0; i < n; i++)
      r0 = (r0 * 2 + x[i]) % npc;

    // n桁すべてのbitが1の場合、各桁までのnpcで割った余りの配列を求める。
    vector<int> rArray(n);
    int pre = 1;
    for (int i = n - 1; 0 <= i; i--) {
      rArray[i] = pre % npc;
      pre *= 2;
      pre %= npc;
    }

    // i桁のビットを反転してf(r)を求める。
    for (int i = n - 1; 0 <= i; i--) {
      if (x[i] == b) {
        int r = r0;
        if (b == 0)
          r = (r0 + rArray[i]) % npc;
        else
          r = (r0 - rArray[i] + npc) % npc;
        ans[i] = f(r) + 1;
      }
    }
  }

  for (const auto &item : ans)
    cout << item << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1000000007;
const double PI = acos(-1);

int pcnt(int x) {
  int res = 0;
  while (x) {
    res += x & 1;
    x >>= 1;
  }
  return res;
}

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> x(n);
  for (int i = 0; i < n; i++)
    x[i] = s[i] - '0';

  int pc = 0; // 初期のポップカウント。
  for (int i = 0; i < n; i++)
    pc += x[i];

  // n桁すべてのbitが1の場合、各桁までのpcで割った余りの配列を求める。
  vector<int> rArray(n);
  int element = 0;
  for (int i = n - 1; 0 <= i; i--)
    rArray[i] = (element * 2 + 1) % pc;

  vector<int> ans(n);
  for (int b = 0; b < 2; b++) {
    int npc = pc;
    if (b == 0)
      npc++; // b==0の時、0を1に反転する。
    else
      npc--; // b==1の時、1を0に反転する。
    if (npc <= 0)
      continue;

    // ビットを反転していないxをnpcで割った余りを求める。
    int r0 = 0;
    for (int i = 0; i < n; i++)
      r0 = (r0 * 2 + x[i]) % npc;

    // i桁のビットを反転してf(r)を求める。
    int r = r0;
    for (int i = n - 1; 0 <= i; i--) {
      if (b == 0)
        r = (r0 + rArray[i]) % npc;
      else
        r = (r0 - rArray[i] + npc) % npc;
      ans[i] = f(r) + 1;
    }
  }

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

#include <stdio.h>
const int N = 2e5 + 5;
int pow2[N][3];
int f[N][3];
char s[N];
int getans(int x) {
  int ans = 0;
  while (x) {
    int tmp = x;
    int cnt = 0;
    while (tmp) {
      if (tmp & 1)
        cnt++;
      tmp >>= 1;
    }
    ans++;
    x = x % cnt;
  }
  return ans;
}
int main() {
  int n;
  scanf("%d%s", &n, s);
  int mod[3] = {0, 0, 0};
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      mod[0]++;
  mod[1] = mod[0] + 1;
  mod[2] = mod[0] - 1;
  for (int j = 0; j < 3; j++)
    if (mod[j] != 0) {
      pow2[0][j] = 1 % mod[j];
    }
  for (int i = 1; i <= n + 1; i++)
    for (int j = 0; j < 3; j++)
      if (mod[j] != 0) {
        pow2[i][j] = pow2[i - 1][j] * 2 % mod[j];
        if (i != n + 1) {
          f[i][j] = f[i - 1][j] * 2 % mod[j];
          f[i][j] = (f[i][j] + (s[i - 1] == '1')) % mod[j];
        }
      }
  for (int i = 0; i < n; i++) {
    int tmp = 0;
    int ans = 0;
    if (s[i] == '1') {
      if (mod[2] == 0) {
        ans = 0;
      } else {
        tmp = ((f[n][2] - pow2[n - i - 1][2]) % mod[2] + mod[2]) % mod[2];
        ans = 1 + getans(tmp);
      }
    } else {
      tmp = (f[n][1] + pow2[n - i - 1][1]) % mod[1];
      ans = 1 + getans(tmp);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

/*
10110111001011011001110
6002382
 */

Error: 0

Fixed submission:

#include <stdio.h>
const int N = 2e5 + 5;
int pow2[N][3];
int f[N][3];
char s[N];
int getans(int x) {
  int ans = 0;
  while (x) {
    int tmp = x;
    int cnt = 0;
    while (tmp) {
      if (tmp & 1)
        cnt++;
      tmp >>= 1;
    }
    x = x % cnt;
    ans++;
  }
  return ans;
}
int main() {
  int n;
  scanf("%d%s", &n, s);
  int mod[3] = {0, 0, 0};
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      mod[0]++;
  mod[1] = mod[0] + 1;
  mod[2] = mod[0] - 1;
  for (int j = 0; j < 3; j++) {
    pow2[0][j] = 1 % mod[j];
  }
  for (int i = 1; i <= n + 1; i++)
    for (int j = 0; j < 3; j++) {
      pow2[i][j] = pow2[i - 1][j] * 2 % mod[j];
      if (i != n + 1) {
        f[i][j] = f[i - 1][j] * 2 % mod[j];
        f[i][j] = (f[i][j] + (s[i - 1] == '1')) % mod[j];
      }
    }
  for (int i = 0; i < n; i++) {
    int tmp = 0;
    int ans = 0;
    if (s[i] == '1') {
      tmp = ((f[n][2] - pow2[n - i - 1][2]) % mod[2] + mod[2]) % mod[2];
      ans = 1 + getans(tmp);
    } else {
      tmp = (f[n][1] + pow2[n - i - 1][1]) % mod[1];
      ans = 1 + getans(tmp);
    }
    printf("%d\\n", ans);
  }
  return 0;
}

/*
10110111001011011001110
6002382
 */
Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

ll Pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return Pow(x * x % mod, n / 2, mod);
  else
    return x * Pow(x, n - 1, mod) % mod;
}

int main() {
  ll n;
  cin >> n;
  string x;
  cin >> x;
  int cnt = 0;
  rep(i, n) {
    if (x[i] == '1')
      cnt++;
  }
  ll modm = 0, modp = 0;
  ll digp = 1, digm = 1;
  rep(i, n) {
    if (x[n - 1 - i] == '1') {
      if (cnt > 1)
        (modm += digm) %= cnt - 1;
      (modp += digp) %= cnt + 1;
    }
    (digp *= 2) %= (cnt + 1);
    if (cnt > 1)
      (digm *= 2) %= (cnt - 1);
  }
  int ans[n];
  rep(i, n) {
    int v = 0;
    if (x[i] == '1') {
      if (cnt > 1) {
        v = (modm - Pow(2, n - 1 - i, cnt - 1)) % (cnt - 1);
        if (v < 0)
          v += cnt - 1;
        ans[i] = 1;
      } else {
        ans[i] = 0;
      }
    } else {
      v = (modp + Pow(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      ans[i] = 1;
    }
    while (v) {
      v %= popcount(v);
      ans[i]++;
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  int ans[n];
  rep(i, n) {
    string s = x;
    if (s[i] == '0')
      s[i] = '1';
    else
      s[i] = '0';
    int cnt = 0;
    rep(j, n) if (s[j] == '1') cnt++;
    if (cnt == 0) {
      ans[i] = 0;
    } else {
      ans[i] = 1;
      int dig = 1;
      int mod = 0;
      rep(j, n) {
        if (s[n - 1 - j] == '1')
          (mod += dig) %= cnt;
        (dig *= 2) %= cnt;
      }
      int v = mod;
      while (v > 0) {
        v %= popcount(v);
        ans[i]++;
      }
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}