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

/*
        Nishant Singh
        nishant.ns19
*/
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
// ******** Big Integer ********
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// #define ll1024  int1024_t
// ******** Big Integer ********
// ******** Using PBDS ********
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ook order_of_key    //count of strictly smaller elements
// #define fbo find_by_order   //find element at an index
// ******** Using PBDS ********
using namespace std;
#define ll long long
#define ld long double
#define pll pair<long long, long long>
#define pii pair<int, int>
#define vll vector<ll>
#define vec vector
#define setcount(x) __builtin_popcountll(x)
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REV(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb push_back
#define in insert
#define init(x, a) memset(x, a, sizeof(x))
#define all(c) c.begin(), c.end()
#define pq priority_queue
#define ffl fflush(stdout)
#define mset multiset
#define out(x)                                                                 \\
  cout << #x << " = "                                                          \\
       << " " << x << "\\n";
#define ub upper_bound
#define lb lower_bound
#define ln "\\n"
#define Y "YES"
#define N "NO"
ll const inf = 2e18;
// ******** Using PBDS ********
// typedef tree<long long, null_type, less<long long>, rb_tree_tag,
//              tree_order_statistics_node_update>
//                 os;
// ******** Using PBDS ********

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll modexp(ll x, ll n, ll mod) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    return (modexp((x * x) % mod, n / 2, mod) % mod * x % mod) % mod;
  } else {
    return (modexp((x * x) % mod, n / 2, mod) % mod);
  }
}
inline ll add(ll a, ll b, ll mod) { return (a % mod + b % mod + mod) % mod; }
inline ll sub(ll a, ll b, ll mod) { return (a % mod - b % mod + mod) % mod; }
// ******** GLOBAL VARIABLES ********
ll n;
// ******** GLOBAL VARIABLES ********
void solve() {
  string x;
  cin >> n >> x;
  ll cur = 0;
  ll ans[n];
  // cout<<cur<<ln;
  ll cnt = 0;
  ll mod1 = 0;
  ll mod2 = 0;

  REP(i, n) {
    if (x[i] == '1')
      cnt++;
  }

  REP(i, n) {
    mod1 = (mod1 * (2LL)) + (x[i] - '0');
    mod1 = mod1 % (cnt + 1);
  }
  if (cnt != 1) {
    REP(i, n) {
      mod2 = (mod2 * (2LL)) + (x[i] - '0');
      mod2 = mod2 % (cnt - 1);
    }
  }

  REP(i, n) {
    if (x[i] == '1') {
      if (cnt == 1) {
        ans[i] = 0;
        continue;
      }
      cnt--;
      ll temp = sub(mod2, modexp(2, n - i - 1, cnt), cnt);
      ll steps = 1;
      while (temp) {
        temp = temp % (setcount(temp));
        steps++;
      }
      cnt++;
      ans[i] = steps;
    } else {
      cnt++;
      ll temp = add(mod1, modexp(2, n - i - 1, cnt), cnt);
      ll steps = 1;
      while (temp) {
        temp = temp % (setcount(temp));
        steps++;
      }
      cnt--;
      ans[i] = steps;
    }
  }
  REP(i, n) { cout << ans[i] << ln; }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = 1;
  // cin>>T;
  for (int t = 1; t <= T; t++) {
    // cout<<"Case #"<<t<<": ";
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
        Nishant Singh
        nishant.ns19
*/
#include <bits/stdc++.h>
#include <functional>
#include <iostream>
// ******** Big Integer ********
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
// #define ll1024  int1024_t
// ******** Big Integer ********
// ******** Using PBDS ********
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ook order_of_key    //count of strictly smaller elements
// #define fbo find_by_order   //find element at an index
// ******** Using PBDS ********
using namespace std;
#define ll long long
#define ld long double
#define pll pair<long long, long long>
#define pii pair<int, int>
#define vll vector<ll>
#define vec vector
#define setcount(x) __builtin_popcountll(x)
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REV(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb push_back
#define in insert
#define init(x, a) memset(x, a, sizeof(x))
#define all(c) c.begin(), c.end()
#define pq priority_queue
#define ffl fflush(stdout)
#define mset multiset
#define out(x)                                                                 \\
  cout << #x << " = "                                                          \\
       << " " << x << "\\n";
#define ub upper_bound
#define lb lower_bound
#define ln "\\n"
#define Y "YES"
#define N "NO"
ll const mod = 1e9 + 7;
ll const inf = 2e18;
// ******** Using PBDS ********
// typedef tree<long long, null_type, less<long long>, rb_tree_tag,
//              tree_order_statistics_node_update>
//                 os;
// ******** Using PBDS ********
inline ll add(ll a, ll b) { return (a % mod + b % mod + mod) % mod; }
inline ll mul(ll a, ll b) { return (a % mod * b % mod + mod) % mod; }
inline ll sub(ll a, ll b) { return (a % mod - b % mod + mod) % mod; }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll modexp(ll x, ll n) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
  } else {
    return (modexp((x * x) % mod, n / 2) % mod);
  }
}
ll modinv(ll n) { return modexp(n, mod - 2); }
// ******** GLOBAL VARIABLES ********
ll n;
// ******** GLOBAL VARIABLES ********
ll fun(string x, ll cnt) {
  if (cnt == 0)
    return 0;
  ll z = 0;
  for (ll i = 0; i < n; i++) {
    z = (z * (2LL)) + (x[i] - '0');
    z = z % cnt;
  }
  ll ans = 1;
  while (z != 0) {
    z = (z % (setcount(z)));
    ans++;
  }
  return ans;
}
void solve() {
  string x;
  cin >> n >> x;
  ll cur = 0;
  ll ans[n];
  // cout<<cur<<ln;
  ll cnt = 0;
  REP(i, n) {
    if (x[i] == '1')
      cnt++;
  }
  REP(i, n) {
    if (x[i] == '1') {
      x[i] = '0';
      cnt--;
      ans[i] = fun(x, cnt);
      cnt++;
      x[i] = '1';
    } else {
      x[i] = '1';
      cnt++;
      ans[i] = fun(x, cnt);
      cnt--;
      x[i] = '0';
    }
  }
  REP(i, n) { cout << ans[i] << ln; }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll T = 1;
  // cin>>T;
  for (int t = 1; t <= T; t++) {
    // cout<<"Case #"<<t<<": ";
    solve();
  }
  return 0;
}