DFS HDOJ 5339 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 }
时间: 2024-12-14 09:52:02

DFS HDOJ 5339 Untitled的相关文章

HDOJ 5339 Untitled 水

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 481    Accepted Submission(s): 245 Problem Description There is an integer a and n integers b1,-,bn. After selecting some numbers from b

hdu 5339 Untitled(回溯)

hdu 5339 Untitled 题目大意:给出n个数字的序列,和一个数a,在n中有m个数b1,...,bm使得__a %b1%b2%...%bm = 0,求最小的m. 解题思路:回溯. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int N = 5

hdu 5339 Untitled dfs

Problem Description There is an integer a and n integers b1,…,bn. After selecting some numbers from b1,…,bn in any order, say c1,…,cr, we want to make sure that a mod c1 mod c2 mod… mod cr=0 (i.e., a will become the remainder divided by ci each time,

hdu 5339 Untitled

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 166    Accepted Submission(s): 83 Problem Description There is an integer a and n integers b1,-,bn. After selecting some numbers from b1

HDU 5339 Untitled(暴搜)

Untitled Problem Description There is an integer $a$ and $n$ integers $b_1, \ldots, b_n$. After selecting some numbers from $b_1, \ldots, b_n$ in any order, say $c_1, \ldots, c_r$, we want to make sure that $a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ m

HDU 5339 Untitled (状态压缩枚举)

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 570    Accepted Submission(s): 291 Problem Description There is an integer a and n integers b1,-,bn. After selecting some numbers from b

hdu 5339 Untitled【搜索】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5339 题意:一个整数a 和一个数组b.问你能否在b中取出r个元素排列组成c数组满足a%c1%c1%-..%cr == 0.输出最小的r,不能满足条件输出-1. 思路:b按从大到小排序,暴搜. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include

DFS HDOJ 2181 哈密顿绕行世界问题

题目传送门 题意:中文题面 分析:直接排完序后DFS.这样的题以后不应该再写题解的. #include <bits/stdc++.h> using namespace std; vector<int> G[21]; int ans[21]; int v[3]; bool vis[21]; int cnt; void print() { printf ("%d: ", ++cnt); for (int i=0; i<20; ++i) { printf (&q

HDU 5339 Untitled (递归穷举)

题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也可以不选,两种情况.递归穷举每个可能性,O(2n). 1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <al