1235-coin Change (IV)
|
PDF (中文版) |
Statistics |
Forum |
| Time Limit:1 second (s) |
Memory limit:32 MB |
Given n coins, values of them are A1, A2 ... an respectively, and you has to find whether you can pay K using the coins. You can use any coin at the most of the times.
Input
Input starts with an integer T (≤100), denoting the number of test cases.
Each case is starts with a line containing, 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 would 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 thought: Binary enumeration; dfs+ two points;
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 namespacestd;TentypedefLong LongLL; One intans[ -]; A intak[100000]; - intac[100000]; - intn,m; the intid1[ -]; - intid2[ -]; - voidDfsintNintMintsum) - { + if(n==m) - { +ak[n]=sum; An++; at return ; - } - inti; - for(i=0; i<=2; i++) - { -DFS (n+1, m,sum+id1[n]*i); in } - } to voidDFS1 (intNintMintsum) + { - if(n==m) the { *ac[m]=sum; $m++;Panax Notoginseng return ; - } the inti; + for(i=0; i<=2; i++) A { theDFS1 (n+1, m,sum+id2[n]*i); + } - } $ intErintNintMintask) $ { - intMid= (n+m)/2; - if(n>m) the return 0; - if(ac[mid]==ask)Wuyi { the return 1; - } Wu Else if(ac[mid]>ask) - { About returnER (n,mid-1, ask); $ } - Else returnER (mid+1, m,ask); - - } A intMainvoid) + { the inti,j,k; -scanf"%d",&k); $ ints; the intn,m; the for(s=1; s<=k; s++) the { thescanf"%d%d",&n,&m); - for(i=0; i<n; i++) in { thescanf"%d",&ans[i]); the } About intcnt1=n/2; the intcnt2=n-cnt1; the for(i=0; i<cnt1; i++) the { +id1[i]=Ans[i]; - } the for(I=cnt1; i<n; i++)Bayi { theid2[i-cnt1]=Ans[i]; the } -n=0; -m=0; theDfs0, Cnt1,0); theDFS1 (0, Cnt2,0); theSort (ac,ac+M); the intflag=0; - for(i=0; i<n; i++) the { the intask=m-Ak[i]; theFlag=er (0, M-1, ask);94 if(flag) Break; the } theprintf"Case %d:", s); the if(flag)98printf"yes\n"); About Elseprintf"no\n"); - }101 return 0;102}
1235-coin Change (IV)