这题很明显是签到题,可我比赛时却没做出,赤裸裸的爆零了,真悲剧……

  看了题解后才知道直接暴搜就行,只是需要把它们从大到小排序后再搜,我当时就没想到。。。不想再多说了

一开始我直接枚举所有情况:

 #include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std; int b[],a; inline bool ok(const vector<int> &v, int a) {
int len = v.size();
for(int i = len - ; i >= ; --i)
if(a % v[i] == ) return ;
else a %= v[i];
return ;
} int main() {
int t,n;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&a);
for(int i = ; i < n; ++i)
scanf("%d",b + i);
sort(b, b + n);
int limit = ( << n) - ;
bool flag = ;
for(int i = ; i <= limit; ++i) {
vector<int> v;
for(int j = ; ( << j) <= i; ++j)
if(i & ( << j)) v.push_back(b[j]);
if(ok(v, a)) {
printf("%d\n",v.size());
flag = ;
break;
}
}
if(flag == ) puts("-1");
}
return ;
}

  可是却发现 700+ms,出奇的慢,于是我试下直接的深搜,竟然 78ms,果然够快!

 #include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int inf = 0x3fffffff; int b[],ans,n; void dfs(int id, int num, int a) {
if(a == ) {
ans = min(ans, num);
return ;
}
if(id > n) return ; dfs(id + , num + , a % b[id]);
dfs(id + , num, a);
} inline bool cmp(int x, int y) {
return x > y;
} int main() {
int t, a;
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&a);
for(int i = ; i <= n; ++i)
scanf("%d",b + i);
sort(b + , b + n + , cmp);
ans = inf;
dfs(, , a);
printf("%d\n",ans == inf ? -: ans);
}
return ;
}

  在网上看到有个位压的比我第一个版本快多了:

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = << ;
int const INF = 0x3fffffff;
int b[], sta[MAX]; int lowbit(int x)
{
return x & (-x);
} bool cmp(int a, int b)
{
return a > b;
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
memset(sta, , sizeof(sta));
int n, a;
scanf("%d %d", &n, &a);
for(int i = ; i <= n; i++)
scanf("%d", &b[i]);
sort(b + , b + n + , cmp);
for(int i = ; i <= n; i++)
sta[ << (i - )] = b[i];
int cnt, ans = INF;
for(int i = ; i < ( << n); i++)
{
int tmp = a;
cnt = ;
for(int j = i; j > ; j -= lowbit(j))
{
tmp %= sta[lowbit(j)];
cnt ++;
}
if(tmp == )
ans = min(ans, cnt);
}
if(ans == INF)
printf("-1\n");
else
printf("%d\n", ans);
}
}

  虽然爆零,但比赛还是得坚持打。

hdu 5339 Untitled的更多相关文章

  1. hdu 5339 Untitled【搜索】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5339 题意:一个整数a 和一个数组b.问你能否在b中取出r个元素排列组成c数组满足a%c1%c1%-. ...

  2. HDU 5339 Untitled (暴力枚举)

    题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...

  3. BestCoder #49 Untitled HDU 5339

    BestCoder #49 Untitled  HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5339 本题採用深搜, 数据量小,先做 ...

  4. CodeForce Round#49 untitled (Hdu 5339)

    Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. DFS BestCoder Round #49 ($) 1001 Untitled

    题目传送门 /* DFS:从大到小取模,因为对比自己大的数取模没意义,可以剪枝.但是我从小到大也过了,可能没啥大数据 */ /************************************* ...

  6. HDU 2157 How many ways?? 【矩阵经典8】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2157 How many ways?? Time Limit: 2000/1000 MS (Java/Ot ...

  7. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  9. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

随机推荐

  1. 最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind

    最小支撑树树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind 最小支撑树树 前几节中介绍的算法都是针对无权图的,本节将介绍带权图的最小 ...

  2. Find The Multiple 分类: 搜索 POJ 2015-08-09 15:19 3人阅读 评论(0) 收藏

    Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21851 Accepted: 8984 Sp ...

  3. 在opencv中实现中文输出

    http://pan.baidu.com/s/1hrQTWDe 已经成功 ; 来自为知笔记(Wiz)

  4. view class source code with JAD plugin in Eclipse

    The default class viewer doesn't decompile the class file so you cannot open and check the source co ...

  5. TSP问题

    之前写过一道类似的题目,Uva 1347. http://www.cnblogs.com/TreeDream/p/5981535.html 这个题目和TSP问题已经很接近了,只是描述的奇奇怪怪的,从最 ...

  6. 2016年11月15日 星期二 --出埃及记 Exodus 20:6

    2016年11月15日 星期二 --出埃及记 Exodus 20:6 but showing love to a thousand of those who love me and keep my c ...

  7. linux中的shell脚本编程

    [1]shell脚本 1--- shell命令 2--- 控制语句(新的语法) (Shell命令的有序集合) [2]创建shell脚本文件 1--- 1.sh 2--- chmod 777 1.sh ...

  8. mfc学生成绩录入与查询

    1.声明结构体 struct Person{ char name[8]; char yuwen[8]; char math[8];}; 2.成绩录入 在"保存"按钮中实现以下代码 ...

  9. java初始化笔记

    构造器初始化static块与非static块区别:对象创建时都做块的初始化,非static块仅在实例创建时初始化,static块在类被第一次加载时初始化数组初始化注意点:1.数组定义时最好在定义的同时 ...

  10. sql 自增字段的控制 hibernate注解的写法

    1 创建序列,新建了一个列作为主键 create sequence pbaseinfo INCREMENT BY 1 START WITH 1 NOMAXVALUE NOCYCLE NOCACHE; ...