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

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define loop(i, b, n) for (int i = b; i < (n); ++i)
#define each(e, v) for (auto &&e : v)

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)v.size()

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const double EPS = 1e-9;
const ll MOD = 1e+9 + 7;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }

vector<int> dp;

int mod_popcount(int x) {
  if (x == 0)
    return 0;
  int count = 0;
  for (int i = 0; i < 32; ++i) {
    if ((x & (1 << i)) != 0)
      ++count;
  }
  return x % count;
}

int main() {
  int n;
  string x;
  cin >> n >> x;

  int count = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1')
      ++count;
  }

  dp = vector<int>(count + 2, -1);
  dp[0] = 0;
  for (int i = 0; i <= count + 1; ++i) {
    dp[i] = 1 + dp[mod_popcount(i)];
  }

  vector<int> vp0(n, 0);
  vector<int> vp1(n, 0);

  int x0 = 0;
  int x1 = 0;
  int p0 = 1;
  int p1 = 1;
  for (int i = x.size() - 1; i >= 0; --i) {
    if (x[i] == '1') {
      x0 = count > 1 ? (x0 + p0) % (count - 1) : 0;
      x1 = (x1 + p1) % (count + 1);
    }
    vp0[i] = p0;
    vp1[i] = p1;
    p0 = count > 1 ? (p0 * 2) % (count - 1) : 0;
    p1 = (p1 * 2) % (count + 1);
  }

  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      if (count <= 1) {
        print(0);
        continue;
      }
      int a = (x0 + (count - 1 - vp0[i])) % (count - 1);
      print(dp[a]);
    } else {
      int a = (x1 + vp1[i]) % (count + 1);
      print(dp[a]);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define loop(i, b, n) for (int i = b; i < (n); ++i)
#define each(e, v) for (auto &&e : v)

#define all(v) (v).begin(), (v).end()
#define sz(v) (int)v.size()

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const double EPS = 1e-9;
const ll MOD = 1e+9 + 7;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }

vector<int> dp;

int mod_popcount(int x) {
  if (x == 0)
    return 0;
  int count = 0;
  for (int i = 0; i < 32; ++i) {
    if ((x & (1 << i)) != 0)
      ++count;
  }
  return x % count;
}

int f(const string &x, int count) {
  if (count == 0)
    return 0;

  int a = 0;
  int p2 = 1;
  for (int i = x.size() - 1; i >= 0; --i) {
    if (x[i] == '1')
      a = (a + p2) % count;
    p2 = (p2 * 2) % count;
  }
  return dp[a];
}

int main() {
  int n;
  string x;
  cin >> n >> x;

  int count = 0;
  for (int i = 0; i < n; ++i) {
    if (x[i] == '1')
      ++count;
  }

  dp = vector<int>(count + 2, -1);
  dp[0] = 0;
  for (int i = 0; i <= count + 1; ++i) {
    dp[i] = 1 + dp[mod_popcount(i)];
  }

  for (int i = 0; i < n; ++i) {
    char p = x[i];
    x[i] = p == '0' ? '1' : '0';
    print(f(x, count + (p == '0' ? 1 : -1)));
    x[i] = p;
  }

  return 0;
}