Coin Change (IV) (dfs)
| Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %lld & %llu |
Description
Given n coins, values of them are A1, A2 ... An respectively, you have to find whether you can pay K using the coins. You can use any coin at most two times.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with a line containing two integers n (1 ≤ n ≤ 18) and K (1 ≤ K ≤ 109). The next line contains n distinct integers denoting the values of the coins. These values will lie in the range [1, 107].
Output
For each case, print the case number and 'Yes' if you can pay K using the coins, or 'No' if it's not possible.
Sample Input
3
2 5
1 2
2 10
1 2
3 10
1 3 5
Sample Output
Case 1: Yes
Case 2: No
Case 3: Yes
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int a[];
bool dfs(int n,int re,int sh)
{
if(re==||sh==re)return true;
if(n==||re<||re>sh)return false;
if(dfs(n-,re,sh-*a[n]))return true;
if(dfs(n-,re-a[n],sh-*a[n]))return true;
if(dfs(n-,re-*a[n],sh-*a[n]))return true;
return false;
}
int main()
{
int t,n,k,i,j,sum;
scanf("%d",&t);
for(i=; i<=t; i++)
{
sum=;
scanf("%d%d",&n,&k);
for(j=; j<=n; j++)scanf("%d",&a[j]);
sort(a+,a+n+);
for(j=; j<=n; j++)sum+=a[j]<<;
cout<<"Case "<<i<<": ";
if(dfs(n,k,sum))
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
Coin Change (IV) (dfs)的更多相关文章
- Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves)
Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves) 深度优先搜索的解题详细介绍,点击 给出一个二维数组 A,每个单元格为 0(代表海)或 1( ...
- 1235 - Coin Change (IV)
1235 - Coin Change (IV) PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 M ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的深度优先搜索遍历(DFS)
关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
随机推荐
- java继承和多态举例
public class Test1 { public static void main(String[] args) { System.out.println(new Dog().name);//狗 ...
- Redis订阅和发布模式和Redis事务
-------------------Redis订阅和发布模式------------------- 1.概念 Redis 发布订阅(pub/sub)是一种消息通信模式: 发送者(pu ...
- Android事件传递机制详解及最新源码分析——Activity篇
版权声明:本文出自汪磊的博客,转载请务必注明出处. 在前两篇我们共同探讨了事件传递机制<View篇>与<ViewGroup篇>,我们知道View触摸事件是ViewGroup传递 ...
- 使用 PUTTY 操作 Google Cloud
目的: 使用putty连接Google Cloud 实例. 总说: 首先要用 PuTTYgen生成 private key 和 public key, 之后 登录Google Cloud 将生成的 ...
- Cookie中的HttpOnly详解
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt377 1.什么是HttpOnly? 如果您在cookie中设置了HttpOn ...
- html标签缺省(自带)样式大全
html标签默认样式整理 作者:佚名 来源:互联网 时间:07-30 16:54:48 文为大家整理了html标签默认样式属性及浏览器默认样式等等,喜欢css布局的朋友们可以学下,希望对大家有所帮助 ...
- 【Beta】Daily Scrum Meeting——Day7
站立式会议照片 1.本次会议为第七次Meeting会议: 2.本次会议在中午12点,在陆大楼召开,本次会议为30分钟讨论今天要完成的任务以及接下来的任务安排. 燃尽图 每个人的工作分配 成 员 昨天已 ...
- 团队作业4——第一次项目冲刺(Alpha版本) Day1
1.开站立式会议: 2.Leangoo任务分解图: 3.开会讨论的结果,任务分派 队员 今日进展 明日安排 林燕 调查产品的市场需求,学习微信开发 完善逻辑架构框架 王李焕 结合实际分析系统设计思路, ...
- Java 第七周总结
1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 我们知道ArrayList是允许重复的,有序的元素的集合,但当我们想用它来放 ...
- 201521123050 《Java程序设计》第7周学习总结
1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 代码: public boolean contains(Object o) ...