[lnsyoj2244]凑数

题意

给定常数 N,A,B,X,Y,Z,求
min{αX+βY+γZ(α+βA+γB=N)}

sol

我们可以将 1,A,B 三者的性价比(即 X,YA,ZB)排序,性价比可能包括 6 种可能。其中,若 1 的性价比不劣于其他任一性价比,说明可以只使用 1 或是用性价比最高的填满后,再使用 1 填满。
因此,我们需要处理顺序为 A,B,1B,A,1 的情况。考虑到可以使用 swap 函数将两者交换,因此我们只考虑 A,B,1 的情况即可。
我们将 N 写为 klcm+b 的形式,其中,(k1)lcm 的部分可以完全使用 A 来计算,仅剩下 lcm+b 部分需要使用 A,B,1 共同计算。我们枚举较大的数,并计算剩余的部分代价为多少,取最小值即可。
算法复杂度为严格 O(N)

代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long LL;

const int N = 1000005;

LL f[N];
LL n, a, b, x, y, z;
int T;

int main(){
    scanf("%d", &T);
    while (T -- ){
        scanf("%lld%lld%lld%lld%lld%lld", &n, &a, &b, &x, &y, &z);
        double X = x, Y = 1.0 * y / a, Z = 1.0 * z / b;
        if (X <= Y && X <= Z) printf("%lld\n", 1ll * n * x);
        else if (X <= Y) printf("%lld\n", n / b * z + n % b * x);
        else if (X <= Z) printf("%lld\n", n / a * y + n % a * x);
        else {
            if (Y > Z) swap(a, b), swap(y, z), swap(Y, Z);
            LL lcm = a / __gcd(a, b) * b, L;
            if (lcm > n) L = n;
            else L = n % lcm + lcm;
            LL res = (n - L) / a * y;
            if (a < b) swap(a, b), swap(y, z), swap(Y, Z);
            LL res2 = 5e18;
            for (int i = 0; a * i <= L; i ++ )
                res2 = min(res2, 1ll * i * y + (L - a * i) / b * z + (L - a * i) % b * x);
            printf("%lld\n", res + res2);
        }
    }

    return 0;
}
posted @   是一只小蒟蒻呀  阅读(32)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示