acwing 532 currency system

Topic

Insert picture description here

answer

  1. If the two currency systems are equivalent, there are the following properties

Property 1: a1, a2, a3...an must be represented.
Property 2: In the optimal solution, b1, b2, b3...bm must be a
property selected from a1, a2, a3...an 3: b1, b2 ,b3...bm must not be represented by other bi

  1. We sort the a array from small to large

(1) If ai can be represented by a0–ai-1, then this number must not be in the b array
(2) If ai cannot be represented by a0–ai-1, then this number must be in the b array

  1. Let's see if any number of a0—ai-1 can represent ai, which is transformed into a complete knapsack problem to find the number of solutions

Code

#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;
}

Guess you like

Origin blog.csdn.net/qq_44791484/article/details/115302301