#P2744. [USACO5.3] 量取牛奶Milk Measuring

    ID: 1752 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>动态规划,dp搜索USACO深度优先搜索,DFS

[USACO5.3] 量取牛奶Milk Measuring

Description

Farmer John wants to measure QQ (1 ≤ Q ≤ 20,000) quarts of his finest milk and put it into a large bottle to sell. He always gives exactly what the customer wants, without any error.

Farmer John is very frugal. He is now buying some buckets at the cow hardware store to measure out QQ quarts from his huge pool of milk. Each bucket costs the same. Your task is to compute the smallest set of buckets he can buy such that, using only these buckets, he can measure exactly QQ quarts. Additionally, because he must carry them home, among two minimal bucket sets, he chooses the “smaller” one: sort both sets in ascending order, compare the first bucket; choose the set whose first bucket has the smaller capacity. If the first bucket is equal, compare the second, and so on, until they differ. For example, the set {3,5,7,100}\{3,5,7,100\} is better than the set {3,6,7,8}\{3,6,7,8\}.

To measure the milk, Farmer John may fill a bucket from the pool and pour it into the bottle. He never pours milk out of the bottle, nor does he pour a bucket’s milk anywhere else. With a 11-quart bucket, Farmer John can measure all possible numbers of quarts using only that bucket. Other bucket combinations are not so convenient.

Compute the best set of buckets to purchase. It is guaranteed that all testdata have at least one solution.

Input Format

The first line contains an integer QQ.

The second line contains an integer PP (1 ≤ P ≤ 100), the number of buckets available in the store.

Lines 33 through P+2P+2 each contain an integer hih_i representing the capacity of the ii-th bucket. (1hi104)(1\le h_i\le 10^4).

Output Format

Output exactly one line of space-separated integers:

First, the minimum number of buckets needed to measure the desired number of quarts, followed by:

a sorted list (in increasing order) of the capacities of the buckets to purchase.

16
3
3
5
7
2 3 5

Hint

Translation source: NOCOW.

USACO Training Section 5.3.

Translated by ChatGPT 5