提交时间:2025-03-02 15:32:38

运行 ID: 37077

#include <bits/stdc++.h> #define int long long using namespace std; int c, n; int frc[16]; signed main() { int c; frc[0] = 1; for (int i = 1; i <= 15; i++) frc[i] = frc[i - 1] * i; scanf("%lld", &c); scanf("%lld", &n); while (n--) { // if (c != 1) // puts("0"); // else // { int x, a, b, c, d; scanf("%lld %lld %lld %lld %lld", &x, &a, &b, &c, &d); int ans = 0x3f3f3f3f3f3f3f3f; for (int i = 0; i < (1 << 13); i++) { int j = 0, tot = 0, tg = 0; int ii = i; while (ii) { j = __lg(ii & (-ii)) + 3; tg += j; ii -= (ii & (-ii)); tot += frc[j]; if (tot > x) break; } if (tot > x) continue; int xx = x - tot, xxx; int lt = x & 3; xx -= lt; xxx = xx; int cc = 0; while (xxx) { cc += __lg(xxx & (-xxx)); xxx -= xxx & (-xxx); } int h = 0; if (lt & 1) h += min(a + b, c); if (lt & 2) h += min(a + 2 * b, c + d); ans = min(ans, __builtin_popcount(i) * a + b * tg + __builtin_popcount(xx) * c + cc * d + h); } printf("%lld\n", ans); // } } }