UVA 165 - Stamps


 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 forn(h,k), whereh is the number of stamps that may be attached to a document,k is the number of denominations of stamps available, andn is the largest attainable value in a continuous sequence starting from $1. For instance, ifh=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 ofh andk, 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 relatingn(h,k) toh, k 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 ofh andk, will determine an optimum set of stamps and the value ofn(h,k).

Input

Input will consist of several lines, each containing a value forh andk. The file will be terminated by two zeroes (0 0). For technical reasons the sum ofh andk 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 ofh andk 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 ofn(h,k) right justified in a field 3 characters wide.

Sample input

3 2
0 0

Sample output

  1  3 ->  7

给你h和k,h是邮票张数,k是有k种面额的,问你面额分别是多少可以从1开始连续达到一个最大的数(可以自由组合)。

虽然知道是回溯,但一开始的时候还是不会做,百度了才知道。。关键是回溯时面额的取值范围,要记录下此时已经达到的最大值,因此还要一个dfs来找出此时达到的最大值。

每次回溯面额的取值范围就是上一次的面额+1到上一次的最大值+1(不能再大了,如果再大就不可能达到上一次的面额+1这个数)。

代码:

<span style="font-family:SimSun;font-size:18px;">#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int h,k,Max,ma[10],stamp[10],ans[10],vis[200]; //ma记录每次回溯最大值,stamp记录面额
void dfs(int cur,int n,int s)  //找出此时能够达到的值
{
    vis[s]=1;
    if(cur>=h) return;
    int i;
    for(i=0; i<n; i++)
        dfs(cur+1,n,s+stamp[i]);
}

void search(int cur)
{
    int i,j;
    if(cur>=k)
    {
        if(ma[cur-1]>Max)
        {
            for(i=0; i<k; i++) ans[i]=stamp[i];
            Max=ma[cur-1];
        }
        return;
    }
    for(i=stamp[cur-1]+1; i<=ma[cur-1]+1; i++)
    {
        stamp[cur]=i;
        memset(vis,0,sizeof(vis));
        dfs(0,cur+1,0);
        for(j=1; vis[j]==1; j++); //找出满足的的最大值
        ma[cur]=j-1;
        search(cur+1);
    }
}
int main()
{
    while(scanf("%d%d",&h,&k),h&&k)
    {
        stamp[0]=1;
        ma[0]=h;
        Max=0;
        search(1);
        int i;
        for(i=0; i<k; i++) printf("%3d",ans[i]);
        printf(" ->%3d\n",Max);
    }
    return 0;
}
</span>


你可能感兴趣的:(UVA 165 - Stamps)