Coin Change (IV) (dfs)

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

[]   [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

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <math.h>
 4 #include <string.h>
 5 #include <algorithm>
 6 using namespace std;
 7 int a[50];
 8 bool dfs(int n,int re,int sh)
 9 {
10     if(re==0||sh==re)return true;
11     if(n==0||re<0||re>sh)return false;
12     if(dfs(n-1,re,sh-2*a[n]))return true;
13     if(dfs(n-1,re-a[n],sh-2*a[n]))return true;
14     if(dfs(n-1,re-2*a[n],sh-2*a[n]))return true;
15     return false;
16 }
17 int main()
18 {
19     int t,n,k,i,j,sum;
20     scanf("%d",&t);
21     for(i=1; i<=t; i++)
22     {
23         sum=0;
24         scanf("%d%d",&n,&k);
25         for(j=1; j<=n; j++)scanf("%d",&a[j]);
26         sort(a+1,a+n+1);
27         for(j=1; j<=n; j++)sum+=a[j]<<1;
28         cout<<"Case "<<i<<": ";
29         if(dfs(n,k,sum))
30             cout<<"Yes"<<endl;
31         else cout<<"No"<<endl;
32     }
33 }
View Code

 

转载于:https://www.cnblogs.com/ERKE/p/3582222.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值