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

#include <bits/stdc++.h>
#define int long long
#define INT_MAX INT64_MAX
#define INT_MIN INT64_MIN
#define PI 3.14159265358979323846
#define CLK clock_t clk = clock(); // Start of main
#define OCLK                                                                   \\
  cerr << "Time (in ms): "                                                     \\
       << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC                    \\
       << '\\n'; // End of main

/* DEFINE cout */
#define pln cout << "===============================\\n"
#define on cout << "\\n"
#define os cout << " "
#define o2(a, b) cout << a << " " << b
#define o(a) cout << a
#define bitcount __builtin_popcount
/* DEFINE cout */
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0); // fastio
using namespace std;
struct hash_pair { // use pair as key in
                   // unordered_map<pair<int,int>,int,hash_pair>;
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
int MOD;
bool prime[10000001];
int P = MOD - 2;
int factorial[10000001] = {0};
int powerFunction(int x, int y) {
  int res = 1;
  int p = MOD;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void sieveFunction(int maxLimit) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; maxLimit >= i; i++) {
    if (prime[i]) {
      for (int j = 2 * i; maxLimit >= j; j += i)
        prime[j] = false;
    }
  }
}
void factorialFunction(int maxLimit) {
  factorial[0] = 1;
  for (int i = 1; i <= maxLimit; i++)
    factorial[i] = (factorial[i - 1] * i) % MOD;
  return;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
signed main() {
  fastio;
  int n;
  cin >> n;
  string str;
  cin >> str;
  int ones = 0;
  for (int i = 0; n > i; i++) {
    if (str[i] == '1') {
      ones++;
    }
  }

  int res1 = 0, res2 = 0;
  int pw1 = 1, pw2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (str[i] == '1') {
      res1 += pw1;
      res1 %= (ones + 1);
      if (ones - 1 != 0) {
        res2 += pw2;
        res2 %= (ones - 1);
      }
    }
    pw1 *= 2;
    pw1 %= (ones + 1);
    if (ones - 1 != 0) {

      pw2 *= 2;
      pw2 %= (ones - 1);
    }
  }

  for (int i = 0; n > i; i++) {
    if (str[i] == '0') {
      int temp = res1;
      MOD = ones + 1;
      res1 += powerFunction(2, n - i - 1);
      res1 %= MOD;
      int ans = 1;
      //            cout << i << ' ' << res1 << endl;
      while (res1) {
        int x = bitcount(res1);
        res1 %= x;
        ans++;
      }
      cout << ans << endl;
      res1 = temp;
    } else {
      int temp = res2;
      MOD = ones - 1;
      if (MOD == 0) {
        cout << 0 << endl;
        continue;
      }
      res2 += MOD;
      res2 -= powerFunction(2, n - i - 1);
      res2 %= MOD;
      int ans = 1;
      while (res2) {
        int x = bitcount(res2);
        res2 %= x;
        ans++;
      }
      cout << ans << endl;
      res2 = temp;
    }
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #define int long long
// #define INT_MAX INT64_MAX
// #define INT_MIN INT64_MIN
#define PI 3.14159265358979323846
#define CLK clock_t clk = clock(); // Start of main
#define OCLK                                                                   \\
  cerr << "Time (in ms): "                                                     \\
       << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC                    \\
       << '\\n'; // End of main
#define MOD 1000000007
/* DEFINE cout */
#define pln cout << "===============================\\n"
#define on cout << "\\n"
#define os cout << " "
#define o2(a, b) cout << a << " " << b
#define o(a) cout << a
#define bitcount __builtin_popcount
/* DEFINE cout */
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0); // fastio
using namespace std;
struct hash_pair { // use pair as key in
                   // unordered_map<pair<int,int>,int,hash_pair>;
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
bool prime[10000001];
int P = MOD - 2;
int factorial[10000001] = {0};
int powerFunction(int x, int y) {
  int res = 1;
  int p = MOD;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void sieveFunction(int maxLimit) {
  memset(prime, true, sizeof(prime));
  prime[0] = prime[1] = false;
  for (int i = 2; maxLimit >= i; i++) {
    if (prime[i]) {
      for (int j = 2 * i; maxLimit >= j; j += i)
        prime[j] = false;
    }
  }
}
void factorialFunction(int maxLimit) {
  factorial[0] = 1;
  for (int i = 1; i <= maxLimit; i++)
    factorial[i] = (factorial[i - 1] * i) % MOD;
  return;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int fun(string &str, int &n, int &one) {
  if (one == 0) {
    return 0;
  }
  int res = 0;
  int pw = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (str[i] == '1') {
      res += pw;
      res %= one;
    }
    pw *= 2;
    pw %= one;
  }
  int ans = 1;
  while (res) {
    int temp = bitcount(res);
    res %= temp;
    ans++;
  }
  //    cout << str << ' ' << ans << endl;
  return ans;
}
signed main() {
  fastio;
  int n;
  cin >> n;
  string str;
  cin >> str;
  int ones = 0;
  for (int i = 0; n > i; i++) {
    if (str[i] == '1') {
      ones++;
    }
  }
  for (int i = 0; n > i; i++) {
    int temp = ones;
    if (str[i] == '1') {
      str[i] = '0';
      ones--;
      cout << fun(str, n, ones) << endl;
      str[i] = '1';
    } else {
      str[i] = '1';
      ones++;
      cout << fun(str, n, ones) << endl;
      str[i] = '0';
    }
    ones = temp;
  }
}