1 条题解

  • 0
    @ 2025-10-8 16:55:20
    #include <bits/stdc++.h>
    using namespace std;
    int maxdep, m, n;
    int a[105], c[105], sta[105], top, ok;
    
    void dfs(int p, int dep, int s) {
        if (s == m) {
            if (!ok) for (int i = 1; i <= top; i++) c[i] = sta[i];
            else {
                for (int i = 1; i <= top; i++) {
                    if (sta[i] > c[i]) break;
                    if (sta[i] < c[i]) {
                        for (int j = i; j <= top; j++) c[j] = sta[j];
                        break;
                    }
                }
            }
            ok = true;
        }
        if (s > m || p > n || dep > maxdep) return;
        for (int i = p; i <= n; i++) {
            if (s + a[i] > m) break;
            sta[++top] = a[i];
            for (int j = 1; s + j * a[i] <= m; j++) dfs(i + 1, dep + 1, s + j * a[i]);
            top--;
        }
    }
    
    int main() {
        scanf("%d%d", &m, &n);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        sort(a + 1, a + n + 1);
        n = unique(a + 1, a + n + 1) - (a + 1);
        ok = 0; top = 0;
        for (maxdep = 1;; maxdep++) {
            dfs(1, 1, 0);
            if (ok) {
                printf("%d", maxdep);
                for (int i = 1; i <= maxdep; i++) printf(" %d", c[i]);
                puts("");
                break;
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1054
    时间
    1000ms
    内存
    128MiB
    难度
    6
    标签
    递交数
    24
    已通过
    12
    上传者