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相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
随机推荐
- macaca 环境搭建篇,(web 和安卓)
appium研究一段时间,感觉appium太不稳定了, 后来听说了阿里开源了macaca,那么我就想尝尝鲜,啥都不说,我感觉还是赶紧上手搭建环境吧. macaca介绍: Macaca是一套完整的自动化 ...
- c# typeof 与 GetType 作用与区别
官方解释: Used to obtain the "System.Type" object for a type. A 'typeof' expression takes the ...
- JSON、数组、时间戳
// on removeClass addClass slice $('.pic-subgroup').on('click',function(){ $('.pic_div.active').remo ...
- js获取地址栏参数数据
// 获取指定地址栏数据//name:参数名 function GetQueryString(name, url) { var reg = new RegExp("(^|&)&quo ...
- jsp---jstl配置
关于eclipse中jstl标准标签库的配置问题 我的eclipse的版本是:Version: Neon.3 Release (4.6.3) 用的1.8.0_121的jre,Tomcat用的9.0, ...
- CCNP-3.vlan间路由及三层交换机的配置
- 团队作业八—第二次团队冲刺(Beta版本) 第 1 天
一.每个人的工作 (1) 昨天已完成的工作 由于是才刚开始冲刺,所以没有昨天的工作 (2) 今天计划完成的工作: 对界面的优化和一些细节的完善 (3) 工作中遇到的困难: 工作中出现了意见不一的情况 ...
- 关于在git添加远程地址的过程中遇到的问题
问题产生的过程 我根据菜鸟教程的步骤,做了如下操作: 1.打开安装文件夹中的git-bash程序 2.设置username和email 3.添加远程地址 结果如下: 之后通过百度知道要先git ini ...
- 201521123029《Java程序设计》第四周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 答:1. 多态性,多态性是相同的形态,不同的行为(定义),其中父类类型变量可以引用子类对象. ...
- 201521123065 《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 Q1:代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...