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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int popcount(string s) {

  int M = s.size();
  int ret = 0;
  for (int i = 0; i < M; i++) {
    if (s[i] == '1') {
      ret++;
    }
  }
  return ret;
}

int f(string s, int pop_cnt) {

  // cout << "s= " << s <<" pop_cnt= "<<pop_cnt<< endl;
  int m = pop_cnt;

  int tmp = 0;

  int M = s.size();

  for (int i = 0; i < M; i++) {
    tmp *= 2;
    if (s[i] == '1') {
      tmp++;
    }
    tmp %= m;
  }

  return tmp;

  /*string t = "";

  int next_pop = 0;

  while (tmp> 0) {
          if ((tmp % 2) == 1) { t += "1"; next_pop++; }
          else { t += "0"; }
          tmp /= 2;
  }
  reverse(t.begin(), t.end());

  //cout << "t= " << t << endl;

  return make_pair(t,next_pop);*/
}

int N;
string X;

int c[200005];

int g(int t) {
  if (c[t] >= 0) {
    return c[t];
  }

  int tmp = t;
  int pcnt = 0;
  while (tmp > 0) {
    if ((tmp % 2) == 1) {
      pcnt++;
    }
    tmp /= 2;
  }

  c[t] = g(t % pcnt) + 1;

  return c[t];
}

int ans[200005];

int main(void) {

  cin >> N >> X;

  for (int i = 1; i <= N; i++) {
    c[i] = -1;
  }

  string s = X;
  int cnt = 0;

  pair<string, int> p;

  int m = popcount(s);

  int tmp = 0;
  for (int i = 0; i < N; i++) {
    tmp *= 2;
    if (X[i] == '1') {
      tmp++;
    }
    tmp %= (m + 1);
  }

  int num = 1;
  for (int i = N - 1; i >= 0; i--) {
    num %= (m + 1);

    if (s[i] == '0') {
      // cout << "num= " << num << endl;
      // cout <<"i= " <<i<<" "<< (tmp + num) % (m + 1) << endl;
      ans[i] = g((tmp + num) % (m + 1)) + 1;
    }
    num *= 2;
  }
  //=============================
  num = 1;

  if (m - 1 > 0) {
    tmp = 0;
    for (int i = 0; i < N; i++) {
      tmp *= 2;
      if (X[i] == '1') {
        tmp++;
      }
      tmp %= (m - 1);
    }

    for (int i = N - 1; i >= 0; i--) {
      num %= (m - 1);

      if (s[i] == '1') {
        //
        ans[i] = g((tmp - (num) + (m - 1)) % (m - 1)) + 1;
      }
      num *= 2;
    }
  }

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

  /*for (int i = 0; i < N; i++) {
          int pop_cnt;
          s = X;
          if (s[i] == '0') { s[i] = '1'; pop_cnt = m + 1; }
          else { s[i] = '0'; pop_cnt = m - 1; }


          if (pop_cnt == 0) { cout << 0 << endl; continue; }

          int cnt = 0;
          int ret= f(s, pop_cnt); cnt++;

          //cout << "ret=" << ret << endl;

          cout << g(ret) + 1 << endl;

  }*/

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int popcount(string s) {

  int M = s.size();
  int ret = 0;
  for (int i = 0; i < M; i++) {
    if (s[i] == '1') {
      ret++;
    }
  }
  return ret;
}

int f(string s, int pop_cnt) {

  // cout << "s= " << s <<" pop_cnt= "<<pop_cnt<< endl;
  int m = pop_cnt;

  int tmp = 0;

  int M = s.size();

  for (int i = 0; i < M; i++) {
    tmp *= 2;
    if (s[i] == '1') {
      tmp++;
    }
    tmp %= m;
  }

  return tmp;

  /*string t = "";

  int next_pop = 0;

  while (tmp> 0) {
          if ((tmp % 2) == 1) { t += "1"; next_pop++; }
          else { t += "0"; }
          tmp /= 2;
  }
  reverse(t.begin(), t.end());

  //cout << "t= " << t << endl;

  return make_pair(t,next_pop);*/
}

int N;
string X;

int c[200005];

int g(int t) {
  if (c[t] >= 0) {
    return c[t];
  }

  int tmp = t;
  int pcnt = 0;
  while (tmp > 0) {
    if ((tmp % 2) == 1) {
      pcnt++;
    }
    tmp /= 2;
  }

  c[t] = g(t % pcnt) + 1;

  return c[t];
}

int main(void) {

  cin >> N >> X;

  for (int i = 1; i <= N; i++) {
    c[i] = -1;
  }

  string s = X;
  int cnt = 0;

  pair<string, int> p;

  int m = popcount(s);

  for (int i = 0; i < N; i++) {
    int pop_cnt;
    s = X;
    if (s[i] == '0') {
      s[i] = '1';
      pop_cnt = m + 1;
    } else {
      s[i] = '0';
      pop_cnt = m - 1;
    }

    if (pop_cnt == 0) {
      cout << 0 << endl;
      continue;
    }

    int cnt = 0;
    int ret = f(s, pop_cnt);
    cnt++;

    // cout << "ret=" << ret << endl;

    cout << g(ret) + 1 << endl;
  }

  return 0;
}