/* -*- coding: utf-8 -*- * * 2744.cc: No.2744 Power! or +1 - yukicoder */ #include #include #include #include using namespace std; /* constant */ const int MAX_N = 200000; const int MAX_N2 = MAX_N * 2; const int MAX_P = 200000; /* typedef */ using ll = long long; using vb = vector; using vi = vector; using pii = pair; using vpii = vector; /* global variables */ vb primes; vi pnums; ll dp[MAX_N2 + 1]; /* subroutines */ int gen_primes(int maxp) { primes.assign(maxp + 1, true); primes[0] = primes[1] = false; int p; for (p = 2; p * p <= maxp; p++) if (primes[p]) { pnums.push_back(p); for (int q = p * p; q <= maxp; q += p) primes[q] = false; } for (; p <= maxp; p++) if (primes[p]) pnums.push_back(p); return (int)pnums.size(); } bool prime_decomp(int n, vpii& pds) { pds.clear(); for (auto pi: pnums) { if (pi * pi > n) { if (n > 1) pds.push_back(pii(n, 1)); return true; } if (n % pi == 0) { int fi = 0; while (n % pi == 0) n /= pi, fi++; pds.push_back(pii(pi, fi)); } } return false; } 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() { gen_primes(MAX_P); 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; vpii npds; prime_decomp(n, npds); //for (auto [p, f]: npds) printf(" %d^%d", p, f); putchar('\n'); dp[0] = dp[1] = 0; for (int i = 2; i <= n2; i++) dp[i] = dp[i - 1] + a; ll f = 1; for (int x = 1; x < n2; x++) { // op 1 setmin(dp[x + 1], dp[x] + a); // op 2 if (x > 1) { ll x2 = (ll)x * x, x3 = x2 * x; if (x2 <= n2) setmin(dp[x2], dp[x] + b2); if (x3 <= n2) setmin(dp[x3], dp[x] + b3); } // op 3 if (x > 1 && f < n2) { f *= x; if (f <= n2 && f > x) setmin(dp[f], dp[x] + c); } } //printf(" dp[n]=%lld\n", dp[n]); ll mind = dp[n]; for (int x = 2; x < n2; x++) { int maxk = 0; for (auto [p, f]: npds) { if (x % p != 0) { maxk = 0; break; } int fx = 0; for (int y = x; y % p == 0; y /= p) fx++; maxk = max(maxk, (f + fx - 1) / fx); } if (maxk >= 2) { if (maxk & 1) { int c2 = (maxk - 3) / 2; setmin(mind, dp[x] + b2 * c2 + b3); } else { int c2 = maxk / 2; setmin(mind, dp[x] + b2 * c2); } } } //printf(" mind=%lld\n", mind); for (int x = 2, r = n; x < n2; x++) { if (r > 1) r /= gcd(x, r); if (r == 1) setmin(mind, dp[x] + c); } printf("%lld\n", mind); return 0; }