UVA 165 Stamps

Source: Internet
Author: User

Original question:

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), where H was the number of STA MPs, attached to a document, K are the number of denominations of stamps available, and N is the largest Attaina BLE value in a continuous sequence starting from 1.forinstance,ifh=3,k=2andthedenominationsare 1. For instance, if h=3, k=2 and the denominations is 1 and 4,wecanmakeallthevaluesfrom 4, we can make all the values from 1 to 6 (Aswellas 6 (as well as 8, 9and 9 and 12). However with the same values of H and K, but using 1and 1 and 3 stamps we can make all the values from 1to 1 to 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 stamps have been Lost–it is published in one of th E government reports but No-one can remember which one, and of the three researchers who started to search for the formula , 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 in to you. You doubt the existence of a formula in the first place so you decide to write a program this, 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 ascending order right justifie D in field 3 characters wide, followed by a space and a arrow (-) and the value of N (h,k) right justified in a field 3 characters wide.

Sample input

3 2
0 0

Sample output

1 3-7

English:
Give you two numbers, H and K. Indicates the use of H stamps, the denomination number does not exceed K. The maximum number of consecutive values that can be expressed.
For example h=3,k=2 can take two denominations of 1 and 2,1 and 2 in the case of using 3 stamps, can be continuously expressed 1,2,3,4,5,6
1<=h+k<=9

#include <bits/stdc++.h> using namespace std;
const int maxn=200;
int h,k;//h stamp k face number int ANS[MAXN],MAXSTAMPVAL,STAMPVAL[MAXN],MAXVAL[MAXN];

BOOL MARK[MAXN];
    void Get_ans (int cur,int n,int sum) {mark[sum]=true;
    if (cur>=h) return;
for (int i=0;i<=n;i++) Get_ans (Cur+1,n,sum+stampval[i]); } void Find_amount (int cur) {if (cur>=k) {if (maxval[cur-1]>maxstampval) {Maxs
            TAMPVAL=MAXVAL[CUR-1];
        memcpy (ans,stampval,sizeof (stampval));
    } return;
        } for (int i=stampval[cur-1]+1;i<=maxval[cur-1]+1;i++) {memset (mark,0,sizeof (Mark));
        Stampval[cur]=i;
        Get_ans (0,cur,0);
        int num=0,j=1;
        while (mark[j++]) num++;
        Maxval[cur]=num;
    Find_amount (cur+1);
    }} int main () {Ios::sync_with_stdio (false);
        while (cin>>h>>k,h+k) {stampval[0]=1;
        Maxval[0]=h;
 Maxstampval=int_min;       Find_amount (1);
        for (int i=0;i<k;i++) COUT&LT;&LT;SETW (3) <<ans[i];
    cout<< "-&LT;&LT;SETW" (3) <<maxStampVal<<endl;
} return 0;
 }

Solution:

The first reading was wrong, thought the optional denomination is 1 to 9. The result is to enumerate all denomination values and then use the Backpack solution, of course, WA.
Later read this blog just know this is a classic problem, continuous postage problem.
Simple enumeration ideas are relatively simple

The value of the denomination is enumerated first, and then the maximum number of consecutive numbers that can be composed by the current denomination is calculated based on the value of the enumeration.

The enumeration scope is key when enumerating a new denomination value. This range is [the maximum value currently owned by +1, the current stamp species can be represented by the maximum consecutive values of +1].
For example, the stamps currently in hand are 1 and 2, so the next one that needs to be enumerated must be in [3,7] less than 3, because there are 1 and 2. Using 1 and 2 for the maximum number of consecutive numbers is 6, then if the next denomination is greater than 7, then the number 7 will not appear.

There is a better solution, the specific idea is to use a similar state compression dynamic planning strategy. Set an array s (i) to represent the collection of 1->i using these denominations and using no more than M stamps. So when the i+1 stamp is enumerated, the new stamp that can be obtained is affixed by the method of all the methods in S (i) that are less than M, plus the denomination of the i+1 stamp. The resulting set of results is recorded as S (i+1). See the link above for specific code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.