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

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

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define forall(it, c)                                                          \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); ++it)

template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c & 15);
  if (!f)
    x = -x;
}

const ll INF = 5e18;

ll mul(ll x, ll y) {
  if (y && x >= INF / y)
    return INF;
  return x * y;
}

ll add(ll x, ll y) {
  if (x >= INF - y)
    return INF;
  return x + y;
}

const int coef[] = {2, 3, 5};
ll cost[3], d;
map<ll, ll> mem;

ll f(ll n) {
  if (!n)
    return 0;
  if (mem.count(n))
    return mem[n];
  ll ans = mul(d, n);
  rep(i, 3) {
    ans = min(ans, f(n / coef[i]) + cost[i] + mul((n % coef[i]), d));
    if (n / coef[i] + 1 != n)
      ans = min(ans,
                f(n / coef[i] + 1) + cost[i] + mul(coef[i] - (n % coef[i]), d));
  }
  mem[n] = ans;
  return ans;
}

void Main() {
  ll n;
  read(n);
  read(cost[0]);
  read(cost[1]);
  read(cost[2]);
  read(d);
  mem.clear();
  printf("%lld\\n", f(n));
}

int main() {
  int t;
  read(t);
  while (t--)
    Main();
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define mp make_pair
#define pb push_back

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define forall(it, c)                                                          \\
  for (__typeof(c.begin()) it = c.begin(); it != c.end(); ++it)

template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = 0;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c & 15);
  if (!f)
    x = -x;
}

const ll INF = 5e18;

ll mul(ll x, ll y) {
  if (y && x >= INF / y)
    return INF;
  return x * y;
}

ll add(ll x, ll y) {
  if (x >= INF - y)
    return INF;
  return x + y;
}

const int sz = 1 << 20;
ll mem[sz];

ll f(ll diff, vector<int> &v, ll d, int i = 0) {
  if (!diff)
    return 0;
  if (diff < sz && ~mem[diff])
    return mem[diff];
  if (i == int(v.size()))
    return mul(diff, d);
  ll ans = INF;
  if (diff % v[i] < v[i] - diff % v[i] || rand() % 2)
    ans = min(ans, f(diff / v[i], v, d, i + 1) + mul(diff % v[i], d));
  if (diff % v[i] > v[i] - diff % v[i] || rand() % 2)
    ans =
        min(ans, f(diff / v[i] + 1, v, d, i + 1) + mul(v[i] - diff % v[i], d));
  if (diff < sz)
    mem[diff] = ans;
  return ans;
}

void Main() {
  ll n;
  read(n);
  ll a, b, c, d;
  read(a);
  read(b);
  read(c);
  read(d);
  ll ans = INF;
  rep(cnt2, 100) rep(cnt3, 100) rep(cnt5, 100) {
    ll nw = 1;
    rep(i, cnt2) nw = mul(nw, 2);
    rep(i, cnt3) nw = mul(nw, 3);
    rep(i, cnt5) nw = mul(nw, 5);
    if (nw == INF)
      break;
    ll diff = abs(n - nw);
    vector<int> v;
    rep(i, cnt5) v.pb(5);
    rep(i, cnt3) v.pb(3);
    rep(i, cnt2) v.pb(2);
    ll cur = 0;
    cur = add(cur, mul(cnt2, a));
    cur = add(cur, mul(cnt3, b));
    cur = add(cur, mul(cnt5, c));
    if (cur >= ans)
      break;
    memset(mem, -1, sizeof(mem));
    cur = add(cur, f(diff, v, d));
    ans = min(ans, cur);
  }
  printf("%lld\\n", ans + d);
}

int main() {
  int t;
  read(t);
  while (t--)
    Main();
  return 0;
}