hdu 2126

背包,输出方案数!

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 505
using namespace std;

int f[maxn];
int cot[maxn];
int num[maxn];

int main()
{
    int n,m,t;
    scanf("%d",&t);
    while(t--)
    {
        int mi=1<<28;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&num[i]);
            mi=min(mi,num[i]);
        }
        if(m<mi)
        {
            puts("Sorry, you can't buy anything.");
            continue;
        }
        memset(f,0,sizeof f);
        memset(cot,0,sizeof cot);
        for(int i=0; i<=m; i++)
            cot[i]=1;
        for(int i=1; i<=n; i++)
        {
            for(int j=m; j>=num[i]; j--)
            {
                if(f[j]==f[j-num[i]]+1)
                    cot[j]+=cot[j-num[i]];
                else if(f[j]<f[j-num[i]]+1)
                {
                    cot[j]=cot[j-num[i]];
                }
                f[j]=max(f[j],f[j-num[i]]+1);
//                printf("%d %d|  ",f[j],cot[j]);
            }
//            puts("");
        }
        printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",cot[m],f[m]);
    }
    return 0;
}
View Code

 

posted @ 2014-04-07 14:23  Yours1103  阅读(160)  评论(0编辑  收藏  举报