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 ...
随机推荐
- Linux批量替换文件内容
问题描述:现在需要将rack1目录下*.send文件中的"-ip="替换成“-localIp=10.0.0.1/n-ip=” 刚才那个批量文本内容替换,只能替换内存中的内容,并不会 ...
- 在oc代码中使用swift第三方框架
swift现在使用越来越多了,一些主流的框架或者效果比较好的demo都陆陆续续使用swift写了.所以,要学会如何在oc的项目中调用swift. 这里主要借助一个桥梁文件,这个桥梁文件一般在你导入sw ...
- java.lang.ClassFormatError: Illegal UTF8 string in constant pool in class file Server/Request
Linux服务器上,将本地编译好的文件上传后,Tomcat启动时报错: Exception in thread "Thread-2" java.lang.ClassFormatEr ...
- activity和fragment的声明周期
Activity生命周期: Fragment生命周期:
- 关于async和await的一些误区实例详解
转载自 http://www.jb51.net/article/53399.htm 这篇文章主要介绍了关于async和await的一些误区实例详解,有助于更加深入的理解C#程序设计,需要的朋友可以参考 ...
- 第五章 jQuery中的动画
通过jQuery中的动画方法,能轻松地为网页添加精彩的视觉效果,给用户一种全新体验. 1.show()方法和hide()方法 该方法的功能与css()方法设置display属性效果相同. 给show( ...
- C#学习笔记15:字符串、文件、目录的操作方法
字符串:不可变性 String str=”abcdf”; 将字符串转换为char数组:ToCharArray(); Char[] ch=str.ToCharAarray(); 将char数组转换为字符 ...
- 20160509-hibernate-集合映射
集合映射 集合映射(set, list, array,bag, map) <set name=”employees” > <key column=”depart_id”/> & ...
- apache http server 多线程模式
一般apache采用prefork和worker机制,通过apachectl -l命令查看默认使用的prefork机制.需要修改prefork策略 那么需要做如下修改: 1,/usr/local/ap ...
- WCF编程系列(六)以编程方式配置终结点
WCF编程系列(六)以编程方式配置终结点 示例一中我们的宿主程序非常简单:只是简单的实例化了一个ServiceHost对象,然后调用open方法来启动服务.而关于终结点的配置我们都是通过配置文件来 ...