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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define ll long long
#define inf 1000000007
#define mod 1000000007
#define PI acos(-1)
#define pa pair<int, int>
#define pal pair<ll, ll>
#define ppa pair<int, pa>
#define EPS (1e-6)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

Point inter(Point a1, Point a2, Point b1, Point b2) {
  Point a = a2 - a1;
  Point b = b2 - b1;
  return a1 + a * cross(b, b1 - a1) / cross(b, a);
}

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ru[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

ll frac[100010];
ll inv[100010];

ll saikif(ll a, int n) {
  if (n == 1)
    return a % mod;
  else if (n % 2 == 0) {
    ll q = saikif(a, n / 2);
    return (q * q) % mod;
  } else
    return (a * saikif(a, n - 1)) % mod;
}

int beki[20];

void gyakugen() {
  frac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    frac[i] = (frac[i - 1] * i) % mod;
  for (int i = 0; i <= 100000; i++)
    inv[i] = saikif(frac[i], mod - 2);
}

//------------kokomade--tenpure---------
int l[20];
map<int, int> ma;

vector<ll> ve;
int saiki(ll n) {
  int q = 0;
  vector<int> ans;
  while (1) {
    if (beki[q] < n)
      q++;
    else
      break;
  }
  //	cout<<n<<" "<<q<<endl;

  if (n == -1)
    return -1;
  if (n == 1)
    return 0;
  if (ma.find(n) != ma.end())
    return ma[n];
  for (int i = 0; i < l[q]; i++) {
    if (n % ve[i] == 0) {
      int u = saiki(n / ve[i]);

      if (u >= 0)
        ans.push_back(saiki(n / ve[i]) + 1);
      //		cout<<saiki(n/ve[i])+1<<endl;
    }
  }
  int d = 0;
  if (ans.size() == 0) {
    ma[n] = -1;
    //	cout<<n<<" "<<ma[n]<<endl;
    return -1;
  } else {
    for (int i = 0; i < ans.size(); i++) {
      d = max(d, ans[i]);
    }
    ma[n] = d;
    //	cout<<n<<" "<<ma[n]<<endl;
    return d;
  }
}

signed main() {
  beki[0] = 1;
  for (int i = 1; i <= 18; i++) {
    beki[i] = beki[i - 1] * 10;
  }

  int r;
  ma[-1] = -1;
  ma[1] = 1;
  for (int i = 1; i <= 18; i++) {
    int ma = (1 << i);
    for (int j = 0; j < ma; j++) {
      r = 0;
      for (int k = 0; k < i; k++) {
        if ((j >> k) & 1 == 1)
          r = r * 10 + 2;
        else
          r = r * 10 + 8;
      }
      ve.push_back(r);
    }
    l[i] = ve.size();
    //	cout<<i<<" "<<l[i]<<endl;
  }

  ll n;
  cin >> n;
  if (n == 1) {
    cout << "-1" << endl;
    return 0;
  }
  int q = 0;
  while (1) {
    if (beki[q] < n)
      q++;
    else
      break;
  }
  // cout<<q<<endl;

  cout << saiki(n) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#include <vector>
#define int long long
#define ll long long
#define inf 1000000007
#define mod 1000000007
#define PI acos(-1)
#define pa pair<int, int>
#define pal pair<ll, ll>
#define ppa pair<int, pa>
#define EPS (1e-6)
#define equals(a, b) (fabs((a) - (b)) < EPS)

using namespace std;

class Point {
public:
  double x, y;
  Point(double x = 0, double y = 0) : x(x), y(y) {}
  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
  Point operator*(double a) { return Point(x * a, y * a); }
  Point operator/(double a) { return Point(x / a, y / a); }
  double absv() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; }
  bool operator==(const Point &p) const {
    return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
  }
};
typedef Point Vector;

struct Segment {
  Point p1, p2;
};

double hen(Vector a) {
  if (fabs(a.x) < EPS && a.y > 0)
    return acos(0);
  else if (fabs(a.x) < EPS && a.y < 0)
    return 3 * acos(0);
  else if (fabs(a.y) < EPS && a.x < 0)
    return 2 * acos(0);
  else if (fabs(a.y) < EPS && a.x > 0)
    return 0.0;
  else if (a.y > 0)
    return acos(a.x / a.absv());
  else
    return 2 * acos(0) + acos(-a.x / a.absv());
}
double cross(Vector a, Vector b) { return a.x * b.y - a.y * b.x; }

Point inter(Point a1, Point a2, Point b1, Point b2) {
  Point a = a2 - a1;
  Point b = b2 - b1;
  return a1 + a * cross(b, b1 - a1) / cross(b, a);
}

double dot(Vector a, Vector b) { return a.x * b.x + a.y * b.y; }

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ru[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};

ll frac[100010];
ll inv[100010];

ll saikif(ll a, int n) {
  if (n == 1)
    return a % mod;
  else if (n % 2 == 0) {
    ll q = saikif(a, n / 2);
    return (q * q) % mod;
  } else
    return (a * saikif(a, n - 1)) % mod;
}

void gyakugen() {
  frac[0] = 1;
  for (int i = 1; i <= 100000; i++)
    frac[i] = (frac[i - 1] * i) % mod;
  for (int i = 0; i <= 100000; i++)
    inv[i] = saikif(frac[i], mod - 2);
}

//------------kokomade--tenpure---------
int l;
map<int, int> ma;

vector<ll> ve;
int saiki(ll n) {
  vector<int> ans;
  if (n == -1)
    return -1;
  if (n == 1)
    return 0;
  if (ma.find(n) != ma.end())
    return ma[n];
  for (int i = 0; i < l; i++) {
    if (n % ve[i] == 0) {
      int u = saiki(n / ve[i]);

      if (u >= 0)
        ans.push_back(saiki(n / ve[i]) + 1);
      //		cout<<saiki(n/ve[i])+1<<endl;
    }
  }
  int d = 0;
  if (ans.size() == 0) {
    ma[n] = -1;
    //	cout<<n<<" "<<ma[n]<<endl;
    return -1;
  } else {
    for (int i = 0; i < ans.size(); i++) {
      d = max(d, ans[i]);
    }
    ma[n] = d;
    //	cout<<n<<" "<<ma[n]<<endl;
    return d;
  }
}

signed main() {
  int r;
  ma[-1] = -1;
  ma[1] = 1;
  for (int i = 1; i <= 17; i++) {
    int ma = (1 << i);
    for (int j = 0; j < ma; j++) {
      r = 0;
      for (int k = 0; k < i; k++) {
        if ((j >> k) & 1 == 1)
          r = r * 10 + 2;
        else
          r = r * 10 + 8;
      }
      ve.push_back(r);
    }
  }
  l = ve.size();
  ll n;
  cin >> n;
  if (n == 1) {
    cout << "-1" << endl;
    return 0;
  }
  cout << saiki(n) << endl;

  return 0;
}