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 }

View Code

 经过大佬指点之后发现还可以换一种思路枚举,这里我们把硬币平均分成前后两组,每一组分别枚举取0,1,2枚硬币的状态(O(n*3^(n/2))),然后在前后两组用二分找是否有和为k的就行了。

 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[20000],b[20000],sum,pow3[20]={1};
 7 bool judge(int n,int k){
 8     if(sum<k)return 0;
 9     int cnta=0,cntb=0;
10     for(int i=0;i<pow3[n/2];i++){
11         int tot=0,t=i;
12         for(int j=n/2-1;j>=0;j--){
13             while(t>=pow3[j])
14                 tot+=v[j],t-=pow3[j];
15         }
16         if(tot==k)return 1;
17         if(tot<k)a[cnta++]=tot;
18     }
19     for(int i=0;i<pow3[n-n/2];i++){
20         int tot=0,t=i;
21         for(int j=n-1;j>=n/2;j--){
22             while(t>=pow3[j-n/2])
23                 tot+=v[j],t-=pow3[j-n/2];
24         }
25         if(tot==k)return 1;
26         if(tot<k)b[cntb++]=tot;
27     }
28     sort(b,b+cntb);
29     for(int i=0;i<cnta;i++)
30         if(binary_search(b,b+cntb,k-a[i]))return 1;
31     return 0;
32 }
33 int main(){
34     rep(i,1,18)pow3[i]=pow3[i-1]*3;
35     int T;
36     rd(T);
37     rep(tt,1,T){
38         int n,k;
39         rd(n);rd(k);
40         sum=0;
41
42         for(int i=0;i<n;i++){
43             rd(v[i]);
44             sum+=v[i]*2;
45         }
46         printf("Case %d: %s\n",tt,judge(n,k)?"Yes":"No");
47     }
48 }

View Code

 

转载于:https://www.cnblogs.com/KafuuMegumi/p/10090646.html

a.Baby Coins相关推荐

  1. zstu新生赛 Problem A: Baby Coins(折半枚举+二分)

    Problem A: Baby Coins Time Limit: 1 Sec Memory Limit: 128 MB Submit: 274 Solved: 29 Description Baby ...

  2. Baby Coins

    http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4432 题解:二分折半查询.每种硬币有三种选择:选择其中一个,选择其中两个,不选择. 因此共 ...

  3. LeetCode刷题记录5——441. Arranging Coins(easy)

    LeetCode刷题记录5--441. Arranging Coins(easy) 目录 LeetCode刷题记录5--441. Arranging Coins(easy) 题目 语言 思路 后记 题 ...

  4. PAT甲级1048 Find Coins :[C++题解]哈希表、两个硬币之和为定值

    文章目录 题目分析 题目链接 题目分析 来源:acwing 题意:找两个数,和为定值. 分析:本题在各大OJ上几乎都有, 反正在Leetcode上做过.本题有两种常见的解法,一种是双指针,另一种是用哈 ...

  5. PAT甲级1068 Find More Coins (30 分):[C++题解]DP、背包问题、dp输出方案

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:m是背包容量,a1,a2,....,ana_1,a_2,....,a_na1​,a2​,....,an​是n个物品,第i个物品的体积是 ...

  6. [watevrCTF 2019]Baby RLWE

    [watevrCTF 2019]Baby RLWE 题目 Mateusz carried a huge jar of small papers with public keys written on ...

  7. [NPUCTF2020]Baby Obfuscation [HDCTF2019]MFC

    文章目录 [NPUCTF2020]Baby Obfuscation 把五个Fox分析一下 F0X1(int a, int b): 运用辗转相除法求得最大公因数(学到一个词汇:最大公约数GCD,最小公倍 ...

  8. 北邮OJ 2016 网预-Square Coins

    时间限制 1000 ms 内存限制 65536 KB 题目描述 Artoria, also known as Saber-chan, was born into a time of chaos and ...

  9. HDOJ 1398 Square Coins

    母函数"第一季",无压力AC~ View Code 1 //#include <fstream> 2 #include <iostream> 3 using ...

最新文章

  1. SFB 项目经验-09-用Lync 2013或Skype for Business 2015抢火车票
  2. MySql 中的常见问题解决方法
  3. 全球及中国一般手术器械行业投资态势与发展价值评估报告2022版
  4. flask中jinjia2模板引擎详解3
  5. JavaScript实现截留雨水问题的蛮力方法的算法(附完整源码)
  6. HashMap30连问,彻底搞懂HashMap
  7. 支配计算领域44年之后,摩尔定律的下一步该往哪里走?
  8. 蓝桥杯练习题:对给定整数基于斐波拉契数列构造字符串
  9. 数据库笔记02:查询与统计数据
  10. 软件设计师学习3——操作系统知识1
  11. Git的学习笔记(一)
  12. 存不存行李寄存平台_行李寄存柜和行李寄存平台的区别
  13. 127.0.0.1 192.168 localhost 之间的区别
  14. poi4.0升级踩坑合集(更新中)
  15. unite 2019 上海,东京,首尔,哥本哈根,悉尼 的视频/资料
  16. php mysql orm_初探PHP ORM框架Doctrine
  17. 自制动漫小姐姐图片api
  18. 08-02-19pe_xscan 增加Windows启动模式和对SuperHidden值检测和报告
  19. day05 - FusionSphere解决方案(云数据中心)
  20. 数字信号处理-3-函数的正交

热门文章

  1. git rebase和git merge使用方法详解
  2. tomcat警告:consider increasing the maximum size of the cache. After eviction approximately [9,267] KB
  3. 玩转无线路由之DD-WRT基础扫盲
  4. android手机设置固定dns,手机dns怎么设置 简单几步就搞定
  5. 网站服务安全访问(HTTPS)
  6. Unity3d 报错”IOException: Sharing violation on path *******”解决方式
  7. 春江水暖鸭先知,不破楼兰誓不还
  8. 统俗讲义之——何为统计显著性(Statistical Significance)
  9. Android和IOS mumu模拟器 正确安装与设置机型
  10. 计算机毕业论文进展情况说明,研究生学位论文进展情况 毕业论文的进度和计划安排怎么写~~请详细些~~...