Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ld = long double;

ll a[3] = {2, 3, 5}, c[3], dd;
map<ll, ll> dp;

ll go(ll n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return dd;
  if (dp.count(n))
    return dp[n];
  ll cm = (n < 2e9 ? n * dd : 1e18);
  rep(i, 3) {
    cm = min(cm, n % a[i] * dd + c[i] + go(n / a[i]));
    if (n % a[i])
      cm = min(cm, (a[i] - n % a[i]) * dd + c[i] + go(n / a[i] + 1));
  }
  dp[n] = cm;
  return cm;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, t;
  cin >> t;
  rep(z, t) {
    cin >> n;
    rep(i, 3) cin >> c[i];
    cin >> dd;
    dp.clear();
    cout << go(n) << '\\n';
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = __int128;
using ld = long double;

const int N = 63;
ll a[3] = {2, 3, 5}, c[3], pw[3][N], inf = 5.1e18, d[2][N][N][N], p[3];

void rel(ll &x, ll y) {
  if (y < x)
    x = y;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  rep(i, 3) {
    pw[i][0] = 1;
    rep(j, N - 1) {
      pw[i][j + 1] = pw[i][j] * a[i];
      if (pw[i][j + 1] > inf)
        break;
    }
  }
  long long t, id, in, ic;
  ll dd, n;
  cin >> t;
  rep(z, t) {
    cin >> in;
    n = in;
    rep(i, 3) {
      cin >> ic;
      c[i] = ic;
    }
    cin >> id;
    dd = id;
    rep(i, N) rep(j, N) rep(k, N) d[0][i][j][k] = d[1][i][j][k] = inf;
    d[0][0][0][0] = d[1][0][0][0] = 0;
    ll ans = inf;
    for (p[0] = 0; pw[0][p[0]] <= n * 5; p[0]++)
      for (p[1] = 0; pw[0][p[0]] * pw[1][p[1]] <= n * 5; p[1]++)
        for (p[2] = 0; pw[0][p[0]] * pw[1][p[1]] * pw[2][p[2]] <= n * 5;
             p[2]++) {
          ll pr = pw[0][p[0]] * pw[1][p[1]] * pw[2][p[2]];
          rep(i, 2) {
            ll nn = (i ? (n + pr - 1) / pr : n / pr);
            ll od = d[i][p[0]][p[1]][p[2]];
            if (nn < 1e9)
              ans = min(ans, od + nn * dd);
            rep(j, 3) {
              p[j]++;
              rep(k, 2) {
                ll nw = (k ? (nn + a[j] - 1) / a[j] : nn / a[j]);
                rel(d[nw > n / (pr * a[j])][p[0]][p[1]][p[2]],
                    od + (k ? nw * a[j] - nn : nn - nw * a[j]) * dd + c[j]);
              }
              p[j]--;
            }
          }
        }
    cout << (long long)ans << '\\n';
  }
}