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

#include <bits/stdc++.h>

long N;
std::vector<long> as;
std::vector<long> bs;

bool step1(long n) {
  std::string str = std::to_string(n);
  bool res = true;
  for (int i = 0; i < (int)str.size(); ++i) {
    if (str[i] != '2' and str[i] != '8') {
      res = false;
      break;
    }
  }
  return res;
}

bool all8(long n) {
  while (n != 0) {
    if (n % 10 != 8) {
      return false;
    }
    n /= 10;
  }
  return true;
}

void dfs1(int i, long n) {
  if (i == 0) {
    if (all8(n))
      return;
    as.push_back(n);
  } else {
    dfs1(i - 1, n * 10 + 2);
    dfs1(i - 1, n * 10 + 8);
  }
}

int dfs2(int i, long n) {
  if (i >= (int)bs.size()) {
    if (n != 1 and not step1(n)) {
      return -1;
    }
    if (step1(n)) {
      return 1;
    }
    return 0;
  }
  if (n == 1) {
    return 0;
  }
  int res = -1;
  int t2 = dfs2(i + 1, n);
  if (t2 > 0) {
    res = std::max(res, t2);
  }
  for (int k = 1;; ++k) {
    if (n == 0)
      break;
    if (n % bs[i] != 0)
      break;
    n /= bs[i];
    int t = dfs2(i + 1, n);
    if (t != -1) {
      res = std::max(res, dfs2(i + 1, n) + k);
    }
  }
  return res;
}

int main(int argc, char *argv[]) {

  std::cin >> N;
  if (N == 1) {
    std::cout << -1 << std::endl;
    return 0;
  }

  for (int i = 1; i <= 11; ++i) {
    dfs1(i, 0);
  }

  for (int i = 0; i < (int)as.size(); ++i) {
    if (N % as[i] == 0) {
      bs.push_back(as[i]);
    }
  }

  int t = dfs2(0, N);
  if (t != -1) {
    std::cout << t << std::endl;
    return 0;
  }

  if (step1(N)) {
    std::cout << 1 << std::endl;
    return 0;
  }
  std::cout << -1 << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

long N;
std::vector<long> as;
std::vector<long> bs;

bool step1(long n) {
  std::string str = std::to_string(n);
  bool res = true;
  for (int i = 0; i < (int)str.size(); ++i) {
    if (str[i] != '2' and str[i] != '8') {
      res = false;
      break;
    }
  }
  if (str.size() <= 9)
    ;
  return res;
}

bool all8(long n) {
  while (n != 0) {
    if (n % 10 != 8) {
      return false;
    }
    n /= 10;
  }
  return true;
}

void dfs1(int i, long n) {
  if (i == 0) {
    as.push_back(n);
  } else {
    dfs1(i - 1, n * 10 + 2);
    dfs1(i - 1, n * 10 + 8);
  }
}

int dfs2(int i, long n) {
  if (i >= (int)bs.size()) {
    if (n != 1 and not step1(n)) {
      return -1;
    }
    if (step1(n)) {
      return 1;
    }
    return 0;
  }
  if (n == 1) {
    return 0;
  }
  int res = -1;
  int t2 = dfs2(i + 1, n);
  if (t2 > 0) {
    res = std::max(res, dfs2(i + 1, n));
  }
  for (int k = 1;; ++k) {
    if (n == 0)
      break;
    if (n % bs[i] != 0)
      break;
    n /= bs[i];
    int t = dfs2(i + 1, n);
    if (t != -1) {
      res = std::max(res, dfs2(i + 1, n) + k);
    }
  }
  return res;
}

int main(int argc, char *argv[]) {

  std::cin >> N;
  if (N == 1) {
    std::cout << -1 << std::endl;
    return 0;
  }

  for (int i = 1; i <= 11; ++i) {
    dfs1(i, 0);
  }

  for (int i = 0; i < (int)as.size(); ++i) {
    if (N % as[i] == 0) {
      bs.push_back(as[i]);
    }
  }

  int t = dfs2(0, N);
  if (t != -1) {
    std::cout << t << std::endl;
    return 0;
  }

  if (step1(N)) {
    std::cout << 1 << std::endl;
    return 0;
  }
  std::cout << -1 << std::endl;

  return 0;
}