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

#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define mod 1000000007ll
#define mod1 998244353ll
#define endl '\\n'
#define inf ((ll)1e18 + 1)
#include <chrono>
using namespace chrono;
mt19937_64 rng(high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>
#define debr(x, y)                                                             \\
  {                                                                            \\
    cout << "-->  " << #x << " to " << #y << " :  ";                           \\
    auto itr = x;                                                              \\
    while (itr != y)                                                           \\
      cout << (*itr++) << ' ';                                                 \\
    cout << endl;                                                              \\
  }
#define deba(x)                                                                \\
  {                                                                            \\
    cout << "-->  " << #x << " :  ";                                           \\
    for (auto ele : (x))                                                       \\
      cout << ele << "  ";                                                     \\
    cout << endl;                                                              \\
  }
#define deb(args...)                                                           \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "  ,  ";
  err(++it, args...);
}

ll solve(ll n) {
  if (n == 0)
    return 0;
  ll how = __builtin_popcountll(n);
  return 1 + solve(n % how);
}

ll power(ll a, ll b, ll modu) {
  if (b == 0)
    return 1;
  ll temp = power(a, b / 2, modu);
  temp = (temp * temp) % modu;
  if (b % 2) {
    temp = (temp * a) % modu;
  }
  return temp;
}

ll fun1(string &s, vector<ll> &p, ll n, ll modu) {
  ll val = 0;
  ll pow = 1 % modu;
  for (ll x = n - 1; x >= 0; x--) {
    if (s[x] == '1') {
      val += pow;
      val %= modu;
    }
    p[n - 1 - x] = pow;
    pow *= 2;
    pow %= modu;
  }
  return val;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll c = 0;

  ll valc = 0, valc_min1 = 0, valc_plus1 = 0;
  for (char ch : s) {
    if (ch == '1') {
      c++;
    }
  }
  vector<ll> pc(n), pc_min1(n), pc_plus1(n);
  if (c > 0) {
    valc = fun1(s, pc, n, c);
    if (c > 1) {
      valc_min1 = fun1(s, pc_min1, n, c - 1);
    }
  }
  valc_plus1 = fun1(s, pc_plus1, n, c + 1);
  // deb(c)

  // deb(valc,valc_min1,valc_plus1)

  ll ori = c;
  for (ll x = 0; x < n; x++) {
    ll val = 0;
    if (s[x] == '1') {
      s[x] = '0';
      c--;
      if (c == ori) {
        val = valc;
        val -= pc[n - 1 - x];
      } else if (c == ori - 1) {
        val = valc_min1;
        val -= pc_min1[n - 1 - x];
      } else if (c == ori + 1) {
        val = valc_plus1;
        val -= pc_plus1[n - 1 - x];
      }
      if (c) {
        val += c;
        val %= c;
      }
    } else {
      s[x] = '1';
      c++;
      if (c == ori) {
        val = valc;
        val += pc[n - 1 - x];
      } else if (c == ori - 1) {
        val = valc_min1;
        val += pc_min1[n - 1 - x];
      } else if (c == ori + 1) {
        val = valc_plus1;
        val += pc_plus1[n - 1 - x];
      }
      if (c) {
        val %= c;
      }
    }
    if (c) {
      ll ans = solve(val) + 1;
      cout << ans << endl;
    } else {
      cout << 0 << endl;
    }
    if (s[x] == '1') {
      s[x] = '0';
      c--;
    } else {
      s[x] = '1';
      c++;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define mod 1000000007ll
#define mod1 998244353ll
#define endl '\\n'
#define inf ((ll)1e18 + 1)
#include <chrono>
using namespace chrono;
mt19937_64 rng(high_resolution_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>
#define debr(x, y)                                                             \\
  {                                                                            \\
    cout << "-->  " << #x << " to " << #y << " :  ";                           \\
    auto itr = x;                                                              \\
    while (itr != y)                                                           \\
      cout << (*itr++) << ' ';                                                 \\
    cout << endl;                                                              \\
  }
#define deba(x)                                                                \\
  {                                                                            \\
    cout << "-->  " << #x << " :  ";                                           \\
    for (auto ele : (x))                                                       \\
      cout << ele << "  ";                                                     \\
    cout << endl;                                                              \\
  }
#define deb(args...)                                                           \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << "  ,  ";
  err(++it, args...);
}

ll solve(ll n) {
  if (n == 0)
    return 0;
  ll how = __builtin_popcountll(n);
  return 1 + solve(n % how);
}

ll oper(string &s) {
  ll c = 0;
  for (char ch : s) {
    if (ch == '1')
      c++;
  }
  if (c == 0)
    return 0;
  ll val = 0;
  ll pow = 1;
  for (ll x = s.length() - 1; x >= 0; x--) {
    if (s[x] == '1') {
      val += pow;
      val %= c;
    }
    pow *= 2;
  }
  ll ans = 1;
  ans += solve(val);
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n;
  cin >> n;
  string s;
  cin >> s;
  for (ll x = 0; x < n; x++) {
    if (s[x] == '1')
      s[x] = '0';
    else
      s[x] = '1';
    cout << oper(s) << endl;
    if (s[x] == '1')
      s[x] = '0';
    else
      s[x] = '1';
  }
  return 0;
}