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 }