BestCoder #49 Untitled HDU 5339,

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.