HDU 5339 Untitled(暴搜)

 

                 Untitled



 

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, 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≤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≤n≤20,1≤a≤106).

2. The second line contains n integers b1,…,bn (∀1≤i≤n,1≤bi≤106).

 

Output
Print T answers in T lines.

 

Sample Input
2
2 9
2 7
2 9
6 7

 

Sample Output
2
-1

 

 
 
 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 }

 

posted on 2015-08-07 16:07    阅读(268)  评论(0编辑  收藏  举报

导航