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).
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
排序后dfs~
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int inf = 0x3f3f3f3f;
int n, a, b[300], ans;
bool cmp(int a, int b)
{
return a > b;
}
void dfs(int x, int num, int A)
{
if(!x) {
ans = min(ans, A);
return;
}
if(num == n) return;
dfs(x % b[num + 1], num + 1, A + 1);
dfs(x, num + 1, A);
}
int main(int argc, char const *argv[])
{
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, cmp);
ans = inf;
dfs(a, 0, 0);
if(ans == inf) printf("-1\n");
else printf("%d\n", ans);
}
return 0;
}