The government of Nova Mareterrania requires that various legal documents has stamps attached to them so, the GOVERNM Ent can derive revenue from them. In terms of recent legislation, each class of document was limited in the number of stamps that may was attached to it. The Government wishes to know about many different stamps, and of what values, they need to print to allow the widest choice of values to is made up under these conditions. Stamps is always valued in units of $.
This had been analysed by government mathematicians who had derived a formula for N(h,K), wher E H is the number of stamps. Attached to a document, and K is the number of denominations of Stam PS available, and N is the largest attainable value of a continuous sequence starting from $. For instance, if h=3, K=2 and the denominations is $ and $4, we can make all the values from $ to $6 (as well as $8, $9 and $). However with the same values of H and K, but using $ and $ stamps we can make all the values from $ t o $7 (as well as $9). This was maximal, so n(3,2) = 7.
Unfortunately the formula relating n(h,K) to H, K and the values of the stamp S has been lost--it is published in one of the government reports but No-one can remember which one, and of the three res Earchers started to search for the formula, and the boredom third a job as a took lighthouse keeper E It provided more social stimulation.
The task has now been passed in 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, would determine an optimum set of stamps and the value of n(h,K).
Input
Input would consist of several lines, each containing a value for H and K. The file is being terminated by zeroes (0 0). For technical reasons the sum of H and K are limited to 9. (The president lost his little finger in a shooting accident and cannot count past 9).
Output
Output would consist of a line for each value of H and K consisting of the K stamp values in ASC Ending order right justified in fields 3 characters wide, followed by a space and a arrow ( ->
) and the value of n< /c9> (h,K) Right justified in a field 3 characters wide.
Sample Input
3 20 0
Sample Output
1 3-7
for this question, I want to say a little more of their own things, about the whole arrangement of the topic has been done a lot, the problem is to let me learn the most things, but also let me understand how weak. Just beginning to see this problem, and did not think how to search, and then thought for a long time , finally have a point of view, because to form a continuous value, so must have 1, but have 1 after what to do, if give me k kind of denominations, So what's my second range? What we think should be [2 ... H + 1], why is it starting from 2 ? I do not know, intuition, but I can explain why to H + 1, because H is empty, I can all fill in 1, then the maximum is H, if my second face value is H + 2, then it is impossible to continuous, so the maximum should be H + 1. The third face is similar to the previous one, and I will not say much. So what do we do with this question? First we use a DFS () to find the possible values for each face value, then use another DFS () to calculate all possible values that can be formed with these denominations. Of course the value of DFS () can be done with the idea of a full backpack DP.
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 7 intoccur[ +]; 8 intstamp[Ten];9 intans[Ten];Ten inth,k; One intMAXN; A intmaxvalue[Ten]; - - //cur indicates how many stamp,n are used to represent the seed number, sum represents the formed and the voidDfsintCurintNintsum) { - if(cur > H)return; -Occur[sum] =1; - for(inti =0; I <= n;i++) { +DFS (cur +1, N,sum +stamp[i]); - } + } A at - //cur Indicates the number of species - voidWorkintcur) {//cur is the kind of stamp; - if(cur = =k) { - if(Maxvalue[cur-1] >MAXN) { -MAXN = Maxvalue[cur-1]; inmemcpy (Ans,stamp,sizeof(stamp)); - } to } + Else { - for(inti = stamp[cur-1] +1; I <= maxvalue[cur-1] +1; i++) { theStamp[cur] =i; *memset (occur,0,sizeof(occur));//Remember to initialize each time you sum $Dfs0, cur,0);Panax Notoginseng intnum =0; - while(Occur[num +1]) num++;//max consecutive num theMaxvalue[cur] =num; +Work (cur +1); A } the } + } - $ intMain () { $ while(Cin >> H >> k,k +h) { -stamp[0] =1; -maxvalue[0] =h; theMAXN =0; -Work1);Wuyi for(inti =0; I < k;i++) { theprintf"%3d", Ans[i]); - } Wuprintf"->%3d\n", MAXN); - } About}
View Code
UVA-165 (Stamp)