alexjercan/bugnet huggingface dataset:
Problem p01869 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

vector<ll> V;

int ans = -1;

void recursive(ll N, int index, int count) {

  if (N == 1) {
    ans = max(ans, count);
    return;
  }

  if (N < V[index] || index == (int)V.size())
    return;

  recursive(N, index + 1, count);

  while (N % V[index] == 0) {
    N /= V[index];
    recursive(N, index + 1, ++count);
  }
}

int main() {

  ll POW[18];

  for (int i = 0; i <= 17; i++) {
    POW[i] = pow(10, i);
  }

  ll tmp;
  int limit;

  ll N;
  scanf("%lld", &N);
  if (N == 1) {
    printf("-1\\n");
    return 0;
  }

  for (int length = 1; length <= 18; length++) {
    limit = pow(2, length);
    for (int state = 0; state < limit; state++) {
      tmp = 0;
      for (int loop = 0; loop < length; loop++) {
        if (state & (1 << loop)) {
          tmp += 8 * POW[loop];
        } else {
          tmp += 2 * POW[loop];
        }
      }
      if (N % tmp == 0) { //?????????????????°????????§??????!!!
        V.push_back(tmp);
      }
    }
  }

  sort(V.begin(), V.end());

  if (V.size() == 0) {
    printf("-1\\n");
    return 0;
  }

  recursive(N, 0, 0);

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

vector<ll> V;

int ans = -1;

void recursive(ll N, int index, int count) {

  if (N == 1) {
    ans = max(ans, count);
    return;
  }

  if (N < V[index] || index == (int)V.size())
    return;

  recursive(N, index + 1, count);

  while (N % V[index] == 0) {
    N /= V[index];
    recursive(N, index + 1, ++count);
  }
}

int main() {

  ll POW[18];

  for (int i = 0; i <= 17; i++) {
    POW[i] = pow(10, i);
  }

  ll tmp;
  int limit;

  ll N;
  scanf("%lld", &N);
  if (N == 1) {
    printf("-1\\n");
    return 0;
  }

  for (int length = 1; length <= 18; length++) {
    limit = pow(2, length);
    for (int state = 0; state < limit; state++) {
      tmp = 0;
      for (int loop = 0; loop < length; loop++) {
        if (state & (1 << loop)) {
          tmp += 8 * POW[loop];
        } else {
          tmp += 2 * POW[loop];
        }
      }
      if (N % tmp == 0) { //?????????????????°????????§??????!!!
        V.push_back(tmp);
      }
    }
  }

  sort(V.begin(), V.end());

  recursive(N, 0, 0);

  printf("%d\\n", ans);

  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> nex[20];
vector<ll> nv;

map<ll, int> memo;

const int INF = (1 << 29);
ll solve(ll x) {
  bool f = false;
  if (memo.count(x) != 0)
    return memo[x];
  int ret = 0;
  int sid = upper_bound(nv.begin(), nv.end(), x) - nv.begin();
  for (int i = min((int)nv.size() - 1, sid); i > -1; i--) {
    if (x == nv[i]) {
      f = true;
      continue;
    }
    if (x % nv[i] == 0) {
      //  cout << x/nv[i] << " - "<<  nv[i] << endl;
      int l = solve(x / nv[i]);
      int r = solve(nv[i]);
      if (l == -1 || r == -1)
        continue;
      ret = max(l + r, ret);
    }
  }
  if (ret == 0) {
    if (!f)
      return memo[x] = -1;
    return memo[x] = 1;
  }
  return memo[x] = ret;
}

int main() {
  nex[0].push_back(2LL);
  nex[0].push_back(8LL);
  for (int k = 0; k < 18; k++) {
    for (int i = 0; i < (int)nex[k].size(); i++) {
      ll p = nex[k][i];
      nex[k + 1].push_back(p * 10LL + 2LL);
      nex[k + 1].push_back(p * 10LL + 8LL);
    }
  }
  for (int k = 0; k < 18; k++) {
    for (int i = 0; i < nex[k].size(); i++) {
      nv.push_back(nex[k][i]);
    }
  }
  sort(nv.begin(), nv.end());
  ll n;
  cin >> n;
  cout << solve(n) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> nex[20];
vector<ll> nv;

unordered_map<ll, int> memo;
set<ll> ok;

const int INF = (1 << 29);
ll solve(ll x) {
  bool f = false;
  if (memo.count(x) != 0)
    return memo[x];
  int ret = INF;
  for (int i = nv.size() - 1; i > -1; i--) {
    if (x == nv[i]) {
      f = true;
      continue;
    }
    if (x % nv[i] == 0) {
      //  cout << x/nv[i] << " - "<<  nv[i] << endl;
      int l = solve(x / nv[i]);
      int r = solve(nv[i]);
      if (l == -1 || r == -1)
        continue;
      ret = min(l + r, ret);
    }
  }
  if (ret == INF) {
    if (!f)
      return memo[x] = -1;
    return memo[x] = 1;
  }
  return memo[x] = ret;
}

bool check(ll x, const vector<ll> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (v[i] == x)
      continue;
    if (x % v[i] == 0)
      return true;
  }
  return false;
}

int main() {
  nex[0].push_back(2);
  nex[0].push_back(8);
  for (int k = 0; k < 18; k++) {
    for (int i = 0; i < (int)nex[k].size(); i++) {
      ll p = nex[k][i];
      nex[k + 1].push_back(p * 10 + 2LL);
      nex[k + 1].push_back(p * 10 + 8LL);
    }
  }
  for (int k = 0; k < 18; k++) {
    for (int i = 0; i < nex[k].size(); i++) {
      nv.push_back(nex[k][i]);
      ok.insert(nex[k][i]);
    }
  }
  sort(nv.begin(), nv.end());
  ll n;
  cin >> n;
  cout << solve(n) << endl;
}
Problem p01869 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

ll n;
vector<ll> num;
unordered_map<ll, int> dp;

inline int rec(ll x) {
  if (dp.count(x))
    return dp[x];
  int &res = dp[x];
  if (x == 1)
    return res = 0;

  res = -inf;
  for (ll m : num) {
    if (x % m == 0)
      res = max(res, 1 + rec(x / m));
    if (m >= x)
      break;
  }
  if (res < 0)
    res = -inf;
  return res;
}

int main() {
  for (int keta = 1; keta <= 18; keta++) {
    rep(i, 1 << keta) {
      ll m = 0;
      rep(j, keta) {
        if (m * 10.0 > 1.1e18) {
          m = -1;
          break;
        }
        m *= 10;
        m += i >> j & 1 ? 8 : 2;
      }
      if (m > 0)
        num.pb(m);
    }
  }
  sort(all(num));

  cin >> n;
  int res = n > 1 ? rec(n) : -inf;
  cout << (res < 0 ? -1 : res) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

ll n;
vector<ll> num;
map<ll, int> dp;

int rec(ll x) {
  if (dp.count(x))
    return dp[x];
  int &res = dp[x];
  if (x == 1)
    return res = 0;

  res = -inf;
  for (ll m : num)
    if (x % m == 0)
      res = max(res, 1 + rec(x / m));
  if (res < 0)
    res = -inf;
  return res;
}

int main() {
  for (int keta = 1; keta <= 18; keta++) {
    rep(i, 1 << keta) {
      ll m = 0;
      rep(j, keta) {
        if (m * 10.0 > 1.1e18) {
          m = -1;
          break;
        }
        m *= 10;
        m += i >> j & 1 ? 2 : 8;
      }
      if (m > 0)
        num.pb(m);
    }
  }

  cin >> n;
  int res = n > 1 ? rec(n) : -inf;
  cout << (res < 0 ? -1 : res) << endl;
  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long int ll;
const ll INF = 1LL << 28;

ll N;
vector<ll> num;
map<ll, ll> memo;
queue<ll> q;
function<ll(ll)> seki;

int main() {
  cin >> N;
  q.push(2LL);
  q.push(8LL);
  while (q.size()) {
    ll n = q.front();
    q.pop();
    num.push_back(n);
    if (n <= 1e18) {
      q.push(n * 10LL + 8LL);
      q.push(n * 10LL + 2LL);
    }
  }

  sort(num.begin(), num.end());

  seki = [&](ll n) -> ll {
    if (memo.find(n) != memo.end())
      return memo[n];

    int i = (int)(lower_bound(num.begin(), num.end(), n) - num.begin());
    memo[n] = -INF;
    if (num[i] == n)
      memo[n] = 1;
    for (; i > -1; i--) {
      if (n % num[i] == 0) {
        memo[n] = max(memo[n], seki(n / num[i]) + seki(num[i]));
      }
    }

    return memo[n];
  };

  int ans = seki(N);
  if (ans > 0)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long int ll;
const ll INF = 1LL << 28;

ll N;
vector<ll> num;
map<ll, ll> memo;
queue<ll> q;
function<ll(ll)> seki;

int main() {
  cin >> N;
  q.push(2LL);
  q.push(8LL);
  while (q.size()) {
    ll n = q.front();
    q.pop();
    if (n <= 1000000000000000000) {
      num.push_back(n);
      q.push(n * 10LL + 8LL);
      q.push(n * 10LL + 2LL);
    }
  }

  sort(num.begin(), num.end());

  seki = [&](ll n) -> ll {
    if (memo.find(n) != memo.end())
      return memo[n];

    int i = (int)(lower_bound(num.begin(), num.end(), n) - num.begin());
    memo[n] = -INF;
    if (num[i] == n)
      memo[n] = 1;
    for (; i > -1; i--) {
      if (n % num[i] == 0) {
        memo[n] = max(memo[n], seki(n / num[i]) + seki(num[i]));
      }
    }

    return memo[n];
  };

  int ans = seki(N);
  if (ans > 0)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long int ll;
const ll INF = 1LL << 28;

ll N;
vector<ll> num;
map<ll, ll> memo;
queue<ll> q;
function<ll(ll)> seki;

int main() {
  cin >> N;
  q.push(2LL);
  q.push(8LL);
  while (q.size()) {
    ll n = q.front();
    q.pop();
    num.push_back(n);
    if (n <= 1e18) {
      q.push(n * 10LL + 8LL);
      q.push(n * 10LL + 2LL);
    }
  }

  sort(num.begin(), num.end());

  seki = [&](ll n) -> ll {
    if (memo.find(n) != memo.end())
      return memo[n];

    int i = (int)(lower_bound(num.begin(), num.end(), n) - num.begin());
    memo[n] = -INF;
    if (num[i] == n)
      memo[n] = 1;
    for (; i > -1; i--) {
      if (n % num[i] == 0) {
        memo[n] = max(memo[n], seki(n / num[i]) + seki(num[i]));
      }
    }

    return memo[n];
  };

  int ans = seki(N);
  if (ans > 0)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef long long int ll;
const ll INF = 1LL << 28;

ll N;
vector<ll> num;
map<ll, ll> memo;
queue<ll> q;
function<ll(ll)> seki;

int main() {
  cin >> N;
  q.push(2LL);
  q.push(8LL);
  while (q.size()) {
    ll n = q.front();
    q.pop();
    if (n <= 1e18) {
      num.push_back(n);
      q.push(n * 10LL + 8LL);
      q.push(n * 10LL + 2LL);
    }
  }

  sort(num.begin(), num.end());

  seki = [&](ll n) -> ll {
    if (memo.find(n) != memo.end())
      return memo[n];

    int i = (int)(lower_bound(num.begin(), num.end(), n) - num.begin());
    if (n == num.size())
      i--;
    memo[n] = -INF;
    if (num[i] == n)
      memo[n] = 1;
    for (; i > -1; i--) {
      if (n % num[i] == 0) {
        memo[n] = max(memo[n], seki(n / num[i]) + seki(num[i]));
      }
    }

    return memo[n];
  };

  int ans = seki(N);
  if (ans > 0)
    cout << ans << endl;
  else
    cout << -1 << endl;

  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> A;

void dfs(int num, int dep) {
  if (dep == 18)
    return;
  if (num)
    A.push_back(num);
  dfs(num * 10 + 2, dep + 1);
  dfs(num * 10 + 8, dep + 1);
}

typedef pair<int, int> P;
signed main() {
  int n;
  cin >> n;
  dfs(0, 0);
  sort(A.begin(), A.end(), greater<int>());
  map<int, int> dp;

  dp[n] = 0, dp[1] = -1;

  for (int a : A) {
    for (P p : dp) {
      int num = p.first;
      int score = p.second;
      if (num % a)
        continue;
      while (num % a == 0)
        num /= a, dp[num] = max(dp[num], ++score);
    }
  }

  int ans = dp[1];
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> A;

void dfs(int num, int dep) {
  if (dep == 18)
    return;
  if (num)
    A.push_back(num);
  dfs(num * 10 + 2, dep + 1);
  dfs(num * 10 + 8, dep + 1);
}

typedef pair<int, int> P;
signed main() {
  int n;
  cin >> n;
  dfs(0, 0);
  sort(A.begin(), A.end());
  map<int, int> dp;

  dp[n] = 0, dp[1] = -1;

  for (int a : A) {
    for (P p : dp) {
      int num = p.first;
      int score = p.second;
      if (num % a)
        continue;
      // cout<<a<<" "<<num<<" "<<score<<endl;
      while (num % a == 0)
        num /= a, dp[num] = max(dp[num], ++score);
    }
  }

  int ans = dp[1];
  cout << ans << endl;

  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int d[2] = {2, 8}, ans = -1;
vector<ll> v;

void func(int x, ll k) {
  if (x)
    v.push_back(k);
  if (x == 18)
    return;
  for (int i = 0; i < 2; i++)
    func(x + 1, k * 10 + d[i]);
}

void func2(int x, ll k, int sum) {
  if (k == 1 && sum)
    ans = max(ans, sum);
  if (v[x] > k)
    return;
  if (!(k % v[x]))
    func2(x, k / v[x], sum + 1);
  if (x + 1 != v.size())
    func2(x + 1, k, sum);
}

int main() {
  ll n;
  cin >> n;
  func(0, 0);
  sort(v.begin(), v.end());
  func2(0, n, 0);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int d[2] = {2, 8}, ans = -1;
vector<ll> v;

void func(int x, ll k) {
  if (x)
    v.push_back(k);
  if (x == 18)
    return;
  for (int i = 0; i < 2; i++)
    func(x + 1, k * 10 + d[i]);
}

void func2(int x, ll k, int sum) {
  if (k == 1 && sum)
    ans = max(ans, sum);
  if (v[x] > k)
    return;
  if (k % !v[x])
    func2(x, k / v[x], sum + 1);
  if (x + 1 != v.size())
    func2(x + 1, k, sum);
}

int main() {
  ll n;
  cin >> n;
  func(0, 0);
  sort(v.begin(), v.end());
  func2(0, n, 0);
  cout << ans << endl;
  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Runtime Error

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

  if (p % x[pos] == 0) {
    maxn = solve(p / x[pos], pos) + 1;
  }

  return max(maxn, solve(p, pos + 1));
}
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));
    }
  }
  sort(x.begin(), x.end());
  cin >> n;
  int r = solve(n, 0);
  if (r < 0 || n == 1)
    r = -1;
  cout << r << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<long long> x;
int solve(long long p, long long pos) {
  if (p == 1)
    return 0;
  if (pos == x.size())
    return -999999;
  int maxn = -999999;
  if (p % x[pos] == 0) {
    maxn = solve(p / x[pos], pos) + 1;
  }
  return max(maxn, solve(p, pos + 1));
}
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));
    }
  }
  long long n;
  cin >> n;
  int r = solve(n, 0);
  if (r < 0 || n == 1)
    r = -1;
  cout << r << endl;
  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int ans = -1;
ll n;
vector<ll> a;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

void saiki(ll num, int idx, int sum) {
  if (num == 1)
    ans = max(sum, ans);
  if (num < a[idx] || idx == a.size())
    return;
  if (num % a[idx] == 0)
    saiki(num / a[idx], idx, sum + 1);
  saiki(num, idx + 1, sum);
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  if (n > 1)
    saiki(n, 0, 0);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int ans = -1;
ll n;
vector<ll> a;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

void saiki(ll num, int idx, int sum) {
  if (num == 1)
    ans = max(sum, ans);
  if (num < idx || idx == a.size())
    return;
  if (num % a[idx] == 0)
    saiki(num / a[idx], idx, sum + 1);
  saiki(num, idx + 1, sum);
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  if (n > 1)
    saiki(n, 0, 0);
  cout << ans << endl;
  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
typedef pair<int, P> iP;
ll n;
vector<ll> a;
queue<iP> Q;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

int bfs() {
  if (n == 1)
    return -1;
  int ans = -1;
  Q.push(iP(0, P(0, n)));
  while (!Q.empty()) {
    iP t = Q.front();
    Q.pop();
    int idx = t.first, sum = t.second.first;
    ll num = t.second.second;
    if (num == 1)
      ans = max(ans, sum);
    if (num < a[idx] || idx == a.size())
      continue;
    if (num % a[idx] == 0)
      Q.push(iP(idx, P(sum + 1, num / a[idx])));
    Q.push(iP(idx + 1, P(sum, num)));
  }
  return ans;
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  cout << bfs() << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, ll> P;
typedef pair<int, P> iP;
ll n;
vector<ll> a;
queue<iP> Q;

void mk28(ll div) {
  if (n < div)
    return;
  if (div)
    a.push_back(div);
  mk28(div * 10 + 2);
  mk28(div * 10 + 8);
}

int bfs() {
  int ans = -1;
  Q.push(iP(0, P(0, n)));
  while (!Q.empty()) {
    iP t = Q.front();
    Q.pop();
    int idx = t.first, sum = t.second.first;
    ll num = t.second.second;
    if (num == 1)
      ans = max(ans, sum);
    if (num < a[idx] || idx == a.size())
      continue;
    if (num % a[idx] == 0)
      Q.push(iP(idx, P(sum + 1, num / a[idx])));
    Q.push(iP(idx + 1, P(sum, num)));
  }
  return ans;
}

int main() {
  cin >> n;
  mk28(0);
  sort(a.begin(), a.end());
  cout << bfs() << endl;
  return 0;
}
Problem p01869 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<long long int> X;

void init(long long int N) {
  if (N > 1000000000000000000LL) {
    return;
  }
  if (N) {
    X.emplace_back(N);
  }
  N *= 10;
  init(N + 2);
  init(N + 8);
}

long long int saiki(long long int now, long long int p) {
  if (now == 1) {
    return 0;
  }
  long long int ret = -1;
  for (size_t i = p; i < X.size(); i++) {
    if (now % X[i] == 0) {
      long long int memo = saiki(now / X[i], i);
      if (memo != -1) {
        ret = max(ret, memo + 1);
      }
    }
  }
  return ret;
}

int main() {
  init(0LL);
  sort(X.rbegin(), X.rend());
  long long int N;
  cin >> N;
  if (N == 1) {
    cout << -1 << endl;
  } else {
    cout << saiki(N, 0) << endl;
  }
}

Error: TLE

Fixed submission:

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

vector<long long int> X;

void init(long long int N) {
  if (N > 1000000000000000000LL) {
    return;
  }
  if (N) {
    X.push_back(N);
  }
  N *= 10;
  init(N + 2);
  init(N + 8);
}

long long int saiki(long long int now, long long int p) {
  if (now == 1) {
    return 0;
  }
  long long int ret = -1;
  for (size_t i = p; i < X.size(); i++) {
    if (now % X[i] == 0) {
      long long int memo = saiki(now / X[i], i);
      if (memo != -1) {
        ret = max(ret, memo + 1);
      }
    }
  }
  return ret;
}

int main() {
  init(0);
  long long int N;
  cin >> N;
  if (N == 1) {
    cout << -1 << endl;
  } else {
    cout << saiki(N, 0) << endl;
  }
}
Problem p01869 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

vector<long long int> v;
map<pair<long long int, long long int>, int> mp;
int solve(long long int n, long long int from) {
  if (mp.find(make_pair(from, n)) == mp.end()) {

    {
      int ans = -1000;
      for (int i = 0; i <= from; ++i) {
        long long int a = v[i];
        if (n % a == 0) {
          if (a == n)
            ans = max(1, ans);
          else {

            ans = max(ans, 1 + solve(n / a, i));
          }
        }
      }
      mp[make_pair(from, n)] = ans;
    }
  }
  return mp[make_pair(from, n)];
}

int main() {
  long long int N;
  cin >> N;
  for (int i = 1; i <= 18; ++i) {
    for (int j = 0; j < (1 << i); ++j) {
      bitset<18> bs(j);
      long long int n = 0;
      for (int k = 0; k < i; ++k) {
        n *= 10;
        n += bs[i - k - 1] ? 8 : 2;
      }
      v.emplace_back(n);
    }
  }
  int ans = solve(N, v.size() - 1);
  if (ans < 0)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

vector<long long int> v;
map<pair<long long int, long long int>, int> mp;
int solve(long long int n, long long int from) {
  if (mp.find(make_pair(from, n)) == mp.end()) {

    {
      int ans = -1000;
      for (int i = 0; i <= from; ++i) {
        long long int a = v[i];
        if (n % a == 0) {
          if (a == n)
            ans = max(1, ans);
          else {

            ans = max(ans, 1 + solve(n / a, a));
          }
        }
      }
      mp[make_pair(from, n)] = ans;
    }
  }
  return mp[make_pair(from, n)];
}

int main() {
  long long int N;
  cin >> N;
  for (int i = 1; i <= 18; ++i) {
    for (int j = 0; j < (1 << i); ++j) {
      bitset<18> bs(j);
      long long int n = 0;
      for (int k = 0; k < i; ++k) {
        n *= 10;
        n += bs[i - k - 1] ? 8 : 2;
      }
      v.emplace_back(n);
    }
  }
  int ans = solve(N, v.size() - 1);
  if (ans < 0)
    ans = -1;
  cout << ans << endl;
  return 0;
}