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

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

unordered_map<uint64_t, uint64_t> memo;

uint64_t _getCeilNum(uint64_t n, int divNum) {
  uint64_t ans = (n + divNum - 1) / divNum;
  if (ans < 0)
    return -ans;
  return ans;
}

uint64_t _getFloorNum(uint64_t n, int divNum) {
  uint64_t ans = n / divNum;
  if (ans < 0)
    return -ans;
  return ans;
}

uint64_t walk(uint64_t n, uint64_t A, uint64_t B, uint64_t C, uint64_t D) {
  if (n == 0)
    return 0;
  if (n == 1)
    return D;
  if (memo.find(n) != memo.end()) {
    return memo[n];
  }

  uint64_t result = min({D * (_getCeilNum(n, 5) * 5 - n) + C +
                             walk(_getCeilNum(n, 5), A, B, C, D),
                         D * (n - _getFloorNum(n, 5) * 5) + C +
                             walk(_getFloorNum(n, 5), A, B, C, D),
                         D * (_getCeilNum(n, 3) * 3 - n) + B +
                             walk(_getCeilNum(n, 3), A, B, C, D),
                         D * (n - _getFloorNum(n, 3) * 3) + B +
                             walk(_getFloorNum(n, 3), A, B, C, D),
                         D * (_getCeilNum(n, 2) * 2 - n) + A +
                             walk(_getCeilNum(n, 2), A, B, C, D),
                         D * (n - _getFloorNum(n, 2) * 2) + A +
                             walk(_getFloorNum(n, 2), A, B, C, D),
                         D * n});

  memo[n] = result;
  return result;
}

int main() {
  int T;
  cin >> T;
  vector<vector<uint64_t>> testCaseList(T, vector<uint64_t>(5));
  for (int i = 0; i < T; ++i) {
    cin >> testCaseList.at(i).at(0) >> testCaseList.at(i).at(1) >>
        testCaseList.at(i).at(2) >> testCaseList.at(i).at(3) >>
        testCaseList.at(i).at(4);
  }

  for (auto testCase : testCaseList) {
    memo.clear();
    cout << walk(testCase.at(0), testCase.at(1), testCase.at(2), testCase.at(3),
                 testCase.at(4))
         << endl;
    ;
  }
}


Error: TLE

Fixed submission:

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

unordered_map<uint64_t, uint64_t> memo;
uint64_t walk(uint64_t n, uint64_t A, uint64_t B, uint64_t C, uint64_t D) {
  if (n == 0)
    return 0;
  if (n == 1)
    return D;

  uint64_t result = min({D * ((uint64_t)ceil(1.0 * n / 5) * 5 - n) + C +
                             walk((uint64_t)ceil(1.0 * n / 5), A, B, C, D),
                         D * ((uint64_t)ceil(1.0 * n / 3) * 3 - n) + B +
                             walk((uint64_t)ceil(1.0 * n / 3), A, B, C, D),
                         D * ((uint64_t)ceil(1.0 * n / 2) * 2 - n) + A +
                             walk((uint64_t)ceil(1.0 * n / 2), A, B, C, D),
                         D * (n - (uint64_t)floor(1.0 * n / 5) * 5) + C +
                             walk((uint64_t)floor(1.0 * n / 5), A, B, C, D),
                         D * (n - (uint64_t)floor(1.0 * n / 3) * 3) + B +
                             walk((uint64_t)floor(1.0 * n / 3), A, B, C, D),
                         D * (n - (uint64_t)floor(1.0 * n / 2) * 2) + A +
                             walk((uint64_t)floor(1.0 * n / 2), A, B, C, D),
                         D * n});

  return result;
}

int main() {
  int T;
  cin >> T;
  vector<vector<uint64_t>> testCaseList(T, vector<uint64_t>(5));
  for (int i = 0; i < T; ++i) {
    cin >> testCaseList.at(i).at(0) >> testCaseList.at(i).at(1) >>
        testCaseList.at(i).at(2) >> testCaseList.at(i).at(3) >>
        testCaseList.at(i).at(4);
  }

  for (auto testCase : testCaseList) {
    memo.clear();
    cout << walk(testCase.at(0), testCase.at(1), testCase.at(2), testCase.at(3),
                 testCase.at(4))
         << endl;
    ;
  }
}