杭电一水题(DFS)Untitled

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

问题描述

有一个整数aa和nn个整数b_1, \ldots, b_nb?1??,…,b?n??。在这些数中选出若干个数并重新排列,得到c_1, \ldots, c_rc?1??,…,c?r??。我们想保证a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ mod \ c_r = 0a mod c?1?? mod c?2?? mod… mod c?r??=0。请你得出最小的rr,也就是最少要选择多少个数字。如果无解,请输出-1−1.

输入描述

输入文件的第一行有一个正整数 T \leq 5T≤5,表示数据组数。

接下去有TT组数据,每组数据的第一行有两个正整数nn和aa (1 \leq n \leq 20, 1 \leq a \leq 10^{6}1≤n≤20,1≤a≤10?6??).

第二行有nn个正整数b_1, \ldots, b_nb?1??,…,b?n?? (\forall 1\leq i \leq n, 1 \leq b_i \leq 10^{6}∀1≤i≤n,1≤b?i??≤10?6??).

输出描述

输出TT行TT个数表示每次询问的答案。

输入样例

2
2 9
2 7
2 9
6 7

输出样例

2
-1

这道题使用dfs是很容易求的,可是需要剪枝,当时手贱没剪好,正确答案都变成-1了。。。。。。。。。。

#include"iostream"
#include"algorithm"
#include"cstdio"
using namespace std;
int n,c[30],a,sum,ok=0;

bool cmp(int a,int b)
{
    return a>b;
}

void dfs(int cc,int d)
{
int j;

if(cc==0) {ok=d;return;}

if(d==n)
return;

for(j=1;j<=n;j++) if(c[j]<=cc) break;

for(int i=j;i<=n;i++) dfs(cc%c[i],d+1);

}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n>>a;

        ok=-1;

        for(int i=1;i<=n;i++) cin>>c[i];

        sort(c+1,c+1+n,cmp);

     for(int i=1;i<=n;i++)
        dfs(a%c[i],1);

        cout<<ok<<endl;
    }
    return 0;
}

 

时间: 2024-08-05 21:42:15

杭电一水题(DFS)Untitled的相关文章

杭电dp题集,附链接

Robberies 点击打开链接 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f[j]=max{f[j],f[j-q[i].v]+q[i].money}(f[j]表示在概率j之下能抢的大洋); 正确的方程是:f[j]=max(f[j],f[j-q[i].money]*q[i].v)  其中,f[j]表示抢j块大洋的最大的逃脱概率,条件是f[j-q[i].money]可达,也就是

acm入门 杭电1001题 有关溢出的考虑

最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM(n) = 1 + 2 + 3 + ... + n. Input The input will consist of a series of integers n, one integer per line Output For each case, output SUM(n) in one line

杭电水题

杭电OJ上的水题:C语言程序设计练习 题目如下: 链接: http://acm.hdu.edu.cn/showproblem.php?pid=2001 注:pid=后面可以为2000 - 2050中的任何一个(对应50道题目) 另外也可以做1000之后的(按顺序做,从1000开始把,1000开头的都蛮简单的) 原文地址:https://www.cnblogs.com/wyb666/p/10909202.html

小试牛刀-搜索基础入门(杭电五题)

hdu 1241 Oil Deposits 水题,BFS,判断区域的块数. 代码清单: #include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef pair<int,int>P; int m,n; char s[105][105]; int xy[8][2]=

变形课 HUD杭电1181 【DFS】

Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规律:如果咒语是以a开头b结尾的一个单词,那么它的作用就恰好是使A物体变成B物体. Harry已经将他所会的所有咒语都列成了一个表,他想让你帮忙计算一下他是否能完成老师的作业,将一个B(ball)变成一个M(Mouse),你知道,如果他自己不能完成的话,他就只好向Hermione请教,并且被迫听

杭电算法题 HDU 1000-1004

h1,h2,h3,h4,h5,h6,p,blockquote { margin: 0; padding: 0 } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif; font-size: 13px; line-height: 18px; color: #737373; background-color: white; margin: 10px

Fire Net HDU杭电1045【DFS】

Problem Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through wh

Sum It Up POJ 1564 HDU 杭电1258【DFS】

Problem Description Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3

动态规划解决杭电OJ1080题——LCS的变种

首先上题目: Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2570    Accepted Submission(s): 1451 Problem Description It is well known that a human gene can be considered as a se