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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 2e5 + 5;
ll f[N];
char s[N];
ll fastpow(ll x, ll y, ll md) {
  ll ret = 1;
  for (; y; y >>= 1, x = x * x % md)
    if (y & 1)
      ret = ret * x % md;
  return ret;
}
int main() {
  ll n;
  scanf("%lld", &n);
  scanf("%s", s + 1);
  ll one = 0;
  for (ll i = 1; i <= n; i++) {
    if (s[i] == '1')
      one++;
  }
  ll num1 = 0, num2 = 0;
  for (ll i = 1, j = n - 1; i <= n; i++, j--) {
    num1 = (num1 + (s[i] - '0') * fastpow(2, j, one + 1)) % (one + 1);
  }
  if (one > 1)
    for (ll i = 1, j = n - 1; i <= n; i++, j--) {
      num2 = (num2 + (s[i] - '0') * fastpow(2, j, one - 1)) % (one - 1);
    }
  for (ll i = 1; i <= n; i++) {
    if (s[i] == '1') {
      s[i] = '0';
      ll none = one - 1;
      ll cnt = 0;
      if (none == 0) {
        puts("0");
        continue;
      }
      ll now = 0;
      do {
        cnt++;
        if (cnt == 1) {
          now = (num2 - fastpow(2, n - i, none) + none) % none;
        } else {
          ll nnow = now;
          none = 0;
          while (nnow) {
            if (nnow & 1)
              none++;
            nnow >>= 1;
          }
          now = now % none;
        }
      } while (now);
      printf("%lld\\n", cnt);
      s[i] = '1';
    } else {
      s[i] = '1';
      ll none = one + 1;
      ll cnt = 0;
      if (none == 0) {
        puts("0");
        continue;
      }
      ll now = 0;
      do {
        cnt++;
        if (cnt == 1) {
          now = (num1 + fastpow(2, n - i, none) + none) % none;
        } else {
          ll nnow = now;
          none = 0;
          while (nnow) {
            if (nnow & 1)
              none++;
            nnow >>= 1;
          }
          now = now % none;
        }
      } while (now);
      printf("%lld\\n", cnt);
      s[i] = '0';
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int f[N];
char s[N];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int one = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      one++;
  }
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      s[i] = '0';
      int none = one - 1;
      int cnt = 0;
      if (none == 0) {
        puts("0");
        continue;
      }
      int now = 0;
      do {
        cnt++;
        if (cnt == 1) {
          for (int j = 1; j <= n; j++) {
            now = ((s[j] - '0') + now * 2) % none;
          }
        } else {
          int nnow = now;
          none = 0;
          while (nnow) {
            if (nnow & 1)
              none++;
            nnow >>= 1;
          }
          now = now % none;
        }
      } while (now);
      printf("%d\\n", cnt);
      s[i] = '1';
    } else {
      s[i] = '1';
      int none = one + 1;
      int cnt = 0;
      if (none == 0) {
        puts("0");
        continue;
      }
      int now = 0;
      do {
        cnt++;
        if (cnt == 1) {
          for (int j = 1; j <= n; j++) {
            now = ((s[j] - '0') + now * 2) % none;
          }
        } else {
          int nnow = now;
          none = 0;
          while (nnow) {
            if (nnow & 1)
              none++;
            nnow >>= 1;
          }
          now = now % none;
        }
      } while (now);
      printf("%d\\n", cnt);
      s[i] = '0';
    }
  }
  return 0;
}