HDU 5339
题意:在 b1,…,bn利任选序列 c1,…,cr使得a mod c1 mod c2 mod… mod cr=0 (即a能被分解为k1*c1 + k2*c2 +...+ kr*cr),求满足条件的序列的最小长度,不满足条件输出-1
思路:先将序列b排序,然后从大到小(小数可能是大数的因子)dfs即可。
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>
#define debug(x) cout << "--------------> " << x << endl
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-10;
const long long INF = 0x7fffffff;
const long long MOD = 1000000007;
const int MAXN = 20 + 7;
int b[MAXN], ans, n, a;
void dfs(int pos, int x, int r)
{
if(x == 0) ans = ans < r ? ans : r;
if(pos == -1) return ;
dfs(pos - 1, x, r);
dfs(pos - 1, x % b[pos], r + 1);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
memset(b, 0, sizeof(b));
scanf("%d%d", &n, &a);
for(int i = 0; i < n; ++i)
{
scanf("%d", &b[i]);
}
ans = 30;
sort(b, b+n);
dfs(n-1, a, 0);
ans < 30 ? printf("%d\n", ans) : printf("-1\n");
}
return 0;
}