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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

long long power(long long x, long long n, long long mod) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return power(x * x % mod, n / 2, mod);
  else
    return x * power(x, n - 1, mod) % mod;
}

int countbits(long bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  VI x(n);
  REP(i, 0, n) x[i] = s[i] - '0';
  int cnt = 0;
  REP(i, 0, n) cnt += x[i];

  VI dp(n + 1);
  REP(i, 1, n + 1) {
    int v = countbits(i);
    dp[i] = dp[i % v] + 1;
  }

  LL tl = 0, ts = 0, sum = 0;
  REP(i, 0, n) {
    if (x[i] == 1) {
      tl += power(2, n - 1 - i, cnt + 1);
      tl %= cnt + 1;
      if (cnt - 1 <= 0)
        continue;
      ts += power(2, n - 1 - i, cnt - 1);
      ts %= cnt - 1;
    }
    sum += (x[i] << i);
  }
  dump(tl);
  dump(ts);
  dump(sum);

  REP(i, 0, n) {
    if (x[i] == 1) {
      if (cnt == 1) {
        cout << 0 << endl;
        continue;
      }
    }

    LL v;
    if (x[i] == 1) {
      v = (ts - power(2, n - 1 - i, cnt - 1) + (cnt - 1)) % (cnt - 1);
    } else {
      v = (tl + power(2, n - 1 - i, cnt + 1)) % (cnt + 1);
    }
    // dump(v);
    cout << dp[v % (cnt + (x[i] == 1 ? -1 : 1))] + 1 << endl;
  }

  // REP(i,0,n+1) cerr << i << ": " << dp[i] << endl;
}

signed main() {
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

long long power(long long x, long long n, long long mod) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return power(x * x % mod, n / 2, mod);
  else
    return x * power(x, n - 1, mod) % mod;
}

int countbits(long bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  VI x(n);
  REP(i, 0, n) x[i] = s[i] - '0';
  int cnt = 0;
  REP(i, 0, n) cnt += x[i];

  VI dp(n + 1);
  REP(i, 1, n + 1) {
    int v = countbits(i);
    dp[i] = dp[i % v] + 1;
  }

  REP(i, 0, n) {
    LL v = 0;
    int b = cnt;
    if (x[i] == 1) {
      if (b == 1) {
        cout << 0 << endl;
        continue;
      } else {
        --b;
      }
    } else {
      ++b;
    }
    LL tmp = 0;
    REP(j, 0, n) {
      if (j == i) {
        if (x[j] == 0) {
          v += power(2, n - 1 - j, b);
          v %= b;
          tmp += (1 << j);
        }
      } else {
        if (x[j] == 1) {
          v += power(2, n - 1 - j, b);
          v %= b;
          tmp += (1 << j);
        }
      }
    }
    v %= b;

    // cerr << i << ": " << v << ", " << tmp << ", " << tmp % b << endl;
    cout << dp[v] + 1 << endl;
  }

  // REP(i,0,n+1) cerr << i << ": " << dp[i] << endl;
}

signed main() {
  solve();

  return 0;
}