【问题描述】

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

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

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

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

同,可是由于他忙着准备泡下一个妹纸(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. IDEA SpringBoot项目连接数据库报Acess denied错误解决方法

    详见:https://blog.csdn.net/qq_36324464/article/details/79534605

  2. ThinkPHP---案例2--部门管理功能

    [一]部门列表展示 分析: ①控制器DeptController.class.php ②方法showList(不要使用list方法,因为list是关键词) ③模板文件:showList.html 下面 ...

  3. Socket中BufferedReader.readLine()的阻塞特性导致的数据无法多次发送的问题

    https://blog.csdn.net/shenpibaipao/article/details/70236657

  4. Window下的———TOMCAT环境的配置

    1. 先去官方网站下载需要的猫(tomcat) http://tomcat.apache.org/ 2.下载好包,然后解压出来,放在你需要的位置上 3.去到配环境变量的地方,进行相应的环境配置     ...

  5. 常用Linux命令_20190211

    1.创建文件夹:mkdir 文件夹名称 2.查看IP地址信息:ipconfig -a 3.查看内存使用情况:free -m 4.查看CPU使用情况:top 5.查看磁盘使用情况:df -a/-h 6. ...

  6. journals in Fluid Dynamics

    annual review of fluid mechanicsjournal of fluid mechanicsphysics of fluidjournal of flow and struct ...

  7. 2.5.5 基本的 I/0 重定向

        标准输入/输出(standard I/O)可能是软件设计原则里最重要的概念了.这个概念就是:程序应该有数据的来源端.数据的目的端以及报告问题的地方,它们分别被称为标准输入(standard i ...

  8. HDU 1542 Atlantics 线段树+离散化扫描

    将 x 轴上的点进行离散化,扫描线沿着 y 轴向上扫描 每次添加一条边不断找到当前状态有效边的长度 , 根据这个长度和下一条边形成的高度差得到一块合法的矩形的面积 #include<iostre ...

  9. hdu 2255KM算法模板

    #include<stdio.h> #include<string.h> #define N  400 #define inf 0x7fffffff int Max(int a ...

  10. sheepdog简介

    1.corosync,single ring最多支持50个节点:zookeeper,500个节点可稳定支撑,1000-1500个节点挑战比较大,需要优化消息传递机制. 2.sheepdog一开始为分布 ...