跳到正文

题解

P6767题解

快斗游鹿约 2 分钟
原文
文章信息与版本

文章信息

发布
2023年7月19日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 2 分钟
获赞
5
题目
P6767

版本

  1. v1当前

先买性价比高的花一定最优。假设 aa 朵 bb 元性价比最高,那么有 ba<dc\dfrac{b}{a}<\dfrac{d}{c},同乘 acac,得 a×d>b×ca\times d>b\times c。先计算按每组 aa 朵买的花费,再去枚举买了 xx 组 cc 朵的情况。

直接枚举肯定会超时,那么考虑如何找到枚举的上界。如果 x×c=y×ax\times c=y\times a,就意味着我们可以通过买 yy 组 aa 朵的方式做到比买 xx 组 cc 朵更优,因为后者性价比高。所以枚举到 agcd⁡(a,c)\dfrac{a}{\gcd(a,c)} 即可。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a,b,c,d,ans=1145141919810000;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int getcost(int sum,int flo,int cost){
	if(sum<=0)return 0;
	if(sum%flo==0)return sum/flo*cost;
	else return (sum/flo+1)*cost;
}
signed main(){
	//freopen("std.in","r",stdin);
	n=read();a=read();b=read();c=read();d=read();
	//b/a<d/c->ad>bc
	if(a*d<b*c){
		swap(a,c);
		swap(b,d);
	}
	int k=a/__gcd(a,c);//枚举上限
	ans=getcost(n,a,b);
	for(int i=1;i<=k;i++){
		ans=min(ans,i*d+getcost(n-i*c,a,b));
	} 
	cout<<ans;
	return 0;
}

原文