首页 > 代码库 > UVA Stamps
UVA Stamps
题目如下:
Stamps
Stamps |
The government of Nova Mareterrania requires that various legaldocuments have stamps attached to them so that the government canderive revenue from them. In terms of recent legislation, each classof document is limited in the number of stamps that may be attached toit. The government wishes to know how many different stamps, and ofwhat values, they need to print to allow the widest choice of valuesto be made up under these conditions. Stamps are always valued inunits of $1.
This has been analysed by government mathematicians who have derived aformula forn(h,k), whereh is the number of stamps that may beattached to a document,k is the number of denominations of stampsavailable, andn is the largest attainable value in a continuoussequence starting from $1. For instance, ifh=3,k=2 and thedenominations are $1 and $4, we can make all the values from $1 to$6 (as well as $8, $9 and $12). However with the same values ofhandk, but using $1 and $3 stamps we can make all the values from$1 to $7 (as well as $9). This is maximal, son(3,2) = 7.
Unfortunately the formula relating n(h,k) to h,k and the values ofthe stamps has been lost--it was published in one of the governmentreports but no-one can remember which one, and of the threeresearchers who started to search for the formula, two died of boredomand the third took a job as a lighthouse keeper because it providedmore social stimulation.
The task has now been passed on to you. You doubt the existence of aformula in the first place so you decide to write a program that, forgiven values ofh andk, will determine an optimum set of stamps andthe value ofn(h,k).
Input
Input will consist of several lines, each containing a value for h andk. The file will be terminated by two zeroes (0 0). For technicalreasons the sum ofh andk is limited to 9. (The President lost hislittle finger in a shooting accident and cannot count past 9).
Output
Output will consist of a line for each value of h and k consisting ofthek stamp values in ascending order right justified in fields 3characters wide, followed by a space and an arrow (->
) and the valueofn(h,k) right justified in a field 3 characters wide.
Sample input
3 2 0 0
Sample output
1 3 -> 7
连续邮资问题。给出贴邮票的位置个数和邮票不同面值的最大个数,求能够组成的连续邮资的最大值。假设已经确定出了一些邮票,那么下一张邮票的取值范围应该是已经确定+的邮票的面值的最大值+1到已经确定的邮票能组成的连续邮资的最大值+1.因为如果再小的话就会跟已经确定的邮票重复,再大的话不能得出最大值+1这个邮资。这样就对深搜加以限制,直接回溯就可以了。用check函数求出当前所有邮票可以组成的所有邮资。确定出一张新邮票之后,更新能组成的邮资,然后从以前的邮票可以组成的连续游资的最大值开始遍历,直到遇到一个不是值不是当前邮票可以组成的邮资,这样连续邮资的最大值就被更新成了这个值-1,继续深搜。
AC的代码如下: