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 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

連續郵資,數據量小回溯,數據量大dp
參考了。http://blog.csdn.net/shuangde800/article/details/7755452
               http://blog.csdn.net/jcwkyl/article/details/4137398
回溯的要點,就是用一個數組max記錄當前使用x張組合出的連續最大值爲max[x];
h張,k種面值
面值爲1的必選(初始化),stamp[1]=1; max[1]=h;
則第x張面值得取值範圍爲 stamp[x-1]+1<=stamp[X]<=max[X-1]+1;
超過之前的max[X-1]+1,則面值爲max[x-1+1],一定無法組合出來;

#include<string.h>
#include<stdio.h>
int h,k,stamp[10],max[10],ans[10],can[200],Max;
void dfs(int N,int num,int value)
{int i;
 if (N>h) return ;
 can[value]=1;
 for (i=1;i<=num;i++)
 dfs(N+1,num,value+stamp[i]);
}
void work(int num)
{
 int i,value;
 if (num>=k)
 {
  if (max[num]>Max)
    {
     for (i=1;i<=k;i++)
     ans[i]=stamp[i];
     Max=max[num];
    }
    return ;
  }
 for (i=stamp[num]+1;i<=max[num]+1;i++)
 {
  stamp[num+1]=i;
  memset(can,0,sizeof(can));
  value=0;
  dfs(0,num+1,0);//將所有當能組合出來的面值標記
  while (can[value+1]) ++value; //統計連續最大面值
  max[num+1]=value;
  work(num+1);
 }
}
int main()
{
 int i;
 stamp[1]=1;
 while (scanf("%d%d",&h,&k),h+k)
 {
  max[1]=h; Max=0;
  work(1);
  for(i=1;i<=k;i++)
    printf("%3d", ans[i]);
    printf(" ->%3d\n",Max);
 }
 return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章