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

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

const int MAX_N = 2e5 + 10;
int N;
string X;
ll a, b;
int memo[MAX_N];
ll c[MAX_N], d[MAX_N];

int go(int x) {
  if (x == 0)
    return 0;
  if (memo[x] != -1)
    return memo[x];
  return memo[x] = (1 + go(x % __builtin_popcount(x)));
}

ll powmod(ll x, int y, int MOD) {
  ll res = 1;
  while (y) {
    if (y & 1)
      (res *= x) %= MOD;
    (x *= x) %= MOD;
    y >>= 1;
  }
  return res;
}

void solve() {
  int tot = 0;
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < N; ++i)
    if (X[i] == '1')
      tot++;
  if (!tot) {
    for (int i = 0; i < N; ++i)
      cout << 1 << '\\n';
  } else if (tot == 1) {
    int b = -1;
    for (int i = 0; i < N; ++i) {
      if (X[i] == '1') {
        b = i;
        break;
      }
    }
    for (int i = 0; i < N; ++i) {
      if (i == b)
        cout << 0 << '\\n';
      else {
        if (i == N - 1 || b == N - 1)
          cout << 2 << '\\n';
        else
          cout << 1 << '\\n';
      }
    }
  } else {
    a = 0, b = 0;
    for (int i = 0; i < N; ++i) {
      c[i] = powmod(2LL, N - 1 - i, tot + 1);
      d[i] = powmod(2LL, N - 1 - i, tot - 1);
      if (X[i] == '1') {
        (a += c[i]) %= (tot + 1);
        (b += d[i]) %= (tot - 1);
      }
    }
    for (int i = 0; i < N; ++i) {
      if (X[i] == '0')
        cout << 1 + go((a + c[i]) % (tot + 1)) << '\\n';
      else
        cout << 1 + go((b - d[i] + tot - 1) % (tot - 1)) << '\\n';
    }
  }
}

int main() {
  cin >> N;
  cin >> X;
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

int N;
string X;
ll a, b;
map<int, int> memo;

int go(int x) {
  if (x == 0)
    return 0;
  if (memo.count(x))
    return memo[x];
  return memo[x] = (1 + go(x % __builtin_popcount(x)));
}

ll powmod(ll x, int y, int MOD) {
  ll res = 1;
  while (y) {
    if (y & 1)
      (res *= x) %= MOD;
    (x *= x) %= MOD;
    y >>= 1;
  }
  return res;
}

void solve() {
  int tot = 0;
  for (int i = 0; i < N; ++i)
    if (X[i] == '1')
      tot++;
  if (!tot) {
    for (int i = 0; i < N; ++i)
      cout << 1 << '\\n';
  } else if (tot == 1) {
    int b = -1;
    for (int i = 0; i < N; ++i) {
      if (X[i] == '1') {
        b = i;
        break;
      }
    }
    for (int i = 0; i < N; ++i) {
      if (i == b)
        cout << 0 << '\\n';
      else {
        cout << 1 + go((powmod(2LL, N - i - b, 2) + powmod(2LL, N - i - 1, 2)) %
                       2)
             << '\\n';
      }
    }
  } else {
    a = 0, b = 0;
    for (int i = 0; i < N; ++i)
      if (X[i] == '1') {
        (a += powmod(2LL, N - i - 1, tot + 1)) %= (tot + 1);
        (b += powmod(2LL, N - i - 1, tot - 1)) %= (tot - 1);
      }
    for (int i = 0; i < N; ++i) {
      if (X[i] == '0')
        cout << 1 + go((a + powmod(2LL, N - i - 1, tot + 1)) % (tot + 1))
             << '\\n';
      else
        cout << 1 + go((b - powmod(2LL, N - i - 1, tot - 1) + tot - 1) %
                       (tot - 1))
             << '\\n';
    }
  }
}

int main() {
  cin >> N;
  cin >> X;
  solve();
  return 0;
}