1 /* 2 DFS:从大到小取模,因为对比自己大的数取模没意义,可以剪枝。但是我从小到大也过了,可能没啥大数据 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-1 18:57:52 7 File Name :A.cpp 8 *************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 #include <iostream> 13 #include <sstream> 14 #include <cstring> 15 #include <cmath> 16 #include <string> 17 #include <vector> 18 #include <queue> 19 #include <deque> 20 #include <stack> 21 #include <list> 22 #include <map> 23 #include <set> 24 #include <bitset> 25 #include <cstdlib> 26 #include <ctime> 27 using namespace std; 28 29 typedef long long ll; 30 const int MAXN = 22; 31 const int INF = 0x3f3f3f3f; 32 const int MOD = 1e9 + 7; 33 int a[MAXN]; 34 bool vis[MAXN]; 35 int n, ans; 36 37 bool cmp(int x, int y) { 38 return x > y; 39 } 40 41 void DFS(int now, int step) { 42 for (int i=1; i<=n; ++i) { 43 if (vis[i] || now < a[i]) continue; 44 if (now % a[i] == 0) { 45 ans = min (ans, step + 1); return ; 46 } 47 vis[i] = true; DFS (now % a[i], step + 1); vis[i] = false; 48 } 49 } 50 51 int main(void) { //HDOJ 5339 Untitled 52 int T; scanf ("%d", &T); 53 while (T--) { 54 int x; scanf ("%d%d", &n, &x); 55 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); 56 sort (a+1, a+1+n, cmp); 57 if (x < a[n]) { 58 puts ("-1"); continue; 59 } 60 memset (vis, false, sizeof (vis)); 61 ans = INF; DFS (x, 0); 62 if (ans == INF) puts ("-1"); 63 else printf ("%d\n", ans); 64 } 65 66 return 0; 67 }
时间: 2024-12-14 09:52:02