1235 - Coin Change (IV)
Time Limit: 1 second(s) Memory Limit: 32 MB

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

Output for Sample Input

3

2 5

1 2

2 10

1 2

3 10

1 3 5

Case 1: Yes

Case 2: No

Case 3: Yes


PROBLEM SETTER: JANE ALAM JAN
思路:折半枚举;
dfs+二分;
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stdlib.h>
7 #include<math.h>
8 #include<stack>
9 using namespace std;
10 typedef long long LL;
11 int ans[100];
12 int ak[100000];
13 int ac[100000];
14 int N,M;
15 int id1[100];
16 int id2[100];
17 void dfs(int n,int m,int sum)
18 {
19 if(n==m)
20 {
21 ak[N]=sum;
22 N++;
23 return ;
24 }
25 int i;
26 for(i=0; i<=2; i++)
27 {
28 dfs(n+1,m,sum+id1[n]*i);
29 }
30 }
31 void dfs1(int n,int m,int sum)
32 {
33 if(n==m)
34 {
35 ac[M]=sum;
36 M++;
37 return ;
38 }
39 int i;
40 for(i=0; i<=2; i++)
41 {
42 dfs1(n+1,m,sum+id2[n]*i);
43 }
44 }
45 int er(int n,int m,int ask)
46 {
47 int mid=(n+m)/2;
48 if(n>m)
49 return 0;
50 if(ac[mid]==ask)
51 {
52 return 1;
53 }
54 else if(ac[mid]>ask)
55 {
56 return er(n,mid-1,ask);
57 }
58 else return er(mid+1,m,ask);
59
60 }
61 int main(void)
62 {
63 int i,j,k;
64 scanf("%d",&k);
65 int s;
66 int n,m;
67 for(s=1; s<=k; s++)
68 {
69 scanf("%d %d",&n,&m);
70 for(i=0; i<n; i++)
71 {
72 scanf("%d",&ans[i]);
73 }
74 int cnt1=n/2;
75 int cnt2=n-cnt1;
76 for(i=0; i<cnt1; i++)
77 {
78 id1[i]=ans[i];
79 }
80 for(i=cnt1; i<n; i++)
81 {
82 id2[i-cnt1]=ans[i];
83 }
84 N=0;
85 M=0;
86 dfs(0,cnt1,0);
87 dfs1(0,cnt2,0);
88 sort(ac,ac+M);
89 int flag=0;
90 for(i=0; i<N; i++)
91 {
92 int ask=m-ak[i];
93 flag=er(0,M-1,ask);
94 if(flag)break;
95 }
96 printf("Case %d: ",s);
97 if(flag)
98 printf("Yes\n");
99 else printf("No\n");
100 }
101 return 0;
102 }

1235 - Coin Change (IV)的更多相关文章

  1. LightOJ 1235 - Coin Change (IV) (折半枚举)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1235 题目描述: 给出n个硬币,每种硬币最多使用两次,问能否组成K面值? 解 ...

  2. Lightoj 1235 - Coin Change (IV) 【二分】

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...

  3. Coin Change (IV) (dfs)

    Coin Change (IV) Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Subm ...

  4. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  5. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. HDU 2069 Coin Change

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

  8. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

  9. C - Coin Change (III)(多重背包 二进制优化)

    C - Coin Change (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

随机推荐

  1. java的缓冲流及使用Properties集合存取数据(遍历,store,load)

    缓冲流 概述 字节缓冲流:BufferedInputStream,BufferedOutputStream 字符缓冲流:BufferedReader,BufferedWriter 缓冲流原理 缓冲区是 ...

  2. 巩固java第六天

    巩固内容: HTML 空元素 没有内容的 HTML 元素被称为空元素.空元素是在开始标签中关闭的. <br> 就是没有关闭标签的空元素(<br> 标签定义换行). 在 XHTM ...

  3. .Net 下高性能分表分库组件-连接模式原理

    ShardingCore ShardingCore 一款ef-core下高性能.轻量级针对分表分库读写分离的解决方案,具有零依赖.零学习成本.零业务代码入侵. Github Source Code 助 ...

  4. addict, address, adequate.四级

    addict addiction – a biopsychosocial [生物社会心理学的 bio-psycho-social] disorder characterized by persiste ...

  5. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs

    [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...

  6. SpringBoot-RestTemplate测试Controller

    1.功能测试类 package com.imooc.controller; import java.io.IOException; import java.math.BigDecimal; impor ...

  7. jenkins之代码部署回滚脚本

    #!/bin/bash DATE=`date +%Y-%m-%d_%H-%M-%S` METHOD=$1 BRANCH=$2 GROUP_LIST=$3 function IP_list(){ if ...

  8. canal整合springboot实现mysql数据实时同步到redis

    业务场景: 项目里需要频繁的查询mysql导致mysql的压力太大,此时考虑从内存型数据库redis里查询,但是管理平台里会较为频繁的修改增加mysql里的数据 问题来了: 如何才能保证mysql的数 ...

  9. java关键字volatile内存语义详细分析

    volatile变量自身具有下列特性. 1.可见性.对一个volatile变量的读,总是能看到(任意线程)对这个volatile变量最后的写 入. · 2.原子性:对任意单个volatile变量的读/ ...

  10. XML(可拓展标记语言)基本概念

    一.XML文档基本结构 <?xml version="1.0" encoding="utf-8"?> <students> <st ...