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 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
随机推荐
- css3 简单动画
<script> <!-- var x,y,n=0,ny=0,rotINT,rotYINT function rotateDIV() { x=document.getElementB ...
- Ajax案例:简易的购书网页
****使用Ajax可以不需要刷新整个页面,而使局部页面更新的技术: ****JSON是JavaScript得原生格式,JavaScript中处理JSON数据不需要特殊的工具或架包,其规则很简单,对象 ...
- php编译器
WordPress http://pan.baidu.com/s/1eQnOnv0 epp3: http://pan.baidu.com/s/1pJKFOD1 配合xampp: http://pan. ...
- CI 同时上传多个图片
最近,一直在研究ci框架,由于项目的需求,在后台需要做一个功能同时上传两张图片.测试了好久都没有两张图片都没有上传成功,(上传的结果是只能上传第二张图片,但是图片名称是第一个图片的).在这里说一下自己 ...
- ios布局约束
http://blog.csdn.net/pucker/article/details/41843511 除了使用Pin菜单之外,也可以直接在文档结构窗口中或者设计器中通过拖拽建立约束.在文档结构窗口 ...
- Objective-C协议与非正式协议
http://blog.csdn.net/siemenliu/article/details/7836499
- Java多个数字求和输出
设计思想: 首先为了能够输入数字,先引入一个类import java.util.Scanner;因为是多个数字求和,所以为了方便声明一个数组double[] num;再声明一个变量int number ...
- UVA 10564 十 Paths through the Hourglass
Paths through the Hourglass Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- CodeForces 552C Vanya and Scales
Vanya and Scales Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u S ...
- CP,SCP 命令(包括windows与linux用xshell互传)
linux之cp/scp命令+scp命令详解 名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... direct ...