10531 - 量取牛奶

农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出。消费者要多少,他就给多少,从不有任何误差。

农夫约翰总是很节约。他现在在奶牛五金商店购买一些桶,用来从他的巨大的牛奶池中量出 Q 夸脱的牛奶。每个桶的价格一样。你的任务是计算出一个农夫约翰可以购买的最少的桶的集合,使得能够刚好用这些桶量出 Q 夸脱的牛奶。另外,由于农夫约翰必须把这些桶搬回家,对于给出的两个极小桶集合,他会选择“更小的”一个,即:把这两个集合按升序排序,比较第一个桶,选择第一个桶容积较小的一个。如果第一个桶相同,比较第二个桶,也按上面的方法选择。否则继续这样的工作,直到相比较的两个桶不一致为止。例如,集合 {3,5,7,100} 比集合 {3,6,7,8} 要好。

为了量出牛奶,农夫约翰可以从牛奶池把桶装满,然后倒进瓶子。他决不把瓶子里的牛奶倒出来或者把桶里的牛奶倒到别处。用一个容积为 1 夸脱的桶,农夫约翰可以只用这个桶量出所有可能的夸脱数。其它的桶的组合没有这么方便。

计算需要购买的最佳桶集,保证所有的测试数据都至少有一个解。

输入

Line 1: 一个整数 Q
Line 2: 一个整数P(1 <= P <= 100),表示商店里桶的数量
Lines 3..P+2: 每行包括一个桶的容积(1 <= 桶的容积 <= 10000)

输出

输出文件只有一行,由空格分开的整数组成:

为了量出想要的夸脱数,需要购买的最少的桶的数量,接着是: 一个排好序的列表(从小到大),表示需要购买的每个桶的容积

样例

输入

16 3 3 5 7 

输出

2 3 5

提示

Milk Measuring Hal Burch Farmer John must measure Q (1 <= Q <= 20,000) quarts of his finest milk and deliver it in one big bottle to a customer. He fills that bottle with exactly the number of quarts that the customer orders.

Farmer John has always been frugal. He is at the cow hardware store where he must purchase a set of pails with which to measure out Q quarts of milk from his giant milk tank. Since the pails each cost the same amount, your task is to figure out a minimal set of pails Farmer John can purchase in order to fill a bottle with exactly Q quarts of milk. Additionally, since Farmer John has to carry the pails home, given two minimal sets of pails he should choose the "smaller" one as follows: Sort the sets in ascending order. Compare the first pail in each set and choose the set with the smallest pail. If the first pails match, compare the second pails and choose from among those, else continue until the two sets differ. Thus the set {3, 5, 7, 100} should be chosen over {3, 6, 7, 8}.

To measure out milk, FJ may completely fill a pail from the tank and pour it into the bottle. He can never remove milk from the bottle or pour milk anywhere except into the bottle. With a one-quart pail, FJ would need only one pail to create any number of quarts in a bottle. Other pail combinations are not so convenient.

Determine the optimally small number of pails to purchase, given the guarantee that at least one solution is possible for all contest input data.

PROGRAM NAME: milk4 INPUT FORMAT Line 1: The single integer Q
Line 2: A single integer P (1 <= P <= 100) which is the number of pails in the store
Lines 3..P+2: Each line contains a single integer pail_value (1 <= pail_value <= 10000), the number of quarts a pail holds

SAMPLE INPUT (file milk4.in) 16 3 3 5 7

OUTPUT FORMAT The output is a single line of space separated integers that contains:

the minimum number of pails required to measure out the desired number of quarts, followed by: a sorted list (from smallest to largest) of the capacity of each of the required pails SAMPLE OUTPUT (file milk4.out) 2 3 5

时间限制 1 秒
内存限制 128 MB
统计
上一题 下一题