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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \\
  ASC((x));                                                                    \\
  REV((x))
#define pb push_back
#define eb emplace_back

ll modbit(string S, ll n) {
  ll N = SZ(S);
  ll d = 1, m = 0;
  REP(i, 0, N) {
    m = (m + d * (S[N - i - 1] - '0')) % n;
    d = (d * 2) % n;
  }
  return m;
}

vec modbit2(ll N, ll n) {
  vec A(N, 0);
  ll d = 1;
  REP(i, 0, N) {
    A[i] = d;
    d = (d * 2) % n;
  }
  return A;
}

ll popcount(ll n) {
  ll cnt = 0;
  REP(i, 0, 20) {
    if ((n >> i) & 1 == 1)
      ++cnt;
  }
  return cnt;
}

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

  ll n0 = 0;
  REP(i, 0, N) {
    if (S[i] == '1')
      ++n0;
  }

  if (n0 > 1) {
    ll X0 = modbit(S, n0 + 1);
    ll X1 = modbit(S, n0 - 1);
    vec Y0 = modbit2(N, n0 + 1);
    vec Y1 = modbit2(N, n0 - 1);
    REV(Y0);
    REV(Y1);

    ll cnt, n;
    REP(i, 0, N) {
      if (S[i] == '0') {
        n = (X0 + Y0[i]) % (n0 + 1);
      } else {
        n = (X1 - Y1[i]) % (n0 - 1);
        n = (n < 0 ? n + n0 - 1 : n);
      }
      cnt = 1;
      while (n > 0) {
        n = n % popcount(n);
        ++cnt;
      }
      PR(cnt);
    }
  } else {
    ll X0 = modbit(S, n0 + 1);
    vec Y0 = modbit2(N, n0 + 1);
    REV(Y0);

    ll cnt, n;
    REP(i, 0, N) {
      if (S[i] == '0') {
        n = (X0 + Y0[i]) % (n0 + 1);
        cnt = 1;
      } else {
        n = 0;
        cnt = 0;
      }
      while (n > 0) {
        n = n % popcount(n);
        ++cnt;
      }
      PR(cnt);
    }
  }

  return 0;
}

/*



*/

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \\
  ASC((x));                                                                    \\
  REV((x))
#define pb push_back
#define eb emplace_back

ll modbit(string S, ll n) {
  ll N = SZ(S), d = 1, m = 0;
  REP(i, 0, N) {
    m = (m + d * (ll)(S[N - i - 1] - '0')) % n;
    d = (d * 2) % n;
  }
  return m;
}

ll popcount(ll n) {
  ll cnt = 0;
  REP(i, 0, 20) {
    if ((n >> i) & 1 == 1)
      ++cnt;
  }
  return cnt;
}

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

  ll n0 = 0;
  REP(i, 0, N) {
    if (S[i] == '1')
      ++n0;
  }

  ll cnt, n;
  REP(i, 0, N) {
    n = n0;
    if (S[i] == '0') {
      S[i] = '1';
      ++n;
    } else {
      S[i] = '0';
      --n;
    }
    cnt = 0;
    if (n > 0) {
      n = modbit(S, n);
      ++cnt;
    }
    while (n > 0) {
      n = n % popcount(n);
      ++cnt;
    }
    PR(cnt);
    if (S[i] == '0') {
      S[i] = '1';
    } else {
      S[i] = '0';
    }
  }

  return 0;
}

/*



*/