HDU 5339 Untitled

。。

Untitled

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 301    Accepted Submission(s): 156


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  T5 , which represents the number of testcases. 

For each testcase, there are two lines:

1. The first line contains two integers  n  and  a  ( 1n20,1a106 ).

2. The second line contains  n  integers  b1,,bn  ( ?1in,1bi106 ).
 

Output
Print  T  answers in  T  lines.
 

Sample Input
    
    
2 2 9 2 7 2 9 6 7
 

Sample Output
    
    
2 -1
 
搜索+剪枝

/* ***********************************************
Author :
Created Time :2015/8/1 18:42:07
File Name :2.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 100+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;

bool cmp(int a,int b){
return a>b;
}
int a[maxn];
int x,y;
int n;
int ans;
int mark;
void dfs(int x,int cnt){
if(cnt>n)return ;
if(x==0) {
mark=1;
ans=min(ans,cnt);return ;
}
for(int i=1;i<=n;i++){
if(x<a[i])continue;//剪枝
y=x%a[i];
dfs(y,cnt+1);
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t;
cin>>t;
while(t--){
cle(a);
scanf("%d %d",&n,&x);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
ans=INF;
mark=0;
dfs(x,0);
if(mark)printf("%d\n",ans);
else puts("-1");
}
return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值