首页 > 代码库 > UVA - 165(邮票)

UVA - 165(邮票)

 Stamps 

The government of Nova Mareterrania requires that various legal documents have stamps attached to them so that the government can derive revenue from them. In terms of recent legislation, each class of document is limited in the number of stamps that may be attached to it. The government wishes to know how many different stamps, and of what values, they need to print to allow the widest choice of values to be made up under these conditions. Stamps are always valued in units of $1.

 

This has been analysed by government mathematicians who have derived a formula for n(h,k), where h is the number of stamps that may be attached to a document, k is the number of denominations of stamps available, and n is the largest attainable value in a continuous sequence starting from $1. For instance, if h=3, k=2 and the denominations 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 of h and k, but using $1 and $3 stamps we can make all the values from $1 to $7 (as well as $9). This is maximal, so n(3,2) = 7.

 

Unfortunately the formula relating n(h,k) to hk and the values of the stamps has been lost--it was published in one of the government reports but no-one can remember which one, and of the three researchers who started to search for the formula, two died of boredom and the third took a job as a lighthouse keeper because it provided more social stimulation.

The task has now been passed on to you. You doubt the existence of a formula in the first place so you decide to write a program that, for given values of h and k, will determine an optimum set of stamps and the value of n(h,k).

 

Input

Input will consist of several lines, each containing a value for h and k. The file will be terminated by two zeroes (0 0). For technical reasons the sum of h and k is limited to 9. (The President lost his little 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 of the k stamp values in ascending order right justified in fields 3 characters wide, followed by a space and an arrow (->) and the value of n(h,k) right justified in a field 3 characters wide.

 

Sample input

 

3 20 0

 

Sample output

   1 3 -> 7


  对于这个题,我想多说点自己的东西,有关全排列的题目已经做了很多了,这个题是让我学到东西最多的,也让我明白了自己到底是有多弱。刚开始看到这个题时,并没有想到该怎么搜,后来想了很久,终于有了点头绪,因为要形成连续的value,所以必须要有1,但是有了1之后该怎么办呢,如果给了我K种denominations,那我第二种的范围该是什么,我们想想应该是[2 ... h + 1],为什么是从2开始呢?我也不知道,直觉,但是我能解释为什么到h + 1, 因为h个空,我可以都填上1,那么最大值就是h,如果我的第二种面值是h + 2,那么不可能连续,所以最大应该是h + 1。第三种面值与前面的类似,我就不多说了。所以这个题,我们该怎么做呢? 第一我们用一个dfs() 来找每种面值的可能值,那么用另一个dfs()来计算用这些面值能形成的所有可能值。当然求值的dfs()可以用完全背包的思想DP搞定。


 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4  5 using namespace std; 6  7 int occur[1000];  8 int stamp[10]; 9 int ans[10];10 int h,k;11 int maxn;12 int maxValue[10];13 14 // cur 表示用了多少张stamp,n表示种数,sum表示形成的和15 void dfs(int cur,int n,int sum) { 16     if (cur > h) return;17     occur[sum] = 1;18     for (int i = 0;i <= n;i++) {19         dfs(cur + 1,n,sum + stamp[i]);20     }21 }22 23 24 // cur 表示种数 25 void work(int cur) { // cur is the kind of stamp;26     if (cur == k) {27         if (maxValue[cur - 1] > maxn) {28             maxn = maxValue[cur - 1];29             memcpy(ans,stamp,sizeof(stamp));30         }31     }32     else {33         for (int i = stamp[cur - 1] + 1;i <= maxValue[cur - 1] + 1;i++) {34             stamp[cur] = i;35             memset(occur,0,sizeof(occur)); // 每次求和的时候记得初始化36             dfs(0,cur,0);37             int num = 0;38             while (occur[num + 1]) num++; //最大连续num39             maxValue[cur] = num;40             work(cur + 1);41         }42     }43 }44 45 int main () {46     while (cin >> h >> k,k + h) {47         stamp[0] = 1;48         maxValue[0] = h;49         maxn = 0;50         work(1);51         for (int i = 0;i < k;i++) {52             printf("%3d",ans[i]);53         }54         printf(" ->%3d\n",maxn);55     }56 }
View Code

 




UVA - 165(邮票)