题目链接: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. c#每循环100次提交一次数据,最后一次不足100次提交一次

    StringBuilder sb=new StringBuilder(); string strId=dataGridView1.Rows[dataGridView1.CurrentRow.Index ...

  2. leetcode NO.171 Excel表列序号 (python实现)

    来源 https://leetcode-cn.com/problems/excel-sheet-column-number/description/ 题目描述 给定一个Excel表格中的列名称,返回其 ...

  3. PHP简单登录退出代码

    PHP简单登录退出代码 登录页面login.html 负责收集用户填写的登录信息.  <html> <head> <title></title> < ...

  4. col-md-push-*和col-md-offset的区别

    实现方式的区别:col-md-offset-*,是利用margin-left实现的,col-md-push-*/col-md-pull-*是利用相对定位实现的. 效果的区别,col-md-offset ...

  5. 监视网络接口TCP状态信息数据有多种工具或命令。下面举例一些:

    nstat命令 nstat kernel ======= ss -s == netstat -i netstat -s ip -s link sar -n DEV 1

  6. 习题:过路费(kruskal+并查集+LCA)

    过路费  [问题描述]在某个遥远的国家里,有 n 个城市.编号为 1,2,3,…,n.这个国家的政府修 建了 m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市 T 需 要收取的过路费 ...

  7. 【bzoj4869】[Shoi2017]相逢是问候 扩展欧拉定理+并查集+树状数组

    题目描述 Informatik verbindet dich und mich. 信息将你我连结. B君希望以维护一个长度为n的数组,这个数组的下标为从1到n的正整数.一共有m个操作,可以分为两种:0 ...

  8. CLion 使用笔记(三)

    我已经在博客里面发布了好几篇 CLion 使用笔记了,没追究这是第几篇,姑且算作第三篇. 我的 CLion 是搭配了 MSYS2 和 Conan 使用的.MSYS2 提供 C++ toolchain. ...

  9. js 如何生成一个不重复的ID的函数

    在MongoDB中的ObjectID,可以理解为是一个不会重复的ID,这里有个链接http://www.jb51.net/article/101164.htm感兴趣可以去研究一下. 我今天要做的就是做 ...

  10. boost::thread编程-线程中断(转)

    原文转自 http://blog.csdn.net/anda0109/article/details/41943691 thread的成员函数interrupt()允许正在执行的线程被中断,被中断的线 ...