# Untitled ( DFS 递归 )

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 cici 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 integerT≤5, which represents the number of testcases.

For each testcase, there are two lines:

1. The first line contains two integers nn and aa (1≤n≤20,1≤a≤106).

2. The second line contains nn integers b1,…,bnb1,…,bn (∀1≤i≤n,1≤bi≤106).
Output
Print TT answers in TT lines.
Sample Input
2
2 9
2 7
2 9
6 7
Sample Output
2

-1

AC代码：

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cstdlib>
using namespace std;
int n,m,ans;
int a[25];
bool dfs(int m,int cnt,int deep){
if(m==0){                 //若找到了第deep个数，即m膜完deep个数后等于零，说明这是一个可能的最优解了
ans=min(ans,deep);  //接下来这个解与前面得到的解作比较，因为题目要求的是输出找到的最少的个数
return true;          //bool类型为true 这时肯定是存在一个deep使m==0的
}
int i;
for(i=cnt;i<n;i++){     //首先要做的工作就是找到小于等于m的数，因为大于m的数没有用，模完还是等于本身，开始i
//等于cnt是因为不用重复扫，因为m模完前一个数肯定比那些数小
if(a[i]<=m)
break;
}
if(i==n)return false;  //如果扫完整个数组a都是大于m的说明不可能存在一个数x使m%x等于0了
for( ;i<n;i++)         //从刚好小于等于m的数开始对m取模
return dfs(m%a[i],cnt+1,deep+1);
}
bool cmp(int a,int b){
return a>b;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
ans=10000;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);          //先将数组从大到小排序
if(!dfs(m,0,0))printf("-1\n");
else printf("%d\n",ans);
}
return 0;
}