CodeForce Round#49 untitled (Hdu 5339)

Untitled

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 947    Accepted Submission(s): 538

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, and at the end, we want a to become 0). Please determine the minimum value of r. If the goal cannot be achieved, print −1 instead.

Input

The first line contains one integer T≤5, which represents the number of testcases.

For each testcase, there are two lines:

1. The first line contains two integers n and a (1≤n≤20,1≤a≤106).

2. The second line contains n integers b1,…,bn (∀1≤i≤n,1≤bi≤106).

Output

Print T answers in T lines.

Sample Input

2
2 9
2 7
2 9
6 7

Sample Output

2
-1

Source

BestCoder Round #49 ($)

本次题目就是个纯暴力搜索的一道题目,唯一一点要注意的就是序列应该先降序排列,为什么呢?(因为如果升序排列的话i<j,Ci小于Cj,Ci先进行取余运算,那么Cj就没有意义了)

除此之外就是简单的深搜,附上本人的代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define maxn 30
 4 int min, sum,n;
 5 int a[maxn], b[maxn],c[maxn];
 6 int Min(int a, int b)
 7 {
 8     return a> b ? b:a;
 9 }
10 int dfs(int m)
11 {
12     if (m == n + 1)
13     {
14         int temp = sum;
15         int length = 0;
16         for (int j = 1; j <=n; j++)
17         {
18             if (temp == 0)
19                 break;
20             if (c[j] == 0)
21             {
22                 temp %= a[j];
23                 length++;
24             }
25         }
26         if (temp==0)
27         min = Min(min, length);
28         return 0;
29     }       //这里利用选择为0,未选为1,方便代码调试时按照二进制运算来看,比较直观
30         c[m] = 0;//用来标记哪些被选了
31         dfs(m + 1);
32         c[m] = 1;//标记未被选
33         dfs(m + 1);
34 }
35 int cmp(const void*a, const void*b)
36 {
37     return *(int *)b - *(int*)a;
38 }
39 int main()
40 {
41     int num;
42     scanf("%d", &num);
43     while (num--)
44     {
45         min= 1000000;
46         scanf("%d%d", &n, &sum);
47         for (int i = 1; i <= n; i++)
48         scanf("%d", &a[i]);
49         qsort(&a[1], n, sizeof(a[1]), cmp);
50         dfs(1);
51         if (min!=1000000)
52         printf("%d\n", min);
53         else printf("-1\n");
54     }
55 }
时间: 2024-08-26 09:27:36

CodeForce Round#49 untitled (Hdu 5339)的相关文章

BestCoder #49 Untitled HDU 5339

BestCoder #49 Untitled  HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php?pid=5339 本题采用深搜, 数据量小,先做排序处理(降序), 然后深搜的时候,进行剪枝,比K大的数就没必要往后搜索,直接剪掉. #include <iostream> #include <algorithm> #include <cstdio> using namespace std; const int INF =

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

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(回溯)

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

Manacher BestCoder Round #49 ($) 1002 Three Palindromes

题目传送门 1 /* 2 Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 3 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间,位运算 >>1 比 /2 速度快,用了程序跑快200ms左右,位运算大法好 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1

BestCoder Round #3 1001 &amp;&amp; HDU 4907 Task schedule (预处理)

题目链接:HDU 4907 Task schedule 中文题. 思路:将工作表存在vis的组数中.预处理一遍.具体看代码 AC代码: #include<stdio.h> #include<string.h> bool vis[200100]; int tak[200100]; int main() { int j,i,ti; int n,m,t,num; while(scanf("%d",&t)!=EOF) { while(t--) { memset(

BestCoder Round #3 A hdu 4907 Task schedule

Task schedule                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 307    Accepted Submission(s): 160 Problem Description 有一台机器

Codeforces Beta Round #49 (Div. 2)

Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define sqr(x) ((x)*(x)) 6 #define pb push_back 7 #define

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