noj [1479] How many (01背包||DP||DFS)
- http://ac.nbutoj.com/Problem/view.xhtml?id=1479
[1479] How many
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
- There are N numbers, no repeat. All numbers is between 1 and 120, and N is no more than 60. then given a number K(1 <= K <= 100). Your task is to find out some given numbers which sum equals to K, and just tell me how many answers totally,it also means find out hwo many combinations at most.
- 输入
- There are T test cases. For each case: First line there is a number N, means there are N numbers. Second line there is a number K, means sum is K. Third line there lists N numbers. See range details in describe.
- 输出
- Output the number of combinations in total.
- 样例输入
2
5
4
1,2,3,4,5
5
6
1,2,3,4,5- 样例输出
2
3- 01背包||DP代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h> using namespace std;
int dp[]; int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
memset(dp,,sizeof(dp));
dp[]=;
int i,j;
for(i=;i<n;i++)
{
int a;
scanf("%d",&a);
getchar();
for(j=m;j>=;j--)
{
if(a+j<=m)
{
dp[a+j]+=dp[j];
}
}
// for(j=0;j<=m;j++) printf("%d ",dp[j]); putchar(10);
}
printf("%d\n",dp[m]);
}
return ;
}DFS代码:
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std; int n,m;
int a[],mark[];
int cnt; void dfs(int x,int s){
if(s==){
cnt++;
return;
}
int i;
for(i=x;i<n;i++){
if(!mark[i]&&s-a[i]>=){
mark[i]=;
dfs(i+,s-a[i]);
mark[i]=;
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
int i;
for(i=;i<n;i++){
scanf("%d",&a[i]);
getchar();
}
memset(mark,,sizeof(mark));
cnt=;
dfs(,m);
printf("%d\n",cnt);
}
return ;
}
noj [1479] How many (01背包||DP||DFS)的更多相关文章
- PAT L3-001 凑零钱(01背包dp记录路径)
韩梅梅喜欢满宇宙到处逛街.现在她逛到了一家火星店里,发现这家店有个特别的规矩:你可以用任何星球的硬币付钱,但是绝不找零,当然也不能欠债.韩梅梅手边有104枚来自各个星球的硬币,需要请你帮她盘算一下,是 ...
- 0-1背包dp|波动数列|2014年蓝桥杯A组10-fishers
标题:波动数列 观察这个数列: 1 3 0 2 -1 1 -2 ... 这个数列中后一项总是比前一项增加2或者减少3. 栋栋对这种数列很好奇,他想知道长度为 n 和为 s 而且后一项总是比前一项增加a ...
- HDU 1203 I NEED A OFFER!(01 背包DP)
点我看题目 题意 : 中文题不详述. 思路 :类似于01背包的DP,就是放与不放的问题,不过这个要求概率,至少得到一份offer的反面就是一份也得不到,所以先求一份也得不到的概率,用1减掉就可以得到所 ...
- (01背包 dp)P1049 装箱问题 洛谷
题目描述 有一个箱子容量为VV(正整数,0≤V≤20000),同时有nn个物品(0<n≤30,每个物品有一个体积(正整数). 要求nn个物品中,任取若干个装入箱内,使箱子的剩余空间为最小. 输入 ...
- HDU 2602 Bone Collector (01背包DP)
题意:给定一个体积,和一些物品的价值和体积,问你最大的价值. 析:最基础的01背包,dp[i] 表示体积 i 时最大价值. 代码如下: #pragma comment(linker, "/S ...
- hiho #1038 : 01背包 (dp)
#1038 : 01背包 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 且说上一周的故事里,小Hi和小Ho费劲心思终于拿到了茫茫多的奖券!而现在,终于到了小Ho领取奖励 ...
- Bookshelf 2(poj3628,01背包,dp递推)
题目链接:Bookshelf 2(点击进入) 题目解读: 给n头牛,给出每个牛的高度h[i],给出一个书架的高度b(所有牛的高度相加>书架高度b),现在把一些牛叠起来(每头牛只能用一次,但不同的 ...
- 01背包-dp
一 问题分析 二 代码实现 package Dp_0_1_bag; import java.io.BufferedWriter; import java.io.FileWriter; import j ...
- TZOJ 1545 Hurdles of 110m(01背包dp)
描述 In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperit ...
随机推荐
- About Webkit
http://blog.csdn.net/spacetiller/article/details/5784461 一 . WebKit 简介 Webkit 是一个开放源代码的浏览器引擎 (web br ...
- 各I/O模型 对应Web服务应用模型(select,poll,epoll,kevent,"/dev/poll")
一.利用select多路复用I/O的Web服务应用模型 /* 可读.可写.异常三种文件描述符集的申明和初始化.*/ fd_set readfds, writefds, exceptionfds; F ...
- RPM的基础命令/yum基本命令
rpm的使用方式非常简单,常用的一些命令如下: 安装软件:rpm -ivh *.rpm 卸载软件:rpm -e 软件名 升级形式安装:rpm -U *.rpm 常用参数: -v 显示详细信息 -h 显 ...
- 关于国际化中的$NON-NLS-1$
百度百科解释: 这实际与eclipse中支持i18n的一种方式,eclipse的标准结构,将所有string常量定义到·properties中,例如下面程序段中的TestRef.hello实际上是·p ...
- WCF图片上传
WCF越来越流行,俺也在用,这是废话.项目中遇到需要图片上传,但是wcf上传会遇到一些异常,调试了N久,找了好多个解决方案才最终解决.代码直接贴上了 /// <summary> /// 笔 ...
- 编码神器之sublime(插件安装)
一款优秀的编辑器是程序员的左膀右臂,相信每一个程序员手边都有自己熟悉的编辑器. 从一开始使用sublime的时候就开始喜欢上了这款编辑器,被他强大的功能深深的吸引了. sublime的强大来源于他的扩 ...
- C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是TAP(Task-based Asynchronous Pattern, 基于任务的异步模式)
学习书籍: <C#本质论> 1--C#5.0之后推荐使用TPL(Task Parallel Libray 任务并行库) 和PLINQ(Parallel LINQ, 并行Linq). 其次是 ...
- StrHelper
public class StrHelper { private static string passWord; //加密字符串 /// <summary> /// 判断输入是否数字 // ...
- JavaScript学习笔记(7)——JavaScript语法之函数
函数就是包裹在花括号中的代码块,前面使用了关键词 function: function functionname() { //这里是要执行的代码 } 当调用该函数时,会执行函数内的代码. 可以在某事件 ...
- mysql空间数据相关操作
建表语句: CREATE TABLE ts.points ( name ) NOT NULL, location POINT NOT NULL, description ) ); 添加记录如下: IN ...