a.Baby Coins

a: Baby Coins

Time Limit: 1 Sec  Memory Limit: 128 MB

Description

Baby 今天清点自己的百宝箱啦,箱子里有 n 种硬币,硬币的面值分别是:val[1],val[2],...,val[n],每种面值的硬币都恰好有 2 个。Baby 实在闲的太无聊了,他想从他所拥有的硬币中选出若干个,使得面值之和为 k。那么他的目标能否实现呢 ~

Input


每一组数据第一行都包含两个数字 n(n≤18),k(1≤k≤1e9)。n 代表箱子中所包含的硬币种数,k 代表 Baby 需要组成的金钱数额。接下来的一行代表 val[1],val[2],......,val[n]。(1≤val[i]≤ 1e7)

Output

如果Baby能组成金钱数额k,请输出Yes,否则输出No。

Sample Input

2
2 10
3 4
3 9
1 2 10

Sample Output

Case 1: Yes
Case 2: No


折半枚举,先假设每种硬币只能选一次,枚举出所有可能性的和存到一个数组里(O(n*2^n)),然后把数组排序二分查找,比如一种可能性是a[i],那我们只要看k-a[i]是不是也在这个数组里就行了。
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 #define rd(a) scanf("%d",&a)
 5 #define rep(i,a,b) for(int i=(a);i<=(b);i++) 
 6 int v[25],a[300000];
 7 int main(){
 8     int T;
 9     rd(T);
10     rep(tt,1,T){
11         int n,k;
12         rd(n);rd(k);
13         for(int i=0;i<n;i++)rd(v[i]);
14         int cnt=0;
15         bool flag=0;
16         for(int i=0;i<(1<<n);i++){//枚举所有可能性
17             int tot=0;
18             for(int j=0;j<n;j++){
19                 if(i&(1<<j))tot+=v[j];
20             }
21             if(tot==k)flag=1;
22             if(tot<k)a[cnt++]=tot;
23         }
24         sort(a,a+cnt);
25         for(int i=0;i<cnt;i++)
26             if(binary_search(a,a+cnt,k-a[i]))flag=1;
27         printf("Case %d: %s\n",tt,flag?"Yes":"No");
28     }
29 } 


猜你喜欢

转载自www.cnblogs.com/KafuuMegumi/p/10090646.html