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

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \\
  do {                                                                         \\
    cout << "[";                                                               \\
    for (int count = (a); count < (b); ++count)                                \\
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \\
    cout << "]\\n";                                                             \\
  } while (0)
#define p_map(map, it)                                                         \\
  do {                                                                         \\
    cout << "{";                                                               \\
    for (auto(it) = map.begin();; ++(it)) {                                    \\
      if ((it) == map.end()) {                                                 \\
        cout << "}\\n";                                                         \\
        break;                                                                 \\
      } else                                                                   \\
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \\
    }                                                                          \\
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\\n';

int main() {
  int n;
  string x;
  cin >> n >> x;
  int cnt = 0;
  for (int i = 0; i < n; ++i)
    cnt += (x[i] == '1');
  reverse(x.begin(), x.end());
  vector<int> m1(n, 0), m2(n, 0);
  int sum1 = 0, sum2 = 0;
  m1[0] = m2[0] = 1;
  if (cnt - 1)
    for (int i = 1; i < n; ++i)
      m1[i] = m1[i - 1] * 2 % (cnt - 1);
  for (int i = 1; i < n; ++i)
    m2[i] = m2[i - 1] * 2 % (cnt + 1);
  if (cnt - 1)
    for (int i = 0; i < n; ++i)
      if (x[i] == '1')
        (sum1 += m1[i]) %= cnt - 1;
  for (int i = 0; i < n; ++i)
    if (x[i] == '1')
      (sum2 += m2[i]) %= cnt + 1;
  for (int j = 0; j < n; ++j) {
    int i = n - 1 - j;
    int ans = 0, mod, sum;
    if (x[i] == '0') {
      mod = cnt + 1;
      sum = sum2 + m2[i];
      ans++;
    } else {
      mod = cnt - 1;
      sum = sum1 - m1[i] + mod;
      if (mod)
        ans++;
    }
    if (mod)
      sum %= mod;
    while (sum && mod) {
      int d = 0;
      for (int j = 0; j < 20; ++j)
        if ((sum >> j) & 1)
          d++;
      sum %= d;
      ans++;
    }
    cout << ans << newl;
  }
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> P;

#define p_ary(ary, a, b)                                                       \\
  do {                                                                         \\
    cout << "[";                                                               \\
    for (int count = (a); count < (b); ++count)                                \\
      cout << ary[count] << ((b)-1 == count ? "" : ", ");                      \\
    cout << "]\\n";                                                             \\
  } while (0)
#define p_map(map, it)                                                         \\
  do {                                                                         \\
    cout << "{";                                                               \\
    for (auto(it) = map.begin();; ++(it)) {                                    \\
      if ((it) == map.end()) {                                                 \\
        cout << "}\\n";                                                         \\
        break;                                                                 \\
      } else                                                                   \\
        cout << "" << (it)->first << "=>" << (it)->second << ", ";             \\
    }                                                                          \\
  } while (0)

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &a) {
  os << "(" << a.first << ", " << a.second << ")";
  return os;
}

const char newl = '\\n';

int main() {
  int n;
  string x;
  cin >> n >> x;
  int cnt = 0;
  for (int i = 0; i < n; ++i)
    cnt += (x[i] == '1');
  reverse(x.begin(), x.end());
  vector<int> m1(n, 0), m2(n, 0);
  int sum1 = 0, sum2 = 0;
  m1[0] = m2[0] = 1;
  if (cnt - 1)
    for (int i = 1; i < n; ++i)
      m1[i] = m1[i - 1] * 2 % (cnt - 1);
  for (int i = 1; i < n; ++i)
    m2[i] = m2[i - 1] * 2 % (cnt + 1);
  if (cnt - 1)
    for (int i = 0; i < n; ++i)
      if (x[i] == '1')
        (sum1 += m1[i]) %= cnt - 1;
  for (int i = 0; i < n; ++i)
    if (x[i] == '1')
      (sum2 += m2[i]) %= cnt + 1;
  for (int j = 0; j < n; ++j) {
    int i = n - 1 - j;
    int ans = 0, mod, sum;
    if (x[i] == '0') {
      mod = cnt + 1;
      sum = sum2 + m2[i];
      ans++;
    } else {
      mod = cnt - 1;
      sum = sum1 - m1[i] + mod;
      if (mod)
        ans++;
    }
    if (mod)
      sum %= mod;
    while (sum) {
      int d = 0;
      for (int j = 0; j < 20; ++j)
        if ((sum >> j) & 1)
          d++;
      sum %= d;
      ans++;
    }
    cout << ans << newl;
  }
}