532. Currency System

532. Currency System

Title:

There are n kinds of currencies with different denominations. The denomination of the i-th currency is a[i]. Each currency has an infinite number of currencies. The currencies can replace each other. For example, 6 is equal to two 3. Ask how many currencies are irreplacable

answer:

Let's change the question i: n kinds of currencies, the number is not limited, the value is a[i], how many kinds of currencies can be represented?
Classic backpack problem.
We first find out how many times each price can be expressed. If it can only be expressed once, it means that only he can express himself, that is, he cannot be replaced.

Code:

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
inline int read(){
    
    
   int s=0,w=1;
   char ch=getchar();
   while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
   while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);
   return s*w;
}
const int maxn=2e5+9;
int a[maxn];
int dp[maxn];
int main()
{
    
    
	int t;
	cin>>t;
	while(t--)
	{
    
    
		int n;
		cin>>n;
		int maxx=0;
		
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		for(int i=1;i<=n;i++)cin>>a[i],maxx=max(a[i],maxx);
		for(int i=1;i<=n;i++)
		{
    
    
			for(int j=a[i];j<=maxx;j++)dp[j]+=dp[j-a[i]];
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			if(dp[a[i]]==1)ans++;
		cout<<ans<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_35975367/article/details/114598309