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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Main {
  int N;
  string X;
  void input() { cin >> N >> X; }
  void output() {
    bitset<200000> bs(X);
    int total = bs.count();
    vector<int> popcounts = {total + 1, total - 1};
    vector<vector<int>> add(2, vector<int>(N));
    vector<ll> preprocessed(2);
    REP(i, 2) {
      if (popcounts[i] <= 0)
        break;
      add[i][0] = 1 % popcounts[i];
      preprocessed[i] = bs[0] * add[i][0];
      REP(j, N - 1) {
        add[i][j + 1] = add[i][j] * 2 % popcounts[i];
        preprocessed[i] =
            (preprocessed[i] + bs[j + 1] * add[i][j + 1]) % popcounts[i];
      }
    }
    REPR(j, N) {
      int i = bs[j];
      if (popcounts[i] <= 0) {
        cout << 0 << endl;
        continue;
      }
      int res = 1;
      int x = preprocessed[i];
      if (i == 0) {
        x = (x + add[i][j]) % popcounts[i];
      } else {
        x = (x - add[i][j] + popcounts[i]) % popcounts[i];
      }
      while (x > 0) {
        bitset<20> bit = x;
        ll sum = 0;
        REP(k, 20) {
          sum = (sum + bit[k] * (1 << k) % bit.count()) % bit.count();
        }
        x = sum;
        ++res;
      }
      cout << res << endl;
    }
  }

public:
  Main() {
    input();
    output();
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  Main();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

class Main {
  int N;
  string X;
  void input() { cin >> N >> X; }
  void output() {
    REP(i, N) {
      string s = X;
      s[i] = s[i] == '0' ? '1' : '0';
      bitset<200000> bs(s);
      int res = 0;
      while (bs.count()) {
        ll x = 0;
        int cnt = 0;
        int add = 1;
        REP(i, 200000) {
          if (bs[i]) {
            ++cnt;
            x += add;
            x %= bs.count();
            if (cnt == bs.count())
              break;
          }
          add = add * 2 % bs.count();
        }
        bs = x;
        ++res;
      }
      cout << res << endl;
    }
  }

public:
  Main() {
    input();
    output();
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  Main();

  return 0;
}