###### UVa 165 Stamps 【DFS】

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 h, 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 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 2
0 0

## Sample output

  1  3 ->  7

#include<cstdio>
#include<cstring>
#define maxn 1001
using namespace std;
int h,k,msum;
int maxval[maxn],staval[maxn],ans[maxn];
bool vis[maxn];
void dfs(int c,int n,int sum)//张数为c种类为n组成的金额为sum
{
vis[sum]=true;
if(c>=h)
return ;
for(int i=0;i<=n;++i)
dfs(c+1,n,sum+staval[i]);
}
void rsearch(int c)
{
if(c>=k)
{
if(maxval[c-1]>msum)
{
msum=maxval[c-1];
memcpy(ans,staval,sizeof(staval));
}
return ;
}
for(int i=staval[c-1]+1;i<=maxval[c-1]+1;++i)
{
staval[c]=i;
memset(vis,false,sizeof(vis));
dfs(0,c,0);
int num=0,j=1;
while(vis[j++])
++num;
maxval[c]=num;
rsearch(c+1);
}
}
int main()
{
while(scanf("%d%d",&h,&k),h+k)
{
staval[0]=1;
maxval[0]=h;
msum=-1;
rsearch(1);
for(int i=0;i<k;++i)
printf("%3d",ans[i]);
printf(" ->%3d\n",msum);
}
return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define maxn 1001
using namespace std;
int h,k,msum;
int ans[maxn],staval[maxn],maxval[maxn],cnt[maxn];
bool vis[maxn];
void rsearch(int c)
{
if(c>=k)
{
if(msum<maxval[c-1])
{
msum=maxval[c-1];
memcpy(ans,staval,sizeof(staval));
}
return ;
}
int tem[maxn];
memcpy(tem,cnt,sizeof(cnt));
for(int i=staval[c-1]+1;i<=maxval[c-1]+1;++i)
{
staval[c]=i;
for(int j=0;j<staval[c-1]*h;++j)//这里动态规划思想
{
if(cnt[j]<h)
{
for(int l=1;l<=h-cnt[j];++l)
{
if(cnt[j]+l<cnt[j+l*i]&&j+l*i<maxn)
cnt[j+l*i]=cnt[j]+l;
}
}
}
int r=maxval[c-1];
while(cnt[r+1]<inf)
r++;
maxval[c]=r;
rsearch(c+1);
memcpy(cnt,tem,sizeof(tem));
}
}
int main()
{
while(scanf("%d%d",&h,&k),h+k)
{
staval[0]=1;
maxval[0]=h;
msum=-1;
int i;
for(i=0;i<=h;++i)
cnt[i]=i;
while(i<maxn)
cnt[i++]=inf;
rsearch(1);
for(int i=0;i<k;++i)
printf("%3d",ans[i]);
printf(" ->%3d\n",msum);
}
return 0;
}

﻿﻿

#### uva 165 Stamps （DFS）

2013-08-04 23:53:58

#### UVa 242 - Stamps and Envelope Size（DP）

2015-08-29 18:10:46

#### 165 - Stamps（dfs）

2014-04-06 18:27:35

#### UVa 165 - Stamps

2016-04-22 15:12:21

#### uva 165 - Stamps

2011-11-01 11:40:36

#### uva 165 Stamps

2013-10-22 17:08:04

#### uva 165 - Stamps

2012-08-17 15:11:47

#### uva 165 - Stamps

2013-11-29 21:46:30

#### UVA - 165 Stamps 回溯

2014-11-01 18:22:24

#### uva 165 Stamps （回溯）

2015-03-03 19:33:22

## 不良信息举报

UVa 165 Stamps 【DFS】