HDU 5339

Untitled

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 828    Accepted Submission(s): 460

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

//题意:给你n个数 在n个数中调出最少的元素使得 m%a1%a2%a3=0不存在就输出-1

//搜索

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int m,n;
int d[1000010];
bool vis[1000010];

struct Node
{
    int x,step;
};

int bfs(int x)
{
    queue<Node>q;
    Node a;
    a.x=x,a.step=0;
    q.push(a);
    while(!q.empty())
    {
        Node b=q.front();
        q.pop();
        vis[b.x]=1;
        if(b.x==0)return b.step;
        for(int i=n-1;i>=0;i--)
        {
            Node c=b;
            c.x=c.x%d[i];
            c.step++;
            if(!vis[c.x])
            {
                vis[c.x]=1;
                q.push(c);
            }
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(d,0,sizeof(d));
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&d[i]);
        }
            int cnt=bfs(m);
            if(cnt==0)
                printf("-1\n");
            else
                printf("%d\n",cnt);
    }
    return 0;
}

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

时间: 2024-08-06 09:17:35

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

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

题目链接: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(暴搜)

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

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 (数学思维)

题意:给一个数a和n个数b1,b2,...,bn. 从n个数中选择一些数重新排列成c1,c2,...,cm使得a%c1%c2%...%cm=0. 如果能选出则输出最少需要几个数,否则输出-1. 分析: 首先很重要的一点就是 “一定是先除大的数再除小的数” 因为如果先除小的数x,则取模的数一定是在0到x-1中,再除大于x的数是没意义的. 即选出的一列数是非递增序列. 由于n最多只有20个,简单的2^n的搜索即可搞定了. 通过这题发现hdu oj的一个bug 就是排序的时候不能手写整数的cmp使其逆

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

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

hdu 5339 递归枚举

只有20个点,从大到小排序然后枚举即可.这里做了一个优化,不模大于自己的数,因为这是徒劳的,我们要求的是最小的r. 注意:不排序就枚举是错误的,想一想,为什么. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int INF = 99; 8 const int N = 2