Bestcoder #49 Untitled HDU 5339
Title: http://acm.hdu.edu.cn/showproblem.php?pid=5339
The subject uses deep search, small data volume, first do sort processing (descending), and then deep search, to prune, than the number of k is not necessary to search back, directly cut off.
#include <iostream> #include <algorithm> #include <cstdio>using namespace std;const int INF = 200;int Arr[20+5] = {};void dfs (int t, int n, int k, int &ans) {if (k==0) {ans = min (ans, t); Return } if (T < n) {if (k%arr[t] = = 0) {ans = min (ans, t+1); } else if (K > Arr[t])//To do pruning, greater than k does not need to be processed backwards {DFS (t+1, N, K%arr[t], ans); DFS (t+1, n, K, ans); }}}inline BOOL CMP (const int &A, const int &b) {return a > B;} int main (void) {//freopen ("In.txt", "R", stdin); int t = 0; cin>>t; while (t--) {int n, K; cin>>n>>k; int zero = 0; If the input data contains a factor of k, then the result must be 1 int v = 0; int j = 0; for (int i=0; i<n; ++i) {scanf ("%d", &v); if (K%v = = 0) zero = 1; if (v < k) arr[j++] = v; } IF (zero = = 1) {printf ("1\n"); Continue } sort (arr, arr+j, CMP); ORDER by DESC//printf ("%d\n", arr[0]); int ans = INF; DFS (0, J, K, ans); printf ("%d\n", Ans==inf -1:ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Http://blog.csdn.net/core__code
Bestcoder #49 Untitled HDU 5339