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. PHP 安装完成后 增加 bcmath 模块

    1. cd /usr/local/src/php-5.6.38/ext/bcmath  目录2. /usr/local/php/bin/phpize  生成configure需要的配置文件3.  ./ ...

  2. JavaScript中函数的调用

    JavaScript中函数的调用 制作人:全心全意 在JavaScript中,函数定义后并不会自动执行,要执行一个函数需要在特定的位置调用该函数,调用函数需要创建调用语句,调用语句包含函数名称和参数. ...

  3. CSS3---渲染属性

    1.计数器 CSS3计数器( CSS Counters )可以允许我们使用css对页面中的任意元素进行计数,实现类似于有序列表的功能.与有序列表相比,它的突出特性在于可以对任意元素计数,同时实现个性化 ...

  4. MySQL autocommit 和 start transaction

    autocommit 和 start transaction 都是事务相关的命令.类似MyISAM的mysql引擎就不支持. autocommit 默认是ON状态,即sql语句是自动提交的 show ...

  5. LeetCode 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. URI跟URL的区别

    关于URL和URI的区别,个人见解.    初学java,最近被一个概念搞得头晕脑胀,就是url和uri的概念和区别,网上查了一大通,发现各种回答眼花缭乱,有百科直接粘贴的,有胡说八道的,有故意绕来绕 ...

  7. python017 Python3 模块

    Python3 模块在前面的几个章节中我们脚本上是用 python 解释器来编程,如果你从 Python 解释器退出再进入,那么你定义的所有的方法和变量就都消失了.为此 Python 提供了一个办法, ...

  8. 尼姆博弈扩展形式(一): 限定每次取物的上限。NYOJ-135,难度5~~~

    取石子(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:5 http://acm.nyist.net/JudgeOnline/problem.php?pid=135 描述 小 ...

  9. hdu 4091

    #include<stdio.h> #include<math.h> __int64 gcd(__int64 a,__int64 b) {  if(b==0)   return ...

  10. “亚信科技杯”南邮第七届大学生程序设计竞赛之网络预赛 A noj 2073 FFF [ 二分图最大权匹配 || 最大费用最大流 ]

    传送门 FFF 时间限制(普通/Java) : 1000 MS/ 3000 MS          运行内存限制 : 65536 KByte总提交 : 145            测试通过 : 13 ...