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的更多相关文章

  1. CodeForce Round#49 untitled (Hdu 5339)

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

  2. hdu 5339 Untitled【搜索】

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

  3. hdu 5339 Untitled

    这题很明显是签到题,可我比赛时却没做出,赤裸裸的爆零了,真悲剧…… 看了题解后才知道直接暴搜就行,只是需要把它们从大到小排序后再搜,我当时就没想到...不想再多说了 一开始我直接枚举所有情况: #in ...

  4. HDU 5339 Untitled (暴力枚举)

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

  5. BestCoder#49

    Untitled Accepts: 504 Submissions: 1542 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/6 ...

  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 ...

  7. Bestcoder round #65 && hdu 5592 ZYB's Premutation 线段树

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...

  8. [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)

    BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...

  9. [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

    Task schedule Problem Description 有一台机器,而且给你这台机器的工作表.工作表上有n个任务,机器在ti时间运行第i个任务,1秒就可以完毕1个任务. 有m个询问,每一个 ...

随机推荐

  1. linux系统日志中出现大量systemd Starting Session ### of user root 解决

    这种情况是正常的,不算是一个问题 https://access.redhat.com/solutions/1564823 Environment Red Hat Enterprise Linux 7 ...

  2. Linux文件和目录的权限笔记

    查看文件或者目录的权限命令:ls -al # -a 表示全部文件包含隐藏文件,-l 表示列出每个文件的详细信息 比如执行 ls -al total 115 drwxr--x--- 4 root roo ...

  3. uwsgs loading shared libraries: libicui18n.so.58 异常处理

    背景 想使用 ningx + uwsgi + flask 搭建 python 应用环境 Python使用的是anaconda3(pyhton 3.6) 依赖包安装完毕,但是执行 uwsgi 的时候出现 ...

  4. 杭电 1260 Tickets

    Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this ...

  5. luogu3302 [SDOI2013]森林

    前置技能:Count on a tree 然后带上一个启发式合并 #include <algorithm> #include <iostream> #include <c ...

  6. 在mysql的操作界面中,如何清屏幕

    1.快捷键:Ctrl+L2.通过执行SHELL命令: \! clear实际上 \! 用来执行操作系统的shell命令,不仅是clear,其他命令也可以.shell命令执行完成后,会返回mysql Us ...

  7. B/S 开发和 C/S开发的区别

    导读:每天都从应用中心下载很多软件安装尝试,在自己的电脑上也装了很多软件,但是,就出现了一个问题,好比QQ,为什么有了APP,还要有网站应用呢?由此,结合到自己的学习,就衍生出一个问题:C/S 开发就 ...

  8. C# easyui json类

    using System; using System.Data; using System.Text; namespace Common { public class JsonHelp { priva ...

  9. Java 实体-实体的映射框架

    一.Object mapping 的技术分类: 运行期 反射调用set/get 或者是直接对成员变量赋值 . 该方式通过invoke执行赋值 *,实现时一般会采用beanutil, Javassist ...

  10. CodeForces - 43B Letter

    字符串的处理 统计已有字符的个数 和需求字符比较 #include <iostream> #include <stdio.h> #include <string.h> ...