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 Shell产生16进制随机数
n为字符长度 openssl rand -hex n
- Ubuntu 修改时间
输入"date",显示的是: Tue Jan :: UTC 输入"date -R" 显示的是: Tue, Jan :: + 和北京时间相差了8个小时. 1.选择 ...
- [转]分布式系统为什么需要 Tracing?
分布式系统为什么需要 Tracing? 先介绍一个概念:分布式跟踪,或分布式追踪. 电商平台由数以百计的分布式服务构成,每一个请求路由过来后,会经过多个业务系统并留下足迹,并产生对各种Cach ...
- react 全局面包屑
renderBreadcrumb() { const {routes} = this.props; const items = routes.map((route, idx) => { let ...
- Meteor错误:TypeError: Meteor.userId is not a function
问题描述: 浏览器console提示错误TypeError: Meteor.userId is not a function. 原因分析: 通过查看Meteor API文档,可知该函数由包accoun ...
- 查看帮助文档的一些方法:help,dir,type,func_global等
help与dir与type:在使用python来编写代码时,会经常使用python自带函数或模块,一些不常用的函数或是模块的用途不是很清楚,这时候就需要用到help函数来查看帮助.这里要注意下,hel ...
- tomcat安装不成功-提示找不到JAVA虚拟机
今天重装tomcate,但是总是提示找不到java虚拟机,但是我明明装了jre和jdk,太烦人了 后来搜了各种方法,终于找到了解决方法,现在和大家分享下 到提示找java虚拟机那一步的时候,选择到jr ...
- [.Net MVC] 使用 log4net 日志框架
项目:后台管理平台 意义:项目开发中提出增加日志功能,对关键的操作.程序运行中的错误信息进行记录,这对程序部署后的调试有很大意义. 注:本文只是对网上搜集的信息进行了整合,以备今后查询. 关键字:.N ...
- IO流04_InputStream和Reader输入流
[输入流中的字符流和字节流] [InputStream和Reader] InputStream和Reader是所有输入流的抽象基类,本身不能实例化,但是他们是所有输入流的模板. [ InputStre ...
- 谱曲软件-MuseScore
谱曲软件-MuseScore 参考: 1.MuseScore官网 2.免费乐谱制作软体MuseScore