AcWing 532. 货币系统
原题链接
中等
作者:
张大爷8号
,
2024-11-29 22:23:21
,
所有人可见
,
阅读 4
转化成完全背包
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110;
int q, n;
int f[N], a[N];
int main()
{
cin >> q;
while (q -- )
{
cin >> n;
int k = 0, cnt = 0;
memset(a, 0, sizeof(a));
memset(f, 0, sizeof(f));
f[0] = 1;
for (int i = 1; i <= n; i ++ )
{
cin >> a[i];
if (a[i] > k) k = a[i];
}
for (int i = 1; i <= n; i ++ )
{
for (int j = a[i]; j <= k; j ++ )
{
f[j] = f[j] + f[j - a[i]];
}
}
for (int i = 1; i <= n; i ++ )
if (f[a[i]] == 1) cnt ++;
cout << cnt << endl;
}
return 0;
}