The face value of the Luogu P2001 coin


The face value of the Luogu P2001 coin


Topic portal
is really a cancer problem
initially think this is a multi-board backpack questions, so without hesitation beat up, samples have not seen, Made in question, they are not ( and the blind )
and then carefully read the question again
it is clear that we It can be found that if the minimum denomination of a given coin is greater than 1, then it must output No answer! ! ! Because the price of 1 cannot be formed first.
Then think about it, assuming that the i-th coin has used the smallest number of coins ans to form the maximum feasible price tot,
then if tot>=a[i+1]-1, then i++ can be directly; otherwise, tot+=a[i] until tot>=a[i+1]-1; At the same time every time ans++.
Because this is an accumulation and the data range is large, it is easy to get stuck. So we have to use one step = small division: k=(a[i+1]-2-tot)/a[i]+1, where k is to add k a[i] to satisfy the condition, so we don’t need one Added.
Finally, note that if tot happens to be equal to m−1 and the loop ends, then we have to output ans+1 outside the loop, which is a special judgment.
AC code

#include<bits/stdc++.h>
using namespace std;
long long n,m,a[2000005],ans,tot;
int main()
{
    
    
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	sort(a+1,a+n+1);
	if(a[1]!=1) 
	{
    
    
		cout<<"No answer!!!";
		return 0;
	}
	a[n+1]=m;
	for(int i=1;i<=n;i++)
	{
    
    
		if(tot<a[i+1]-1)
		{
    
    
			long long k=(a[i+1]-2-tot)/a[i]+1;
			tot+=a[i]*k;
			ans+=k;
			if(tot>=m)
			{
    
    
				cout<<ans;
				return 0;
			}
		}
	}
	cout<<ans+1;
	return 0; 
}

At last

Do not open long long to see the ancestors! ! !

Guess you like

Origin blog.csdn.net/weixin_50624971/article/details/114649170