Problem p02609 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF pow(10, 9) + 7
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define chmax(x, y) x = max(x, y)
#define print(x) cout << x << "\\n"
#define ALL(v) v.begin(), v.end()
#define SUM(v) accumulate(ALL(v), 0)
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
typedef long long ll;
using namespace std;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;

void solve() {

  ll N, pop_count;
  string X;
  cin >> N >> X;
  vl ans(N, 1);
  vl A(N);

  ll cnt = 0;

  ll M0 = 0;
  ll M1 = 0;
  ll temp0 = 1;
  ll temp1 = 1;

  rep(i, N) cnt += X[i] == '1';

  rep(i, N) {
    if (X[N - 1 - i] == '1') {
      if (cnt != 1)
        M0 = (M0 + temp0) % (cnt - 1);
      M1 = (M1 + temp1) % (cnt + 1);
    }
    if (cnt != 1)
      temp0 = (temp0 * 2) % (cnt - 1);
    temp1 = (temp1 * 2) % (cnt + 1);
  }

  temp0 = 1;
  temp1 = 1;

  rep(i, N) {

    if (X[N - 1 - i] == '0')
      A[N - 1 - i] = (M1 + temp1) % (cnt + 1);

    else {
      if (cnt == 1) {
        A[i] = 0;
        ans[N - 1 - i] -= 1;
      } else {
        A[N - 1 - i] = (M0 - temp0) % (cnt - 1);
        A[N - 1 - i] = (A[N - 1 - i] + (cnt - 1)) % (cnt - 1);
      }
    }

    if (cnt != 1)
      temp0 = (temp0 * 2) % (cnt - 1);
    temp1 = (temp1 * 2) % (cnt + 1);
  }

  rep(i, N) {

    while (A[i]) {

      pop_count = __builtin_popcount(A[i]);
      A[i] %= pop_count;
      ans[i] += 1;
    }
  }

  rep(i, N) print(ans[i]);
}

int main() {

  fastio;
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF pow(10, 9) + 7
#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep2(i, j, k) for (ll i = j; i < k; i++)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define chmax(x, y) x = max(x, y)
#define print(x) cout << x << "\\n"
#define ALL(v) v.begin(), v.end()
#define SUM(v) accumulate(ALL(v), 0)
#define MAX(v) *max_element(ALL(v))
#define MIN(v) *min_element(ALL(v))
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
typedef long long int ll;
using namespace std;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vs = vector<string>;
using vvs = vector<vector<string>>;

ll popcount(ll 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);
}

void solve() {

  ll N;
  string X;
  cin >> N >> X;
  vl ans(N, 0);
  vl A(N);

  ll cnt = 0;
  ll init = 0;
  ll temp = 1;

  rep(i, N) {
    if (X[N - 1 - i] == '1') {
      init += temp;
      cnt += 1;
    }
    temp *= 2;
  }

  temp = 1;
  rep(i, N) {
    if (X[N - 1 - i] == '0')
      A[N - 1 - i] = init + temp;
    else
      A[N - 1 - i] = init - temp;
    temp *= 2;
  }

  rep(i, N) {
    if (X[i] == '0') {
      A[i] %= (cnt + 1);
      ans[i] += 1;
    } else {
      if (cnt == 1)
        A[i] = 0;
      else {
        A[i] %= (cnt - 1);
        ans[i] += 1;
      }
    }
  }

  rep(i, N) {

    while (A[i]) {

      ll pop_count = popcount(A[i]);
      A[i] %= pop_count;
      ans[i] += 1;
    }
  }

  rep(i, N) print(ans[i]);
}

int main() {

  fastio;
  solve();

  return 0;
}