Problem Description
2 2 9 2 7 2 9 6 7
2 -1
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1 << 22;
int const INF = 0x3fffffff;
int b[25], sta[MAX];
int lowbit(int x)
{
return x & (-x);
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int T;
scanf("%d", &T);
while(T --)
{
memset(sta, 0, sizeof(sta));
int n, a;
scanf("%d %d", &n, &a);
for(int i = 1; i <= n; i++)
scanf("%d", &b[i]);
sort(b + 1, b + n + 1, cmp);
for(int i = 1; i <= n; i++)
sta[1 << (i - 1)] = b[i];
int cnt, ans = INF;
for(int i = 1; i < (1 << n); i++)
{
int tmp = a;
cnt = 0;
for(int j = i; j > 0; j -= lowbit(j))
{
tmp %= sta[lowbit(j)];
cnt ++;
}
if(tmp == 0)
ans = min(ans, cnt);
}
if(ans == INF)
printf("-1\n");
else
printf("%d\n", ans);
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/tc_to_top/article/details/47206995