Untitled

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

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
 
Source
 
本次题目就是个纯暴力搜索的一道题目,唯一一点要注意的就是序列应该先降序排列,为什么呢?(因为如果升序排列的话i<j,Ci小于Cj,Ci先进行取余运算,那么Cj就没有意义了)
除此之外就是简单的深搜,附上本人的代码
 #include<stdio.h>
#include<stdlib.h>
#define maxn 30
int min, sum,n;
int a[maxn], b[maxn],c[maxn];
int Min(int a, int b)
{
return a> b ? b:a;
}
int dfs(int m)
{
if (m == n + )
{
int temp = sum;
int length = ;
for (int j = ; j <=n; j++)
{
if (temp == )
break;
if (c[j] == )
{
temp %= a[j];
length++;
}
}
if (temp==)
min = Min(min, length);
return ;
}
//这里利用选择为0,未选为1,方便代码调试时按照二进制运算来看,比较直观
c[m] = ;//用来标记哪些被选了
dfs(m + );
c[m] = ;//标记未被选
dfs(m + );
}
int cmp(const void*a, const void*b)
{
return *(int *)b - *(int*)a;
}
int main()
{
int num;
scanf("%d", &num);
while (num--)
{
min= ;
scanf("%d%d", &n, &sum);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
qsort(&a[], n, sizeof(a[]), cmp);
dfs();
if (min!=)
printf("%d\n", min);
else printf("-1\n");
}
}
 
 

CodeForce Round#49 untitled (Hdu 5339)的更多相关文章

  1. BestCoder #49 Untitled HDU 5339

    BestCoder #49 Untitled  HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5339 本题採用深搜, 数据量小,先做 ...

  2. Codeforces Beta Round #49 (Div. 2)

    Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A #include<bits/stdc++.h> ...

  3. Manacher BestCoder Round #49 ($) 1002 Three Palindromes

    题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...

  4. Codeforce Round #643 #645 #646 (Div2)

    codeforce Round #643 #645 #646 div2 Round #643 problem A #include<bits/stdc++.h> using namespa ...

  5. hdu 5339 Untitled【搜索】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5339 题意:一个整数a 和一个数组b.问你能否在b中取出r个元素排列组成c数组满足a%c1%c1%-. ...

  6. DFS BestCoder Round #49 ($) 1001 Untitled

    题目传送门 /* DFS:从大到小取模,因为对比自己大的数取模没意义,可以剪枝.但是我从小到大也过了,可能没啥大数据 */ /************************************* ...

  7. hdu 5339 Untitled

    这题很明显是签到题,可我比赛时却没做出,赤裸裸的爆零了,真悲剧…… 看了题解后才知道直接暴搜就行,只是需要把它们从大到小排序后再搜,我当时就没想到...不想再多说了 一开始我直接枚举所有情况: #in ...

  8. HDU 5339 Untitled (暴力枚举)

    题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也 ...

  9. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

随机推荐

  1. Akka to .NET

    Akka.NET Current Roadmap Akka.NET is a community-driven port of the popular Java/Scala framework Akk ...

  2. 从C#到Objective-C,循序渐进学习苹果开发(5)--利用XCode来进行IOS的程序开发

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验.前面几篇随笔主要介绍C#和O ...

  3. ASP.NET MVC 请求流程

    一.应用程序启动 1.Application_Start方法,程序启动 2.RegisterRoutes方法,注册路由 3.System.Web.Mvc.RouteCollectionExtensio ...

  4. 【C#】1.3 WPF应用程序学习要点

    分类:C#.VS2015 创建日期:2016-06-14 使用教材:十二五国家级规划教材<C#程序设计及应用教程>(第3版) 一.要点概述 <C#程序设计及应用教程>(第3版) ...

  5. 孙鑫MFC学习笔记11:保存图像

    1.CPtrArray指针数组 2.CPtrArray返回void指针,需要做类型转换 3.View类中的OnPaint调用OnPrepareDC和OnDraw,如果覆盖OnPaint,就不会调用On ...

  6. Eclipse使用多个Console

    调试程序时,有时需要同时使用多个Console,如一个Console运行Server,一个Console运行Client.而一般情况下Eclipse只有一个Console,这时就需要增加一个Conso ...

  7. DatagramSocket收发UDP数据包

    Java的Socket通信分为TCP和UDP两种,Socket和ServerSocket类用于TCP通信,DatagramSocket用于UDP通信. 使用DatagramSocket发送UDP数据包 ...

  8. 【Java每日一题】20161202

    20161201问题解析请点击今日问题下方的"[Java每日一题]20161202"查看 package Dec2016; public class Ques1202 { publ ...

  9. 第 15 章 CSS 文本样式[上]

    学习要点: 1.字体总汇 2.字体设置 3.Web 字体 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS 文本样式,通过文本样式的设置,更改字体的大小.样式以及文本的方位. 一.字体总汇 本节 ...

  10. python日志模块---logging

    1.将日志打印到屏幕 import logging logging.debug('This is debug message---by liu-ke') logging.info('This is i ...