You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 110; // N个正整数
const int M = 25010; // 表示的最大金额上限
int n; // 实际输入的正整数个数
int v[N]; // 每个输入的数字,也相当于占用的体积是多大
int f[M]; // 二维优化为一维的DP数组f[i]面额为i时的前序货币组成方案数
int main() {
int T;
cin >> T;
while (T--) {
// 每轮初始化一次dp数组因为有多轮
memset(f, 0, sizeof f);
cin >> n;
for (int i = 0; i < n; i++) cin >> v[i];
// 每个货币的金额,都只能由比它小的货币组装而成,需要排一下序
sort(v, v + n);
// 背包容量
int m = v[n - 1];
// 在总金额是0的情况下只有一种方案
f[0] = 1;
// 恰好装满:计算每个体积(面额)的组成方案
for (int i = 0; i < n; i++)
for (int j = v[i]; j <= m; j++)
f[j] += f[j - v[i]];
// 统计结果数
int res = 0;
for (int i = 0; i < n; i++)
// 如果当前面额的组成方案只有一种,那么它只能被用自己描述自己,不能让其它人描述自己
// 这个面额就必须保留
if (f[v[i]] == 1) res++;
// 输出结果
printf("%d\n", res);
}
return 0;
}