BestCoder #49 Untitled HDU 5339,
BestCoder #49 Untitled HDU 5339
Question: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5339
This topic uses deep search, with a small amount of data. Sort the data first (in descending order), and then perform pruning during Deep Search. If the number is larger than K, you do not need to search for it and cut it off directly.
# 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]) // do pruning. if it is greater than k, there is no need to process it later {dfs (t + 1, n, k % arr [t], ans); dfs (t + 1, n, k, ans) ;}} inline bool cmp (const int & a, const int & B) {r Eturn 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, 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Http://blog.csdn.net/core__code