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

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

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 1e5 + 5;

int M;
int powerr(int a, int b) {
  if (b == 0)
    return 1;
  int sm = powerr(a, b / 2);
  int ans = (sm * sm) % M;
  if (b % 2 == 0) {
    return ans;
  }
  return (ans * a) % M;
}

int32_t main() {
  GO;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int count = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      count++;

  int ans = 0;
  int power = 1, sum1 = 0;
  if (count > 1) {
    for (int i = n - 1; i >= 0; i--) {
      sum1 += power * (s[i] - '0');
      power *= 2;
      sum1 %= (count - 1);
      power %= (count - 1);
    }
  }
  power = 1;
  int sum2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    sum2 += power * (s[i] - '0');
    power *= 2;
    sum2 %= (count + 1);
    power %= (count + 1);
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      s[i] = '0';
    else
      s[i] = '1';
    if (s[i] == '1')
      count++;
    else
      count--;
    if (count == 0) {
      cout << 0 << endl;
      if (s[i] == '1')
        s[i] = '0';
      else
        s[i] = '1';

      if (s[i] == '1')
        count++;
      else
        count--;
      continue;
    }
    int sum;
    if (s[i] == '1') {
      M = count;
      sum = sum2;
      sum = sum + powerr(2, n - i - 1);
      sum %= count;
    } else {
      sum = sum1;
      M = count;
      sum = sum - powerr(2, n - i - 1);
      sum = (sum + count * 10) % count;
    }
    int ans = 1;
    while (sum > 0) {
      sum = sum % __builtin_popcount(sum);
      ans++;
    }
    cout << ans << endl;
    if (s[i] == '1')
      s[i] = '0';
    else
      s[i] = '1';

    if (s[i] == '1')
      count++;
    else
      count--;
  }
  return 0;
}

//!

Error: TLE

Fixed submission:

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

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 1e5 + 5;

int32_t main() {
  GO;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int count = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      count++;

  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      s[i] = '0';
    else
      s[i] = '1';
    if (s[i] == '1')
      count++;
    else
      count--;
    if (count == 0) {
      cout << 0 << endl;
      if (s[i] == '1')
        s[i] = '0';
      else
        s[i] = '1';

      if (s[i] == '1')
        count++;
      else
        count--;
      continue;
    }
    int power = 1, sum = 0;
    for (int i = n - 1; i >= 0; i--) {
      sum += power * (s[i] - '0');
      power *= 2;
      sum %= count;
      power %= count;
    }
    int ans = 1;
    while (sum > 0) {
      sum = sum % __builtin_popcount(sum);
      ans++;
    }
    cout << ans << endl;
    if (s[i] == '1')
      s[i] = '0';
    else
      s[i] = '1';

    if (s[i] == '1')
      count++;
    else
      count--;
  }
  return 0;
}

//!