题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5339

题意:一个整数a 和一个数组b。问你能否在b中取出r个元素排列组成c数组满足a%c1%c1%…..%cr == 0。输出最小的r,不能满足条件输出-1。

思路:b按从大到小排序,暴搜。

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <assert.h> using namespace std; int n, a, b[300],ans; bool cmp11(int a, int b)
{
return a > b;
} void dfs(int x,int num,int A)
{
if (x == 0)
{
ans = min(ans, A); return;
}
if (num == n) return; //printf(" %d\n",x);
dfs(x % b[num + 1], num + 1, A + 1);
dfs(x, num + 1, A);
} int main()
{
//printf("3 % 7");
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d %d", &n, &a);
for (int i = 1; i <= n; i++)
scanf("%d", &b[i]);
sort(b + 1, b + 1 + n, cmp11);
ans = 500;
dfs(a, 0, 0);
if (ans == 500) ans = -1;
printf("%d\n", ans);
}
return 0;
}

hdu 5339 Untitled【搜索】的更多相关文章

  1. hdu 5339 Untitled

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

  2. HDU 5339 Untitled (暴力枚举)

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

  3. BestCoder #49 Untitled HDU 5339

    BestCoder #49 Untitled  HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5339 本题採用深搜, 数据量小,先做 ...

  4. CodeForce Round#49 untitled (Hdu 5339)

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

  5. hdu 5468(莫比乌斯+搜索)

    hdu 5468 Puzzled Elena   /*快速通道*/ Sample Input 5 1 2 1 3 2 4 2 5 6 2 3 4 5   Sample Output Case #1: ...

  6. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  7. HDU 1045 (DFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1045 题目大意:在不是X的地方放O,所有O在没有隔板情况下不能对视(横行和数列),问最多可以放多少个 ...

  8. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  9. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

随机推荐

  1. easyui可封装的各种方法

    组件:datagrid 获取选中行(单行) var row = $('#tt').datagrid('getSelected');    if (row){        alert('Item ID ...

  2. php 备份数据库代码(生成word,excel,json,xml,sql)

    单表备份代码: 复制代码代码如下: <?php class Db { var $conn; function Db($host="localhost",$user=" ...

  3. hdu6134[莫比乌斯反演] 2017多校8

    /*hdu6134[莫比乌斯反演] 2017多校8*/ #include <bits/stdc++.h> using namespace std; typedef long long LL ...

  4. SQL死锁

    我们操作数据库大量数据时,可能会出现死锁现象. 所谓死锁: 是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统 ...

  5. 为MYSQL加注释--mysql注释符

    上午插入记录的时候一直没有成功,郁闷不知道为什么.因为是很多条记录一起插入,中间一些不用的数据就用"--"来注释了,结果没有效果. 没有办法,在网上找了找,才发现注释符" ...

  6. JavaScript 笔记(7) -- 在HTML中嵌入 js (外部引用)

    本节主要说明,在HTML中嵌入自定义 JavaScript.通过HTML的script标签加载JavaScript文件 为防止网页加载缓慢,也可以把非关键的JavaScript放到网页底部,例如下面的 ...

  7. 【06】react 之 PropsType

    React组件创建的时候,需要传入属性,我们可以使用使用PropTypes进行类型检查,您可以使用React.PropTypes在组件的道具上运行. React.PropTypes.array Rea ...

  8. AssetDatabase.RenameAsset 重命名文件失败

    今天想写一段Unity Editor 的代码将在 Project Panel 中选中的所有 Texture 改变 Format,然后重命名 成 xxx.Dither.png 然后自动进行上一篇文章提到 ...

  9. Nature Reserve

    Nature Reserve time limit per test:2 seconds memory limit per test:256 megabytes input:standard inpu ...

  10. linux之创建临时文件的方法

    有时候,我们需要创建文件临时存放一些输出的信息,创建文件时就可能出现文件名存在的问题.如何创建唯一的文件名,Linux为我们提供几个方案: 1.mktemp(强烈推荐) The  mktemp  ut ...