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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

bool is_good(string n, set<char> s = {'2', '8'}) {
  EACH(d, n) {
    if (s.count(d) == 0) {
      return false;
    }
  }
  return true;
}
ll power(ll x, ll n) {
  ll res = 1;
  REP(i, n) res *= x;
  return res;
}
vector<ll> gns;
ll solve(ll n, ll idx) {
  if (idx == gns.size())
    return -1;
  ll res = -1;
  if (is_good(to_string(n)))
    res = max(res, 1LL);
  if (n % gns[idx] == 0) {
    ll r = solve(n / gns[idx], idx);
    if (r >= 0) {
      res = max(res, r + 1);
    }
  }
  res = max(res, solve(n, idx + 1));
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string nstr;
  cin >> nstr;
  ll n = stoll(nstr);
  vector<ll> gn = {2, 8};
  FOR(w, 1, 17) {
    REP(i, (1 << w)) {
      ll val = 0;
      REP(j, w) { val = val * 10 + gn[(i >> (w - j - 1)) & 1]; }
      if (val == 222888288 || is_good(to_string(val), {'8'}))
        continue;
      if (n % val == 0)
        gns.pb(val);
    }
  }
  cout << solve(n, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; i--)
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

bool is_good(string n, set<char> s = {'2', '8'}) {
  EACH(d, n) {
    if (s.count(d) == 0) {
      return false;
    }
  }
  return true;
}
ll power(ll x, ll n) {
  ll res = 1;
  REP(i, n) res *= x;
  return res;
}
vector<ll> gns;
ll solve(ll n) {
  if (n == 1)
    return -1;
  ll ans = 0;
  string nstr = to_string(n);
  RREP(i, gns.size()) {
    while (n % gns[i] == 0) {
      n /= gns[i];
      ++ans;
    }
  }
  if (n > 1)
    return -1;
  // if (n > 1) {
  //     ll cnt = 0;
  //     while (n % 2 == 0) {
  //         n /= 2;
  //         ++ans;
  //         ++cnt;
  //     }
  //     if ( cnt >= 1 && is_good(to_string(n*2)) ) {
  //     }
  //     else if ( cnt >= 2 && is_good(to_string(n*4)) ) {
  //         --ans;
  //     }
  //     else {
  //         ans = -1;
  //     }
  // }
  return ans;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  string nstr;
  cin >> nstr;
  ll n = stoll(nstr);
  vector<ll> gn = {2, 8};
  FOR(w, 1, 17) {
    REP(i, (1 << w)) {
      ll val = 0;
      REP(j, w) { val = val * 10 + gn[(i >> (w - j - 1)) & 1]; }
      gns.pb(val);
    }
  }
  vector<ll> ngns;
  set<ll> s;
  REP(i, gns.size()) {
    ll rest = gns[i];
    ll to = lower_bound(ALL(ngns), (ll)sqrt((double)rest) + 1) - ngns.begin();
    // RREP(j, ngns.size()) {
    bool f = false;
    RREP(j, ngns.size()) {
      // RREP(j, min((ll)ngns.size(), to)) {
      while (rest % ngns[j] == 0) {
        // if (gns[i] == 222888288) cout << ngns[j] << " " << rest << endl;
        rest /= ngns[j];
        // if (s.count(rest) > 0) {
        //     f = true;
        // }
      }
    }
    if (rest > 1 && !f) {
      s.insert(gns[i]);
      ngns.pb(gns[i]);
    }
  }
  gns = ngns;
  // cout << gns << endl;
  // cout << gns << endl;
  // cout << gns.size() << endl;
  // cout << (ll)28 * 282 * 28228 * 282282 * 28228228 << endl;
  // return 0;
  // set<ll> s;
  // EACH(a, gns) EACH(b, gns) {
  //     s.insert(a*b);
  // }
  // EACH(a, gns) EACH(b, gns) EACH(c, gns) {
  //     if ( s.count(a*b*c) > 0 ) {
  //         cout << a << " " << b << " " << c << " " << a*b*c << endl;
  //     }
  // }
  // return 0;
  // return 0;
  // const ll M = 10000000LL;
  // vector<ll> dp(M, 0);
  // REP(i, gns.size()) {
  //     if (gns[i] >= M) continue;
  //     dp[gns[i]] = max(dp[gns[i]], 1LL);
  //     FOR(j, 1, M) {
  //         if (j * gns[i] >= M) break;
  //         if ( dp[j] > 0 ) {
  //             dp[j*gns[i]] = max(dp[j*gns[i]], dp[j]+1);
  //         }
  //     }
  // }
  // ll ans = solve(n);
  // if (ans < 0) ans = 0;
  // if (n < M) ans = max(ans, dp[n]);
  // else {
  //     EACH(a, gns) {
  //         EACH(b, gns) {
  //             if ( n % (a*b) == 0 && n / (a*b) < M && dp[n/(a*b)] > 0 ) {
  //                 ans = max(ans, dp[n/(a*b)]+2);
  //             }
  //             if ( n % a == 0 && n / a < M && dp[n/a] > 0 ) {
  //                 ans = max(ans, dp[n/a]+1);
  //             }
  //         }
  //     }
  // }
  // // cout << ans << endl;
  // if (ans == 0) {
  //     if ( is_good(nstr) ) {
  //         ans = 1;
  //     }
  //     else {
  //         ans = -1;
  //     }
  // }
  // cout << ans << endl;
  // cout << solve((ll)2828282828282828) << endl;
  cout << solve(n) << endl;

  // REP(i, gns.size()) {
  //     if (gns[i] < M && dp[gns[i]] > 1) {
  //         cout << gns[i] << " " << dp[gns[i]] << endl;
  //     }
  // }
  // cout << solve(8) << endl;
  // cout << solve(222888288) << endl;
  // cout << solve((ll)22222*888888888*22222) << endl;
  // cout << solve(n) << endl;
}