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

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

#define int long long

typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N;

vint vec;
void dfs(int n, int d) {
  if (d > 1000000000ll)
    return;
  if (n) {
    vec.pb(d);
  }
  dfs(n + 1, d * 10 + 2);
  dfs(n + 1, d * 10 + 8);
}

bool C(int d) {
  if (d == 1)
    return true;
  stringstream ss;
  ss << d;
  string s = ss.str();
  rep(i, s.size()) if (s[i] != '2' && s[i] != '8') return false;
  return true;
}

signed main() {
  cin >> N;
  if (N == 1) {
    puts("-1");
    return 0;
  }
  dfs(0, 0);
  sort(all(vec));
  reverse(all(vec));

  map<int, int> prev;
  prev[N] = 0;
  for (int i = 0; i < vec.size(); i++) {
    map<int, int> next;
    each(it, prev) {
      chmax(next[it->fi], it->se);
      int tmp = it->fi, hoge = 0;
      while (tmp % vec[i] == 0) {
        tmp /= vec[i];
        hoge++;
        chmax(next[tmp], it->se + hoge);
      }
    }
    prev = next;
  }

  int ans = -1;
  each(it, prev) {
    if (C(it->fi))
      chmax(ans, it->se + (it->fi != 1));
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define int long long

typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
template <class T, class U> void chmin(T &t, U f) {
  if (t > f)
    t = f;
}
template <class T, class U> void chmax(T &t, U f) {
  if (t < f)
    t = f;
}

int N;

vint vec;
void dfs(int n, int d) {
  if (d > 1000000000000000000ll)
    return;
  if (n) {
    vec.pb(d);
  }
  dfs(n + 1, d * 10 + 2);
  dfs(n + 1, d * 10 + 8);
}

bool C(int d) {
  if (d == 1)
    return true;
  stringstream ss;
  ss << d;
  string s = ss.str();
  rep(i, s.size()) if (s[i] != '2' && s[i] != '8') return false;
  return true;
}

signed main() {
  cin >> N;
  if (N == 1) {
    puts("-1");
    return 0;
  }
  dfs(0, 0);

  map<int, int> prev;
  prev[N] = 0;
  for (int i = 0; i < vec.size(); i++) {
    map<int, int> next;
    each(it, prev) {
      chmax(next[it->fi], it->se);
      int tmp = it->fi, hoge = 0;
      while (tmp % vec[i] == 0) {
        tmp /= vec[i];
        hoge++;
        chmax(next[tmp], it->se + hoge);
      }
    }
    prev = next;
  }

  if (prev.find(1) == prev.end())
    cout << -1 << endl;
  else
    cout << prev[1] << endl;
  return 0;
}