DFS BestCoder Round #49 ($) 1001 Untitled

 

题目传送门

 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 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4697333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值