標籤:acm uva brute force backtracking
題目如下:
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 20 0
Sample output
1 3 -> 7
連續郵資問題。給出貼郵票的位置個數和郵票不同面值的最大個數,求能夠組成的連續郵資的最大值。假設已經確定出了一些郵票,那麼下一張郵票的取值範圍應該是已經確定+的郵票的面值的最大值+1到已經確定的郵票能組成的連續郵資的最大值+1.因為如果再小的話就會跟已經確定的郵票重複,再大的話不能得出最大值+1這個郵資。這樣就對深搜加以限制,直接回溯就可以了。用check函數求出當前所有郵票可以組成的所有郵資。確定出一張新郵票之後,更新能組成的郵資,然後從以前的郵票可以組成的連續遊資的最大值開始遍曆,直到遇到一個不是值不是當前郵票可以組成的郵資,這樣連續郵資的最大值就被更新成了這個值-1,繼續深搜。
AC的代碼如下: