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

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
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;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) { is >> v[i]; }
  return is;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int n;
  string x;
  cin >> n >> x;
  VV tri(n, V(3, INFLL)); //+1,turnal, -1 on mod each posit;
  int mod = 0;
  rep(i, n) { mod += (x[i] == '1'); }
  //	clog <<"mod:"<< mod << endl;
  rep(j, 3) {
    if (mod - j + 1 <= 0)
      continue;
    rrep(i, n) {
      if (i == n - 1) {
        tri[i][j] = 1LL % (mod - j + 1);
      } else {
        tri[i][j] = tri[i + 1][j] * 2LL;
        tri[i][j] %= (mod - j + 1);
      }
    }
  }
  ll up = 0LL;
  ll down = 0LL;
  rep(i, n) {
    if (x[i] == '1') {
      up += tri[i][0];
      down += tri[i][2];
    }
  }
  ll cnt = 0LL;
  int count = 0;
  int id;
  rep(i, n) {
    cnt = 0LL;
    count = 0LL;

    if ((mod == 1) and x[i] == '1') {
      cout << 0 << endl;
      continue;
    }
    if (x[i] == '1') {
      cnt = down;
      cnt -= tri[i][2];
    } else {
      cnt = up;
      cnt += tri[i][0];
    }
    cnt %= (x[i] == '0' ? mod + 1 : mod - 1);
    count = 1;
    // clog << cnt << endl;
    while (cnt != 0LL) {
      count++;
      cnt %= __builtin_popcount(cnt);
      //	clog << count << " " << cnt << endl;
    }
    cout << count << endl;
    // clog << "---" << endl;
  }
  // clog << tri << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \\
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
string solve(bool a) { return ((a) ? "Yes" : "No"); }
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
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;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  rep(i, v.size()) { is >> v[i]; }
  return is;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
int main() {
  int n;
  string x;
  cin >> n >> x;
  VV tri(n, V(3, INFLL)); //+1,turnal, -1 on mod each posit;
  int mod = 0;
  rep(i, n) { mod += (x[i] == '1'); }
  // clog <<"mod:"<< mod << endl;
  rep(j, 3) {
    if (mod - j + 1 <= 0)
      continue;
    rrep(i, n) {
      if (i == n - 1) {
        tri[i][j] = 1LL % (mod - j + 1);
      } else {
        tri[i][j] = tri[i + 1][j] * 2LL;
        tri[i][j] %= (mod - j + 1);
      }
    }
  }
  ll cnt = 0LL;
  int count = 0;
  int id;
  rep(i, n) {
    cnt = 0LL;
    count = 0LL;
    if (i != 0) {
      if (x[i - 1] == '1') {
        x[i - 1] = '0';
      } else {
        x[i - 1] = '1';
      }
    }
    if (x[i] == '0') {
      x[i] = '1';
      id = 0;
    } else {
      x[i] = '0';
      id = 2;
    }

    rep(j, n) {
      if (tri[j][id] == INFLL) {
        count = INF;
        break;
      }
      if (x[j] == '1') {
        cnt += tri[j][id];
      }
    }
    if (count == INF) {
      cout << 0 << endl;
      continue;
    }
    cnt %= (id == 0 ? mod + 1 : mod - 1);
    count = 1;
    //	clog << cnt << endl;
    while (cnt != 0LL) {
      count++;
      cnt %= __builtin_popcount(cnt);
      //	clog << count << " " << cnt << endl;
    }
    cout << count << endl;
    //	clog << "---" << endl;
  }
  //	clog << tri << endl;
}