Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

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)的更多相关文章

  1. Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves)

    Leetcode之深度优先搜索(DFS)专题-1020. 飞地的数量(Number of Enclaves) 深度优先搜索的解题详细介绍,点击 给出一个二维数组 A,每个单元格为 0(代表海)或 1( ...

  2. 1235 - Coin Change (IV)

    1235 - Coin Change (IV)    PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 M ...

  3. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  4. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  5. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  6. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  7. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  8. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  9. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

随机推荐

  1. Rxjava2视频笔记

    观察者模式 观察者模式是一对多的模式,多个观察者同时监听一个主题,当主题更新时,会同时通知所有的观察者 subject –>observer 学习新技术的步骤 1.官方文档的查阅和学习 2.Go ...

  2. Java的类型转换

    Java的类型转换 在适当的时候,我们会想要将一种数据类型自动转换成另一种,比如把int转化成float类型.Java有隐藏式的自动转换,可以自动转换成想要的类型,但是强制的自动转换的话,.需要将希望 ...

  3. [2014-12-29]使用Enum位模式进行多重状态(或权限)管理

    前言 对于Enum在AspNet Mvc中的应用,我之前提到一种扩展,如何在 Asp.net Mvc 开发过程中更好的使用Enum.这里将介绍另一种更好的使用Enum的方法. Enum定义 以一个代表 ...

  4. JSP页面中<%!%>与<%%>与<%=%>

    首先,我们要了解jsp运行原理.JSP的本质就是一个Servlet,JSP的运行之前会先被Tomcat服务器翻译为.java文件,然后在将.java文本编译 为.class文件,而我们在访问jsp时, ...

  5. .net基础第一天

    .net基础 <head>+控制类标签 +<head> 1  <body>+网页要显示的内容+</body> <body +空格+bgcolor= ...

  6. windows.onload和 document.ready区别

    在Jquery里面,我们可以看到两种写法:$(function(){}) 和$(document).ready(function(){}) 这两个方法的效果都是一样的,都是在dom文档树加载完之后执行 ...

  7. java Properties类得到当前的系统属性

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt218 package com.shi.simpleUtil; import  ...

  8. 遇到的面试题-sql

    sql面试题(学生表_课程表_成绩表_教师表) 原帖链接:http://bbs.csdn.net/topics/280002741 表架构 Student(S#,Sname,Sage,Ssex) 学生 ...

  9. Bootstrap框架的了解和使用(一)

      前  言 Bootstrap 什么是 Bootstrap?Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JavaScrip ...

  10. CentOS7中将home迁移到/下的命令 CentOS7中将home迁移到/下的命令

    # mkdir -p /backup # cp -r /home/* /backup # umount /home #  df -hl # fdisk -l # lvremove /dev/cento ...