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 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 ($)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF=0x3f3f3f3f;

int n,a;
int b[30];

int main()
{
  int T_T;
  scanf("%d",&T_T);
  while(T_T--)
    {
      scanf("%d%d",&n,&a);
      for(int i=0;i<n;i++)
        {
          scanf("%d",b+i);
        }
      sort(b,b+n,greater<int>());

      int ans=INF;
      for(int i=0;i<(1<<n);i++)
        {
          int ta=a;
          int cnt=0;
          for(int j=0;j<n;j++)
            {
              if((i>>j)&1)
                {
                  cnt++;
                  ta%=b[j];
                }
            }
          if(ta==0)
            {
              ans=min(ans,cnt);
            }
        }
      if(ans==INF) ans=-1;
      printf("%d\n",ans);
    }
  return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-10 01:15:44

HDOJ 5339 Untitled 水的相关文章

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

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 (状态压缩枚举)

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(暴搜)

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【搜索】

题目链接: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

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

HDOJ 5317 RGCDQ 水

预处理出每个数有多少个不同的因数,因数最多不超过7 RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 641    Accepted Submission(s): 304 Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He

[hdoj]1143递归水题……可是我还没做出来……

1 #include <stdio.h> 2 #define MAX 31 3 int a[MAX]; 4 int main() { 5 int n; 6 a[0] = 1; 7 a[2] = 3; 8 for(int i = 4; i< MAX; i = i+2) 9 a[i] = 4*a[i-2] - a[i-4]; 10 while(scanf("%d",&n)==1&&n!=-1) 11 printf("%d\n",