您的位置:首页 > 其它

HDU 5339 Untitled

2016-04-21 19:03 246 查看

。。

Untitled

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


[align=left]Problem Description[/align]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.
 
[align=left]Input[/align]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).
 
[align=left]Output[/align]Print T answers in T lines.
 
[align=left]Sample Input[/align]
2
2 9
2 7
2 9
6 7 
[align=left]Sample Output[/align]
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: