BestCoder #49 Untitled HDU 5339
BestCoder #49 Untitled HDU 5339
题目: http://acm.hdu.edu.cn/showproblem.php?
pid=5339
本题採用深搜, 数据量小,先做排序处理(降序), 然后深搜的时候,进行剪枝,比K大的数就不是必需往后搜索,直接剪掉。
#include <iostream>
#include <algorithm>
#include <cstdio> using namespace std; const int INF = 200;
int arr[20+5] = {}; void dfs(int t, int n, int k, int &ans)
{
if (k==0)
{
ans = min(ans, t);
return;
} if (t < n)
{
if (k%arr[t] == 0)
{
ans = min(ans, t+1);
}
else if (k > arr[t]) // 要做剪枝处理, 大于k 就不是必需往后处理了
{
dfs(t+1, n, k%arr[t], ans);
dfs(t+1, n, k, ans);
}
}
} inline bool cmp(const int &a, const int &b)
{
return a > b;
} int main(void)
{
//freopen("in.txt", "r", stdin); int t = 0;
cin>>t;
while(t--)
{
int n, k;
cin>>n>>k; int zero = 0; // 假设输入数据含有 k 的因子,那么结果肯定是 1
int v = 0;
int j = 0;
for(int i=0; i<n; ++i)
{
scanf("%d", &v); if (k%v == 0)
zero = 1;
if (v < k)
arr[j++] = v;
} if (zero == 1)
{
printf("1\n");
continue;
} sort(arr, arr+j, cmp); // order by DESC
//printf("%d\n", arr[0]); int ans = INF;
dfs(0, j, k, ans);
printf("%d\n", ans==INF ? -1: ans); } return 0;
}
BestCoder #49 Untitled HDU 5339的更多相关文章
- CodeForce Round#49 untitled (Hdu 5339)
Untitled Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5339 Untitled【搜索】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5339 题意:一个整数a 和一个数组b.问你能否在b中取出r个元素排列组成c数组满足a%c1%c1%-. ...
- hdu 5339 Untitled
这题很明显是签到题,可我比赛时却没做出,赤裸裸的爆零了,真悲剧…… 看了题解后才知道直接暴搜就行,只是需要把它们从大到小排序后再搜,我当时就没想到...不想再多说了 一开始我直接枚举所有情况: #in ...
- HDU 5339 Untitled (暴力枚举)
题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...
- BestCoder#49
Untitled Accepts: 504 Submissions: 1542 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/6 ...
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...
- [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...
随机推荐
- mysql多字段组合删除重复行
DELETEFROM boll_paramWHERE id in ( SELECT a.id FROM ( SELECT id FROM boll_param WHERE (symbol, time_ ...
- style对象的cssText方法
cssText 本质是什么? cssText 的本质就是设置 HTML 元素的 style 属性值. cssText 怎么用? domElement.style.cssText = "col ...
- LeetCode03--无重复字符的最长子串
''' 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "ab ...
- XHR2:js异步上传
http://dev.opera.com/articles/xhr2/ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...
- sprintboot + mybaits + mysql + html5 + thymeleaf 个人笔记
参考:https://github.com/daleiwang/moxi service @Mapper 与 @Select 等 @Mapper似乎是一个myBaits 注解,表示将java方法和sq ...
- 使用cURL和用户名和密码?
问题描述 我想访问一个需要用户名/密码的URL.我想尝试用 curl 来访问它.现在我正在做一些事情: curl http://api.somesite.com/test/blah?something ...
- hdu 1232水
#include<stdio.h> #define N 1000 int pre[N]; int find(int n ){ return pre[n]=n==pre[n]?n:find( ...
- The Grove(poj 3182)
题意:一个n*m(n,m<=50)的矩阵有一片连着的树林,Bessie要从起始位置出发绕林子一圈再回来,每次只能向横着.竖着或斜着走一步.问最少需多少步才能完成. /* 如果我们用搜索来写的话, ...
- hdu4405:Aeroplane chess
题目大意:有编号为0-n的格子,从0开始,扔骰子扔到几就走几格.有m个瞬移点,每个点可以从格x直接飞到格y,若瞬移到另一个瞬移点可以继续瞬移.求到达格n的期望扔骰子次数. 题解:期望DP入门好题.网上 ...
- 给Ubuntu更换成163的源(sources.list)Unable to locate package
Refer to http://www.crifan.com/ubuntu_change_sources_list_to_163/ 1. backup /etc/apt/sources.list 2. ...