/* -*- coding: utf-8 -*- * * 2744.cc: No.2744 Power! or +1 - yukicoder */ #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MAX_N2 = MAX_N * 2; const long long LINF = 1LL << 62; /* typedef */ using ll = long long; using vb = vector; using vi = vector; using pii = pair; using vpii = vector; using pli = pair; /* global variables */ ll ds[MAX_N2]; bool fgs[MAX_N2]; int fs[MAX_N2]; /* subroutines */ inline void setmin(ll &a, ll b) { if (a > b) a = b; } template T gcd(T m, T n) { // m >= 0, n >= 0 if (m < n) swap(m, n); while (n > 0) { T r = m % n; m = n; n = r; } return m; } /* main */ int main() { int n, a, b, c; scanf("%d%d%d%d", &n, &a, &b, &c); int n2 = n * 2; ll b2 = (ll)b * b, b3 = b2 * b; for (int i = 2, r = n; i < n2; i++) { if (r > 1) r /= gcd(i, n); fgs[i] = (r == 1); } fs[0] = fs[1] = 1; for (int i = 2; i < n2; i++) { ll f = (ll)fs[i - 1] * i; fs[i] = (f < n) ? f : n + (f % n); } fill(ds, ds + n2, LINF); ds[1] = 0; priority_queue q; q.push({0, 1}); ll mind = LINF; while (! q.empty()) { auto [ud, u] = q.top(); q.pop(); ud = -ud; //printf(" u=%d, ud=%lld\n", u, ud); if (ds[u] != ud) continue; if (u == n) { mind = ud; break; } // op 1 { int v = u + 1; if (v >= n2) v = n + (v % n); ll vd = ud + a; if (ds[v] > vd) ds[v] = vd, q.push({-vd, v}); } // op 2 { ll v2 = (ll)u * u, v3 = v2 * u; if (v2 >= n2) v2 = n + (v2 % n); if (v3 >= n2) v3 = n + (v3 % n); ll vd2 = ud + b2, vd3 = ud + b3; if (ds[v2] > vd2) ds[v2] = vd2, q.push({-vd2, v2}); if (ds[v3] > vd3) ds[v3] = vd3, q.push({-vd3, v3}); } // op 3 { int v = fgs[u] ? n : fs[u]; ll vd = ud + c; if (ds[v] > vd) ds[v] = vd, q.push({-vd, v}); } } printf("%lld\n", mind); return 0; }