【问题描述】

人生赢家老王在网上认识了一个妹纸,然后妹纸的生日到了,为了表示自己的心

意,他决定送她礼物。可是她喜爱的东西特别多,然而他的钱数有限,因此他想

知道当他花一定钱数后剩余钱数无法再购买任何一件剩余物品(每种物品他最多

买一个)时有多少种方案,两种方案不同,当且仅当两种方案中至少有一件品不

同,可是由于他忙着准备泡下一个妹纸(chi),因此麻烦聪明的你帮帮忙。

【输入格式】

输入第一行 n 和 m,n 表示妹纸喜欢的礼物数目,m 表示现有的钱数,第二行 n

个数,表示 n 个物品的价格。

【输出格式】

输出一行一个数表示方案数目,答案对 1000000007 取模。

【样例输入 1】

gift.in

6 25

8 9 8 7 16 5

【样例输出 1】

gift.out

15

【数据范围】

30%的数据: 0<=n<=100 0<=m<=500

100% 的数据:0<=n<=1000 0<=m<=1000

注意:所有物品价格均小于 m

【题解】

算法:动态规划

30%的数据:爆搜+剪枝或DP,DP是三维的…..

100%的数据:从大到小排一遍序,枚举取到了第i个物品f[j]表示这时剩余j元的方案数.

取第i个物品:f[j]+=f[j-a[i]],若i取的话,i+1…n一定要被取到,那么剩余的钱在<=m-a[i+1] ,>m-a[i]的范围内时就可以更新答案.

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#define ll long long
#define re register
#define il inline
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
const int N=1005,mod=1000000007;
bool vis[N];
int a[N],v[N][N],w[N];
il int gi()
{
re int x=0;
re short int t=1;
re char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
int main()
{
freopen("gift.in","r",stdin);
freopen("gift.out","w",stdout);
int n,m,ans=0;
n=gi();m=gi();
fp(i,1,n) a[i]=gi();
sort(a+1,a+1+n);
fp(i,1,n) w[i]=w[i-1]+a[i];
fp(i,1,n)
{
fp(k,0,a[i])
{
if(vis[k]==0) continue;
fq(j,m,a[i])
{
v[j][k]+=v[j-a[i]][k];
v[j][k]%=mod;
}
}
if(w[i-1]<=m) v[w[i-1]][a[i]]++,vis[a[i]]=1;
}
fp(i,0,m)
fp(j,0,m)
if(m-i<j) ans=(ans+v[i][j])%mod;
printf("%d\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
}

Gift的更多相关文章

  1. USACO . Greedy Gift Givers

    Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...

  2. CF# Educational Codeforces Round 3 B. The Best Gift

    B. The Best Gift time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. 快来玩“Gift大转盘”百分百赚好礼

    现在开始到今年的最后一天,你天天都可以来转100%中奖的“ Gift大转盘 ”.代金券.产品折扣.精美纪念礼,没有多余规则.全部网友都可参加,转到就是你赚到,赶快转起来吧! >>活动主页& ...

  4. Gift Hunting(分组背包)

    Gift Hunting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. Codeforces Educational Codeforces Round 3 B. The Best Gift 水题

    B. The Best Gift 题目连接: http://www.codeforces.com/contest/609/problem/B Description Emily's birthday ...

  6. 1002 GTY's birthday gift

    GTY's birthday gift                                                                       Time Limit ...

  7. [light oj 1328] A Gift from the Setter

    1328 - A Gift from the Setter   Problem setting is somewhat of a cruel task of throwing something at ...

  8. 119 - Greedy Gift Givers

     Greedy Gift Givers  The Problem This problem involves determining, for a group of gift-giving frien ...

  9. HDU 5171 GTY's birthday gift 矩阵快速幂

    GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  10. USACO Section 1.1-2 Greedy Gift Givers

    Greedy Gift Givers 贪婪的送礼者 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少. 在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那 ...

随机推荐

  1. Redis系列(七)--Sentinel哨兵模式

    在上一篇文章了解了主从复制,主从复制本身的容错性很差,一旦master挂掉,只能进行手动故障转移,很难完美的解决这个问题 而本文讲解的sentinel可以解决这个问题 Redis sentinel示意 ...

  2. 03C++基本数据类型

    基本数据类型 2.2.1整型数据 短整型(short int) 有符号短整型(signed short int) 无符号短整型(unsigned short int) 一般整型(int) 有符号一般整 ...

  3. 第二节:1_C#中的委托的使用和讲解(转)

    C# 中的委托 引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容 ...

  4. [luogu3573 POI2014] RAJ-Rally (拓扑排序 权值线段树)

    传送门 Solution 在DAG中我们可以\(O(n)\)预处理\(Ds(u)\)表示从u表示以s为起点的最长路\(Dt(u)\)表示以u为终点的最长路,那么经过\((u,v)\)的最长路即为\(D ...

  5. 虚拟机下Linux网络配置

    之前配置好了linux系统,在网络这块我用的是桥接模式. 现在分享一下使用虚拟机桥接模式配置Linux网络的过程. 一.首先配置外网的本地Ip地址. 二.配置Linux 网络链接 1.打开linux网 ...

  6. 哈希表模板(Hash set)

    省选前最后的复(chui si)习(zheng zha). 上模板吧 namespace Hash_Table{ #define inf ~0U>>1 #define MaxN 10010 ...

  7. hadoop balancer 二

    1.每次迭代一个datanode会移动不超过如下两者中较小的一个的数据量 1)10G 2)能力阈值 dfs.datanode.balance.bandwidthPerSec 每次迭代时间不会超过20分 ...

  8. 53. spring boot系列合集【从零开始学Spring Boot】

    前40章节的spring boot系列已经打包成PDF在csdn进行发布了,如果有需要的可以进行下载. 下载地址:http://download.csdn.net/detail/linxinglian ...

  9. Remmarguts’ Date(poj 2449)

    求第k短路的长度,如果没有,输出-1. /* k短路模板 注意当s=t时,k++. */ #include<iostream> #include<cstdio> #includ ...

  10. MyBatis启动:MapperStatement创建

    参考:http://blog.csdn.net/ashan_li/article/details/50351080 MappedStatement说明 一个MappedStatement对象对应Map ...