HDU 5339 Untitled(暴搜)

                 Untitled

Problem Description

There is an integer $a$ and $n$ integers $b_1, \ldots, b_n$. After selecting some numbers from $b_1, \ldots, b_n$ in any order, say $c_1, \ldots, c_r$, we want to make sure that $a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ mod \ c_r = 0$ (i.e., $a$ will become the remainder divided by $c_i$ each time, and at the end, we want $a$ to become $0$). Please determine the minimum value of $r$. If the goal cannot be achieved, print $-1$ instead.

Input

The first line contains one integer $T \leq 5$, which represents the number of testcases.

For each testcase, there are two lines:

1. The first line contains two integers $n$ and $a$ ($1 \leq n \leq 20, 1 \leq a \leq 10^6$).

2. The second line contains $n$ integers $b_1, \ldots, b_n$ ($\forall 1\leq i \leq n, 1 \leq b_i \leq 10^6$).

Output

Print $T$ answers in $T$ lines.

Sample Input

2

2 9

2 7

2 9

6 7

Sample Output

2

-1

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

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4
 5 int ans,m,n;
 6 int b[21];
 7
 8 bool cmp(int a,int b)
 9 {
10     return a>b;
11 }
12
13 void dfs(int total,int cur,int num)
14 {
15     if(total==0)
16     {
17         ans=min(ans,num);
18         return;
19     }
20     if(cur==m)
21     return;
22     dfs(total%b[cur],cur+1,num+1);
23     dfs(total,cur+1,num);
24 }
25
26 int main()
27 {
28     //freopen("in.txt","r",stdin);
29     int i,t;
30     scanf("%d",&t);
31     while(t--)
32     {
33         ans=21;
34         scanf("%d%d",&m,&n);
35         for(i=0;i<m;i++)
36         scanf("%d",&b[i]);
37         sort(b,b+m,cmp);
38         dfs(n,0,0);
39         if(ans==21)
40         printf("-1\n");
41         else
42         printf("%d\n",ans);
43     }
44     return 0;
45 }
时间: 2024-12-29 11:49:28

HDU 5339 Untitled(暴搜)的相关文章

hdu 5339 Untitled(回溯)

hdu 5339 Untitled 题目大意:给出n个数字的序列,和一个数a,在n中有m个数b1,...,bm使得__a %b1%b2%...%bm = 0,求最小的m. 解题思路:回溯. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int N = 5

HDU 1045 DFS暴搜

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6229    Accepted Submission(s): 3506 Problem Description Suppose that we have a square city with straight streets. A map of a city is a s

hdu 5339 Untitled【搜索】

题目链接: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

HDU 5012 bfs暴搜

Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 243    Accepted Submission(s): 135 Problem Description There are 2 special dices on the table. On each face of the dice, a distinct number wa

HDU 5339 Untitled (状态压缩枚举)

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 570    Accepted Submission(s): 291 Problem Description There is an integer a and n integers b1,-,bn. After selecting some numbers from b

hdu 5339 Untitled

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 166    Accepted Submission(s): 83 Problem Description There is an integer a and n integers b1,-,bn. After selecting some numbers from b1

Hdu 5339 Untitled (数学思维)

题意:给一个数a和n个数b1,b2,...,bn. 从n个数中选择一些数重新排列成c1,c2,...,cm使得a%c1%c2%...%cm=0. 如果能选出则输出最少需要几个数,否则输出-1. 分析: 首先很重要的一点就是 “一定是先除大的数再除小的数” 因为如果先除小的数x,则取模的数一定是在0到x-1中,再除大于x的数是没意义的. 即选出的一列数是非递增序列. 由于n最多只有20个,简单的2^n的搜索即可搞定了. 通过这题发现hdu oj的一个bug 就是排序的时候不能手写整数的cmp使其逆

HDU 5339 Untitled (递归穷举)

题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也可以不选,两种情况.递归穷举每个可能性,O(2n). 1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <al

hdu 5339 Untitled dfs

Problem Description There is an integer a and n integers b1,…,bn. After selecting some numbers from b1,…,bn in any order, say c1,…,cr, we want to make sure that a mod c1 mod c2 mod… mod cr=0 (i.e., a will become the remainder divided by ci each time,