AT_arc139_b [ARC139B] Make N 题解

5k_sync_closer

2023-08-22 09:56:55

Solution

令 $1$ 的性价比为 $\dfrac 1x$,$a$ 的性价比为 $\dfrac ay$,$b$ 的性价比为 $\dfrac bz$。 $1$ 的性价比最高,第二高的情况平凡,钦定 $\dfrac ay\ge \dfrac bz\ge \dfrac 1x$。 若 $a>\sqrt n$,则 $a$ 被选不超过 $\sqrt n$ 次,枚举 $a$ 被选的次数即可。 若 $a\le\sqrt n$,则 $b$ 被选不超过 $a$ 次,枚举 $b$ 被选的次数即可。 ```cpp #include <cstdio> #include <algorithm> #define int long long using namespace std; int T, n, a, b, x, y, z, q; signed main() { scanf("%lld", &T); while (T--) { scanf("%lld%lld%lld%lld%lld%lld", &n, &a, &b, &x, &y, &z); if (a * z < b * y) swap(a, b), swap(y, z); if (a * x <= y) printf("%lld\n", n * x); else if (b * x <= z) printf("%lld\n", n / a * y + n % a * x); else { q = 1e18; if (a * a > n) for (int i = 0; a * i <= n; ++i) q = min(q, y * i + (n - a * i) / b * z + (n - a * i) % b * x); else for (int i = 0; i <= a && b * i <= n; ++i) q = min(q, z * i + (n - b * i) / a * y + (n - b * i) % a * x); printf("%lld\n", q); } } return 0; } ```