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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

ll me(ll x, ll y, ll M) {
  if (y == 0) {
    return 1;
  } else {
    if (y % 2 == 0) {
      return me((x * x) % M, y / 2, M);
    } else {
      return (x * me((x * x) % M, (y - 1) / 2, M)) % M;
    }
  }
}

int binStringtodec(string s, int x) {
  int val = 0;
  for (auto &it : s) {
    val = val * 2 + (it - '0');
    val = val % x;
  }
  return val;
}

int find_mod(int x, int M) {
  if (x >= 0)
    return x % M;
  return (M + x % M) % M;
}

int32_t main() {
  IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    int n;
    string s;
    cin >> n;
    cin >> s;
    int cou = 0;
    for (char c : s) {
      if (c == '1')
        cou++;
    }
    int cou1 = cou - 1;
    int cou2 = cou + 1;
    int val1 = -1, val2 = -1;
    if (cou1 > 0) {
      val1 = binStringtodec(s, cou1);
    }
    val2 = binStringtodec(s, cou2);
    rep(i, 0, n - 1) {
      int couu = cou;
      if (s[i] == '1')
        couu--;
      else
        couu++;
      if (couu <= 0) {
        cout << "0" << endl;
        continue;
      }
      int val;
      if (couu == cou1)
        val = val1;
      else
        val = val2;
      int idx = n - i - 1;
      int f = me(2, idx, couu);
      int x;
      if (s[i] == '0')
        x = val + f;
      else
        x = val - f;
      x = find_mod(x, couu);
      // deb1(x);
      int c = 1;
      while (x > 0) {
        int f = __builtin_popcountll(x);
        x = x % f;
        c++;
      }
      cout << c << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define double long double
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define endl "\\n"
#define repp(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define brep(i, a, b) for (int i = a; i >= b; i--)
#define deb1(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)                                                          \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define trace(v)                                                               \\
  for (auto it = v.begin(); it != v.end(); it++)                               \\
    cout << *it << " ";                                                        \\
  cout << endl;
#define tracearr(a, l, r)                                                      \\
  for (int iii = l; iii <= r; iii++)                                           \\
    cout << a[iii] << " ";                                                     \\
  cout << endl;
#define PI 3.1415926535897932384626
#define F first
#define S second
#define clr(x, y) memset(x, y, sizeof(x))
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are
// strictly smaller than x

const int N = 1e5 + 5;
const int MOD = 1e9 + 7;

int binStringtodec(string s, int x) {
  int val = 0;
  for (auto &it : s) {
    val = val * 2 + (it - '0');
    val = val % x;
  }
  return val;
}

int32_t main() {
  IOS;
  int T = 1;
  // cin >> T;
  //  int numOfSetBits = __builtin_popcountll(n);
  std::cout << std::fixed << std::setprecision(12);
  while (T--) {
    int n;
    string s;
    cin >> n;
    cin >> s;
    int cou = 0;
    for (char c : s) {
      if (c == '1')
        cou++;
    }
    rep(i, 0, n - 1) {
      if (s[i] == '1') {
        s[i] = '0';
        cou--;
      } else {
        s[i] = '1';
        cou++;
      }
      if (cou == 0) {
        cout << "0" << endl;
        if (s[i] == '0') {
          s[i] = '1';
          cou++;
        } else {
          s[i] = '0';
          cou--;
        }
        continue;
      }
      int x = binStringtodec(s, cou);
      // deb1(x);
      int c = 1;
      while (x > 0) {
        int f = __builtin_popcountll(x);
        x = x % f;
        c++;
      }
      cout << c << endl;
      if (s[i] == '0') {
        s[i] = '1';
        cou++;
      } else {
        s[i] = '0';
        cou--;
      }
    }
  }
}