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

#include <bits/stdc++.h>

using namespace std;

vector<long> ff(100000, -1);

int calc(long n) {
  if (n == 0) {
    return 0;
  } else {
    if (n < 100000 && ff[n] != -1) {
      return ff[n];
    }
    int c = 0;
    for (int bit = 1; bit <= n; bit *= 2) {
      if (n & bit)
        c++;
    }
    if (n % c == 0)
      return 1;
    else
      return ff[n] = 1 + calc(n % c);
  }
}

int main() {
  int n;
  string x;
  cin >> n;
  cin >> x;

  // for(int i=0;i<n;i++){
  //     char c = '0' + i%2;
  //     x += c;
  // }

  long pop = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      pop++;
  }
  long modm1[n];
  long mod[n];
  long modp1[n];

  long tmp = 1;
  long tmpp = 1;
  long tmpm = 1;

  for (int i = 0; i < n; i++) {
    modm1[i] = tmpm;
    modp1[i] = tmpp;

    if (pop > 1)
      tmpm = (tmpm * 2) % (pop - 1);
    else if (pop == 1)
      tmpm = 0;
    tmpp = (tmpp * 2) % (pop + 1);
  }

  long m = 0;
  long p = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      m += modm1[n - 1 - i];
      p += modp1[n - 1 - i];

      if (pop > 1)
        m %= (pop - 1);
      else if (pop == 1)
        m = 0;
      p %= (pop + 1);
    }
  }

  vector<long> ans(n);

  for (int i = 0; i < n; i++) {
    long popp = pop;
    long xx = 0;
    if (x[i] == '0') {
      popp++;
      xx = p + modp1[n - 1 - i];
    } else {
      popp--;
      xx = m - modm1[n - 1 - i];
      while (popp != 0 && xx < 0) {
        xx += 1000000 * popp;
      }
    }

    if (popp == 0) {
      ans[i] = 0;
      continue;
    } else if (popp == 1) {
      ans[i] = 1;
      continue;
    } else {
      ans[i] = 1 + calc(xx % popp);
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

vector<long> ff(1000000, -1);

int calc(long n) {
  if (n == 0) {
    return 0;
  } else {
    if (n < 1000000 && ff[n] != -1) {
      return ff[n];
    }
    int c = 0;
    for (int bit = 1; bit <= n; bit *= 2) {
      if (n & bit)
        c++;
    }
    if (n % c == 0)
      return 1;
    else
      return ff[n] = 1 + calc(n % c);
  }
}

int main() {
  int n;
  string x;
  cin >> n;
  cin >> x;

  // for(int i=0;i<n;i++){
  //     char c = '0' + i%2;
  //     x += c;
  // }

  long pop = 0;
  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      pop++;
  }
  long modm1[n];
  long mod[n];
  long modp1[n];

  long tmp = 1;
  long tmpp = 1;
  long tmpm = 1;

  for (int i = 0; i < n; i++) {
    modm1[i] = tmpm;
    modp1[i] = tmpp;

    if (pop > 1)
      tmpm = (tmpm * 2) % (pop - 1);
    else if (pop == 1)
      tmpm = 0;
    tmpp = (tmpp * 2) % (pop + 1);
  }

  long m = 0;
  long p = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      m += modm1[n - 1 - i];
      p += modp1[n - 1 - i];

      if (pop > 1)
        m %= (pop - 1);
      else if (pop == 1)
        m = 0;
      p %= (pop + 1);
    }
  }

  vector<long> ans(n);

  for (int i = 0; i < n; i++) {
    long popp = pop;
    long xx = 0;
    if (x[i] == '0') {
      popp++;
      xx = p + modp1[n - 1 - i];
    } else {
      popp--;
      xx = m - modm1[n - 1 - i];
      while (xx < 0) {
        xx += 1000000 * popp;
      }
    }

    if (popp == 0) {
      ans[i] = 0;
      continue;
    } else if (popp == 1) {
      ans[i] = 1;
      continue;
    } else {
      ans[i] = 1 + calc(xx % popp);
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}