acwing 532 货币系统

题面

在这里插入图片描述

题解

  1. 若两个货币系统等价,则存在以下性质

性质1:a1,a2,a3…an一定可以被表示出来
性质2:在最优解中,b1,b2,b3…bm 一定是从a1,a2,a3…an中选出来的
性质3: b1,b2,b3…bm一定不能被其他 bi 表示出来

  1. 我们将a数组从小到大排序

(1) 若ai 能被 a0–ai-1 表示,则这个数肯定不在b 数组中
(2) 若ai 不能被 a0–ai-1 表示 ,则这个数肯定存在于 b 数组中

  1. 我们看任意个a0—ai-1 能否表示出 ai ,这样就转化成了 完全背包问题 求方案数

代码

#include<bits/stdc++.h>

using namespace std;
const int N = 110, M = 25010;

int t, n;
int a[N];
int f[M];

int main() {
    
    

    cin >> t;
    while (t--) {
    
    
        memset(f, 0, sizeof f);
        cin >> n;
        for (int i = 1; i <= n; i++) cin >> a[i];
        sort(a + 1, a + 1 + n);
        int m = a[n];
        int res = 0;
        f[0] = 1;
        for (int i = 1; i <= n; i++) {
    
    
            if (!f[a[i]]) res++;
            for (int j = a[i]; j <= m; j++) {
    
    
                f[j] += f[j - a[i]];
            }
        }
        cout << res << endl;
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44791484/article/details/115302301