hdu 5339 Untitled
这题很明显是签到题,可我比赛时却没做出,赤裸裸的爆零了,真悲剧……
看了题解后才知道直接暴搜就行,只是需要把它们从大到小排序后再搜,我当时就没想到。。。不想再多说了
一开始我直接枚举所有情况:
#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的更多相关文章
- hdu 5339 Untitled【搜索】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5339 题意:一个整数a 和一个数组b.问你能否在b中取出r个元素排列组成c数组满足a%c1%c1%-. ...
- HDU 5339 Untitled (暴力枚举)
题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...
- BestCoder #49 Untitled HDU 5339
BestCoder #49 Untitled HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5339 本题採用深搜, 数据量小,先做 ...
- CodeForce Round#49 untitled (Hdu 5339)
Untitled Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- DFS BestCoder Round #49 ($) 1001 Untitled
题目传送门 /* DFS:从大到小取模,因为对比自己大的数取模没意义,可以剪枝.但是我从小到大也过了,可能没啥大数据 */ /************************************* ...
- 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 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- springMVC配置freemarker
这里呢,我首先来说明一下写该篇的目的. 我最近要用到freemarker因此研究了一下这个东西. 先来说说如何配置吧. 1.jar包.地址见下链接. http://pan.baidu.com/s/1j ...
- C#常用日期格式处理转换[C#日期格式转换大全
DateTime dt = DateTime.Now; Label1.Text = dt.ToString();//2005-11-5 13:21:25 Label2.Text = dt.ToFile ...
- spring Aop中aop:advisor 与 aop:aspect的区别
转载:http://blog.csdn.net/u011710466/article/details/52888277 在spring的配置中,会用到这两个标签.那么他们的区别是什么呢? ...
- mean函数
求平均值 th> a=torch.zeros(,) [.0002s] th> a [torch.DoubleTensor of size 1x3] [.0003s] th> a[{, ...
- discuz安装与学习资料
discuz的安装地址:http://www.discuz.net/thread-3457145-1-1.html 一些学习资料:http://www.discuz.net/forum-10-1.ht ...
- (2)redis的基本数据结构是动态数组
redis的基本数据结构是动态数组 一.c语言动态数组 先看下一般的动态数组结构 struct MyData { int nLen; ]; }; 这是个广泛使用的常见技巧,常用来构成缓冲区.比起指针, ...
- vs自带iis局域网调试
http://www.cnblogs.com/liluping860122/p/4685564.html
- Struts2的标签库(四)——数据标签
Struts2的标签库(四) --数据标签 1.action标签 该标签用于在jsp页面直接调用一个Action,通过指定executeResult参数,还可以将Action的处理结果包含到此页面中来 ...
- SqlSever基础 delete 删除一个表中的所有数据
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Linux新手学堂 Crontab命令的语法
crontab 命令的用途就是:提交.编辑.列出或除去 cron 作业. 语法 crontab [ -e [UserName] | -l [UserName] | -r [UserName] | -v ...