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

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\\n"
#define ld long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rep_a(i, a, n) for (int i = a; i < n; i++)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
typedef priority_queue<ll> pq;
#define o_set                                                                  \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define graph vector<vi>

const ll mod = 1000000007;
const ll INF = (ll)1e18;
const ll MAXN = 1000006;

ll po(ll x, ll n, ll m) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = (ans * x) % m;
    x = (x * x) % m;
    n /= 2;
  }
  return ans;
}

pair<int, int> fun(string &s) {}

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

  int T = 1;
  // cin >> T;
  while (T--) {
    ll n;
    string s;
    cin >> n >> s;

    int x = 0;
    pair<int, int> p;
    rep(i, s.length()) if (s[i] == '1') x++;
    ll z = 0, y = 0;
    rep(i, s.length()) {
      z = z * 2 + (int)(s[i] - '0');
      y = y * 2 + (int)(s[i] - '0');
      if (x > 1)
        z %= (x - 1);
      y %= (x + 1);
    }
    if (x > 1)
      p = mp(z, y);
    else if (x == 1)
      p = mp(-1, 0);
    else
      p = mp(-1, -1);

    int ans;
    rep(i, n) {
      if (s[i] == '0') {
        if (p.ss == -1) {
          cout << 1 << el;
        } else {
          z = (p.ss + po(2ll, (ll)n - i - 1, (ll)x + 1)) % (x + 1);
          ans = 1;
          while (z > 0) {
            y = __builtin_popcount(z);
            z %= y;
            ans++;
          }
          cout << ans << el;
        }
      } else {
        if (p.ff == -1)
          cout << 0 << el;
        else {
          z = (p.ff - po(2ll, (ll)(n - i - 1), (ll)(x - 1)) + x - 1) % (x - 1);
          ans = 1;
          while (z > 0) {
            y = __builtin_popcount(z);
            z %= y;
            ans++;
          }
          cout << ans << el;
        }
      }
    }
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
  return 0;
}

Error: TLE

Fixed submission:

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define el "\\n"
#define ld long double
#define rep(i, n) for (int i = 0; i < n; i++)
#define rev(i, n) for (int i = n; i >= 0; i--)
#define rep_a(i, a, n) for (int i = a; i < n; i++)
#define all(ds) ds.begin(), ds.end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
typedef priority_queue<ll> pq;
#define o_set                                                                  \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define graph vector<vi>

const ll mod = 1000000007;
const ll INF = (ll)1e18;
const ll MAXN = 1000006;

ll po(ll x, ll n) {
  ll ans = 1;
  while (n > 0) {
    if (n & 1)
      ans = (ans * x) % mod;
    x = (x * x) % mod;
    n /= 2;
  }
  return ans;
}

int fun(string &s) {
  int x = 0;
  rep(i, s.length()) if (s[i] == '1') x++;
  ll z = 0;
  if (x == 0)
    return -1;
  rep(i, s.length()) {
    z = z * 2 + (int)(s[i] - '0');
    z %= x;
  }
  return z;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r" , stdin);
  // freopen("output.txt", "w" , stdout);
  // #endif
  int T = 1;
  // cin >> T;
  while (T--) {
    ll n;
    string s;
    cin >> n >> s;
    int ans;
    int z, x;
    rep(i, n) {
      if (s[i] == '0')
        s[i] = '1';
      else
        s[i] = '0';

      z = fun(s);

      if (z == -1) {
        cout << 0 << el;
      } else {
        ans = 1;
        while (z > 0) {
          x = __builtin_popcount(z);
          z %= x;
          ans++;
        }
        cout << ans << el;
      }

      if (s[i] == '0')
        s[i] = '1';
      else
        s[i] = '0';
    }
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
  return 0;
}