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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
vector<long long> x, y;
map<long long, int> M;
int solve(long long p) {
  if (M[p] != 0)
    return M[p];
  int maxn = -999999;
  int pos1 = lower_bound(x.begin(), x.end(), p) - x.begin();
  if (pos1 != x.size() && x[pos1] == p)
    maxn = 1;

  int pos2 = lower_bound(y.begin(), y.end(), p) - y.begin();
  if (pos2 != y.size() && y[pos1] == p)
    maxn = 2;

  for (int i = 0; i < x.size(); i++) {
    if (x[i] * x[i] > p)
      continue;
    if (x[i] >= 10000000)
      continue;

    if (p % x[i] == 0 && (p / x[i]) % 2 == 0) {
      int r1 = solve(x[i]);
      int r2 = solve(p / x[i]);
      maxn = max(maxn, r1 + r2);
    }
  }
  M[p] = maxn;
  return maxn;
}
int main() {
  for (int i = 1; i <= 18; i++) {
    for (int j = 0; j < (1 << i); j++) {
      string S = "";
      for (int k = 0; k < i; k++) {
        if ((j / (1 << k)) % 2 == 0)
          S += "2";
        if ((j / (1 << k)) % 2 == 1)
          S += "8";
      }
      x.push_back(stoll(S));
    }
  }
  for (int i = 254; i < 4094; i++) {
    for (int j = i + 1; j < 4094; j++) {
      y.push_back(x[i] * x[j]);
    }
  }
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  long long n;
  cin >> n;
  int c = solve(n);
  if (c < 0)
    c = -1;
  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
vector<pair<long long, int>> x;
map<long long, int> M;
long long dp[100005];
int cnt;
inline int solve(long long p, long long q) {
  if (p < 90000 && dp[p] < 0)
    return -999999;

  if (M[p] != 0)
    return M[p];
  int maxn = -999999;
  int pos1 = lower_bound(x.begin(), x.end(), make_pair(p, 0)) - x.begin();
  if (pos1 != x.size() && x[pos1].first == p)
    maxn = 1;

  if (p % 4 != 0)
    return maxn;

  for (int i = 0; i < x.size(); i++) {
    if (x[i].first * x[i].first > p)
      continue;
    long long c1 = x[i].first, c2 = p / x[i].first;

    int r = q - x[i].second;
    cnt++;
    if (r <= 0)
      continue;
    if (p % x[i].first == 0 && c2 % 2 == 0) {
      int r1 = solve(c1, r);
      int r2 = solve(c2, r);
      maxn = max(maxn, r1 + r2);
    }
  }
  M[p] = maxn;
  return maxn;
}
int main() {
  for (int i = 1; i <= 18; i++) {
    for (int j = 0; j < (1 << i); j++) {
      string S = "";
      for (int k = 0; k < i; k++) {
        if ((j / (1 << k)) % 2 == 0)
          S += "2";
        if ((j / (1 << k)) % 2 == 1)
          S += "8";
      }
      long long r = stoll(S), t = r, u = 0;
      while (t % 2 == 0) {
        t /= 2;
        u++;
      }
      x.push_back(make_pair(stoll(S), u));
    }
  }
  for (int i = 1; i <= 100000; i++)
    dp[i] = -99999999;
  sort(x.begin(), x.end());
  dp[1] = 0;
  for (int i = 1; i <= 100000; i++) {
    for (int j = 0; j < x.size(); j++) {
      if (x[j].first >= 100000)
        break;
      if (i % x[j].first == 0) {
        dp[i] = max(dp[i], dp[i / x[j].first] + 1);
      }
    }
  }
  long long n;
  cin >> n;
  int v = 0, w = n;
  while (w % 2 == 0) {
    w /= 2;
    v++;
  }
  int c = solve(n, v);
  if (c < 0)
    c = -1;
  cout << c << endl;
  // cout << cnt << endl;
  return 0;
}