【玲珑学院 1058 - Coco】+ 递归

1058 - Coco

Time Limit:1s Memory Limit:64MByte

Submissions:262Solved:125
DESCRIPTION

Coco just learned a math operation call mod.Now,there is an integer a
and n integers b1,…,bn. After selecting some numbers from b1,…,bn in any order, say c1,…,cr, Coco want to make sure that amodc1modc2mod…modcr=0\ (i.e., a will become the remainder divided by ci each time, and at the end, Coco 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(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

递归~~~

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAXN = 1000010;
int ma[MAXN],vis[MAXN],N,ans;
void sol(int x,int cut){
    if(x == 0) {
    ans = cut;return;
}
    for(int i = 1 ; i <= N && ma[i] <= x; i++){
         if(!vis[x % ma[i]])
            vis[x % ma[i]] = 1,sol(x % ma[i],cut + 1);
         if(ans != -1) return;
     }
}
int main()
{
    int T; scanf("%d",&T);
    while(T--){
        int a; scanf("%d %d",&N,&a);
        for(int i = 1 ; i <= N; i++) scanf("%d",&ma[i]); 
        memset(vis,0,sizeof(vis)); ans = -1;
        sort(ma + 1 ,ma + 1 + N);
        sol(a,0);
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值