BestCoder Round #49 ($) 1001

12 篇文章 0 订阅
12 篇文章 0 订阅


Problem Description

There is an integer aaa and nnn integers b1,…,bnb_1, \ldots, b_nb1,,bn. After selecting some numbers from b1,…,bnb_1, \ldots, b_nb1,,bn in any order, say c1,…,crc_1, \ldots, c_rc1,,cr, we want to make sure that a mod c1 mod c2 mod… mod cr=0a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ mod \ c_r = 0a mod c1 mod c2 mod mod cr=0 (i.e., aaa will become the remainder divided by cic_ici each time, and at the end, we want aaa to become 000). Please determine the minimum value of rrr. If the goal cannot be achieved, print −1-11 instead.

Input

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

For each testcase, there are two lines:

  1. The first line contains two integers nnn and aaa (1≤n≤20,1≤a≤1061 \leq n \leq 20, 1 \leq a \leq 10^61n20,1a106).

  2. The second line contains nnn integers b1,…,bnb_1, \ldots, b_nb1,,bn (∀1≤i≤n,1≤bi≤106\forall 1\leq i \leq n, 1 \leq b_i \leq 10^61in,1bi106).

Output

Print TTT answers in TTT lines.

Sample Input
2
2 9
2 7
2 9
6 7
Sample Output
2
-1


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f3f

using namespace std;
bool vis[25];
int b[21];
int n;
int ant;

void DFS(int pos,int a,int step)
{
    if(pos < 0 || pos >=n || vis[pos])
        return ;
    if(ant <= step)
        return ;
    if(a%b[pos] == 0)
    {
        if(ant > step + 1)
            ant = step + 1;
        return ;
    }
    vis[pos] = true;
    DFS(pos + 1, a%b[pos], step+1);
    DFS(pos - 1, a%b[pos], step+1);
    vis[pos] = false;
    DFS(pos + 1,a,step);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(vis,false,sizeof(vis));
        bool flag = false;
        int a;
        ant = INF;
        scanf("%d%d",&n,&a);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&b[i]);
            if(a % b[i] == 0)
            {
                flag = true;
            }
        }
        if(!flag)
        {

            DFS(0,a,0);
            if(ant == INF)
                cout<<-1<<endl;
            else
                cout<<ant<<endl;
        }
        else
        {
            cout<<1<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值