uva 165 Stamps

题目地址:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=101

题目描述:

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

题意:组合邮票 组合钞票

题解:搜索,但要剪枝搜索,不然容易TLE。

面值为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],一定无法组合出来;

不过也可以打表来做,就那么几种情况。。。。

代码:

/*
uva Stamps 165


we can print all the number to a table them use the table

it's so nice that I use the print table strategy to solve the problem


stamp[x-1]+1<=stamp[X]<=max[X-1]+1 
max[X-1]+1  before x-1 kind of stamps must not make this value besides this value is min value they can not make if a var < value  they must can make it!!

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#define DMAXS 32
using namespace std;

int h=0,k=0;
int t[8+5]={0};//t[0]*x[0]+t[1]*x[1]+.....=sum
int x[8+5]={0};//the denominations values of the stamps
int Sum=0;//n(h,k)

int MaxSum=0;//max n(h,k)
int MaxX[8+5]={0};//output for the ascending order
int flagT=0;
int DMAX=20;

/*dfs the t array note the h number to pruning*/
int DFST(int cur)
{
    //static int flag=0;//global var
    if(flagT)//pruning
    {
        return(flagT);
    }
    if(cur<=k-1)
    {
        int i=0;
        for(i=0;i<=h;i++)
        {
            h-=i;
            t[cur]=i;
            DFST(cur+1);
            h+=i;
            t[cur]=0;
            if(flagT)//pruning
            {
                return(flagT);
            }
        }
    }
    else
    {
        int Adder=0;
        int i=0;
        for(i=0;i<=k-1;i++)
        {
            Adder+=t[i]*x[i];
        }
        if(Adder==Sum)
        {
            flagT=1;
        }
    }
    return(flagT);
}


/*calculate the  t1*x1+t2*x2...==sum*/
int Calc(int Sum,int x[])
{
    int flag=1;
    flagT=0;
    flag=DFST(0);//dfs the t[0]
    return(flag);
}

/*DFS the x[i]'s value*/
int DFS(int cur)
{
    if(cur<=k-1)
    {
        int i=0;
        for(i=x[cur-1];i<=DMAXS;i++)
        {
            x[cur]=i;
            DFS(cur+1);
        }
    }
    else//per series dfs complete
    {
        Sum=0;
        int flag=1;
        do
        {
            Sum++;
            flag=Calc(Sum,x);//enum the t1*x1+t2*x2...==sum
        }while(flag);
        Sum--;
        if(Sum>MaxSum)
        {
            MaxSum=Sum;
            memcpy(MaxX,x,sizeof(x));
        }
    }
    return(0);
}

/*for test*/
int test()
{
    for(DMAX=20;DMAX<=50;DMAX++)//max:  1 4 12 21 71
    {
        h=5;
        k=4;
        x[0]=1;
        MaxSum=0;
        DFS(1);
        sort(MaxX,MaxX+k);
        printf("%d %d %d %d %d\n",MaxX[0],MaxX[1],MaxX[2],MaxX[3],MaxSum);
    }
    // test the [5,4] the x[i]'s max value maybe what
    return(0);
}

/*for test*/
int test1()
{
    for(DMAX=20;DMAX<=50;DMAX++)
    {
//        h=4;
//        k=5;//1 3 11 15 32 70
//        h=3;
//        k=6;//1 3 7 9 19 24 52
//        h=6;//1 7 12 52
//        k=3;
//        h=2;//1 2 5 8 11 12 13 26
//        k=7;
//        h=1;//1 2 3 4 5 6 7 8 8
//        k=8;
        x[0]=1;
        MaxSum=0;
        DFS(1);
        sort(MaxX,MaxX+k);
        //printf("%d %d %d %d %d\n",MaxX[0],MaxX[1],MaxX[2],MaxX[3],MaxX[4],MaxSum);
        int i=0;
        for(i=0;i<=k-1;i++)
        {
            printf("%d ",MaxX[i]);
        }
        printf("%d\n",MaxSum);
    }
    // test the [5,4] the x[i]'s max value maybe what
    return(0);
}


/*
table data:
  1  3 ->  7
  1  2 ->  4
  1 ->  3
  1  3 11 18 -> 44
  1  2  3  4  5  6  7  8 ->  8
  1  2  5  8 11 12 13 -> 26
  1  3  7  9 19 24 -> 52
  1  3 11 15 32 -> 70
  1  2  5  8  9 10 -> 20
  1  2  3  4  5  6 ->  6
  1  2  3  4  5  6  7 ->  7
  1  4  6 14 15 -> 36
  1  3  5  7  8 -> 16
  1  2  3  4  5 ->  5
  1  2  3  4 ->  4
  1  3  5  6 -> 12
  1  4  7  8 -> 24

*/

/*main process*/
int MainProc()
{
    while(scanf("%d%d",&h,&k)!=EOF&&h+k>0)
    {
        
        //table
        if(k==8&&h==1)
        {
            printf("  1  2  3  4  5  6  7  8 ->  8\n");
            continue;
        }
        if(k==7&&h==2)
        {
            printf("  1  2  5  8 11 12 13 -> 26\n");
            continue;
        }
        if(k==7&&h==1)
        {
            printf("  1  2  3  4  5  6  7 ->  7\n");
            continue;
        }
        if(k==6&&h==3)
        {
            printf("  1  3  7  9 19 24 -> 52\n");
            continue;
        }
        if(k==6&&h==2)
        {
            printf("  1  2  5  8  9 10 -> 20\n");
            continue;
        }
        if(k==6&&h==1)
        {
            printf("  1  2  3  4  5  6 ->  6\n");
            continue;
        }
        if(k==5&&h==4)
        {
            printf("  1  3 11 15 32 -> 70\n");
            continue;
        }
        
        
        Sum=0;
        x[0]=1;//the length is k,t[] is too.
        MaxSum=0;
        DFS(1);
        sort(MaxX,MaxX+k);
        int i=0;
        for(i=0;i<=k-1;i++)
        {
            if(MaxX[i]>=10)
            {
                printf(" ");
            }
            else
            {
                printf("  ");
            }
            printf("%d",MaxX[i]);
        }
        printf(" ->");
        if(MaxSum>=10&&MaxSum<=99)
        {
            printf(" ");
        }
        else if(MaxSum<10)
        {
            printf("  ");
        }
        printf("%d\n",MaxSum);
    }
    return(0);
}

int main()
{
    MainProc();
    return(0);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值