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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define pi acos(-1.0)
const ll INF = 1LL << 61;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = jou(N, P / 2);
    return t * t % mod;
  }
  return (N * jou(N, P - 1)) % mod;
} // jou(n,mod-2)で逆元
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

ll ans(ll n, ll a, ll b, ll c, ll d, map<ll, ll> &memo) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (memo.count(n))
    return memo.at(n);

  ll ko1 = ans(n / 2, a, b, c, d, memo) + (n % 2) * d + a;
  ll ko2 = ans((n + 1) / 2, a, b, c, d, memo) + (n % 2) * d + a;
  ll ko3 = ans(n / 3, a, b, c, d, memo) + (n % 3) * d + b;
  ll ko4 = ans((n + 2) / 3, a, b, c, d, memo) + (2 - ((n - 1) % 3)) * d + b;
  ll ko5 = ans(n / 5, a, b, c, d, memo) + (n % 5) * d + c;
  ll ko6 = ans((n + 4) / 5, a, b, c, d, memo) + (4 - ((n - 1) % 5)) * d + c;

  vector<ll> p = {ko1, ko2, ko3, ko4, ko5, ko6};
  sort(all(p));

  if (p[0] / d > n)
    p[0] = d * n;

  memo[n] = p[0];

  return p[0];
}

int main() {
  int t;
  cin >> t;

  map<ll, ll> memo;

  rep(i, t) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;

    memo.clear();

    cout << ans(n, a, b, c, d, memo) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define pi acos(-1.0)
const ll INF = 1LL << 61;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = jou(N, P / 2);
    return t * t % mod;
  }
  return (N * jou(N, P - 1)) % mod;
} // jou(n,mod-2)で逆元
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

ll ans(ll n, ll a, ll b, ll c, ll d, map<ll, ll> memo) {
  if (n == 1)
    return d;

  ll ko1, ko2, ko3, ko4, ko5, ko6, ko7;
  if (memo.count(n / 2))
    ko1 = memo.at(n / 2) + (n % 2) * d + a;
  else
    ko1 = ans(n / 2, a, b, c, d, memo) + (n % 2) * d + a;
  if (memo.count((n + 1) / 2))
    ko2 = memo.at((n + 1) / 2) + (n % 2) * d + a;
  else
    ko2 = ans((n + 1) / 2, a, b, c, d, memo) + (n % 2) * d + a;

  if (memo.count(n / 3))
    ko3 = memo.at(n / 3) + (n % 3) * d + b;
  else
    ko3 = ans(n / 3, a, b, c, d, memo) + (n % 3) * d + b;
  if (memo.count((n + 2) / 3))
    ko4 = memo.at((n + 2) / 3) + (2 - ((n - 1) % 3)) * d + b;
  else
    ko4 = ans((n + 2) / 3, a, b, c, d, memo) + (2 - ((n - 1) % 3)) * d + b;

  if (memo.count(n / 5))
    ko5 = memo.at(n / 5) + (n % 5) * d + c;
  else
    ko5 = ans(n / 5, a, b, c, d, memo) + (n % 5) * d + c;
  if (memo.count((n + 4) / 5))
    ko6 = memo.at((n + 4) / 5) + (4 - ((n - 1) % 5)) * d + c;
  else
    ko6 = ans((n + 4) / 5, a, b, c, d, memo) + (4 - ((n - 1) % 5)) * d + c;

  ko7 = n * d;

  vector<ll> p = {ko1, ko2, ko3, ko4, ko5, ko6, ko7};
  sort(all(p));
  memo[n] = p[0];

  return p[0];
}

int main() {
  int t;
  cin >> t;

  rep(i, t) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;

    map<ll, ll> memo;
    memo[0] = 0;
    memo[1] = d;

    cout << ans(n, a, b, c, d, memo) << endl;
  }
}