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

用a去模b中的数,问最少需要多少个数才能使a变为0

感觉模的数字大,需要的数可能就会少一点,所以排个序,再暴力解决

#include <iostream>
#include <cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a[30]; bool cmp(int a,int b)
{
return a > b;
} int work(int q,int n)
{
int minx = 0x3f3f3f3f;
int i,j;
for(i = 1; i <= n; i++)
{
int temp = q;
for( j = i; j <= n; j++)
{
if(temp == 0)
break;
else
temp %= a[j];
}
if(temp == 0)
minx = min(minx,j-i);
}
return minx;
} int main()
{
int T;
int n,all;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&all);
for(int i=1; i<=n; i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
int p = work(all,n);
if(p != 0x3f3f3f3f)
printf("%d\n",p);
else
printf("-1\n");
}
return 0;
}

  

HDU5339——Untitled的更多相关文章

  1. CodeForce Round#49 untitled (Hdu 5339)

    Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. Jupyter运行时出现下面的错误:Unexpected error while saving file: arma/Untitled.ipynb [Errno 13] Permission denied:

    运行环境:Ubuntu16.04+Python2.7执行如下代码修改Jupyter的一部分文件的权限(执行完之后重新启动即可): sudo chmod ~/.local/share/jupyter/ ...

  3. 【QT】Cannot find file: untitled.pro,项目路径不要包含中文。

    Cannot find file: D:\文件及下载相关\文档\untitled\untitled.pro. 17:01:45: 进程"D:\Englishpath\QT5.9.3\5.9. ...

  4. BestCoder #49 Untitled HDU 5339

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

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

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

  6. Django 修改该项目文件夹、项目名及项目文件夹中同名文件夹,报错 ModuleNotFoundError: No module named 'untitled'

    如果你直接重构项目文件夹名及重构项目名和重构项目文件夹内同名文件夹 执行项目报错 ModuleNotFoundError: No module named 'untitled' 请执行以下操作

  7. jupyter notebook new Python3报错:Permission denied: Untitled.ipynb,修改workspace

    点击新建Python文件即弹出弹窗显示 Permission denied: Untitled.ipynb 看到Permission denied 尝试是权限问题进行解决,各种百度结果都是对文件进行权 ...

  8. hdu 5339 Untitled

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

  9. HDU 5339 Untitled (暴力枚举)

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

随机推荐

  1. 项目Alpha冲刺Day2

    一.会议照片 二.项目进展 1.今日安排 初步搭建后台框架,根据昨天的最终设计再修改原型,成功使用powerDesigner导出sql. 2.问题困难 使用了比较多的框架,而且是首次尝试纯java配置 ...

  2. JAVA的循环控制与循环嵌套

    循环控制和循环嵌套 循环控制是除了循环条件之外,控制循环是否进行的一个机制,这给处理循环问题带来了灵活性.循环体内的语句块可以是顺序执行的语句,可以是分支结构的语句,也可以是循环语句,循环中含循环,就 ...

  3. 腾讯云服务器上安装phstudy和lnmp

    phpstudy的安装:wget -c http://lamp.phpstudy.net/phpstudy.bin chmod +x phpstudy.bin #权限设置./phpstudy.bin ...

  4. 用Java语言实现简单的词法分析器

    编译原理中的词法分析算是很重要的一个部分,原理比较简单,不过网上大部分都是用C语言或者C++来编写,笔者近期在学习Java,故用Java语言实现了简单的词法分析器. 要分析的代码段如下: 输出结果如下 ...

  5. python random 模块的用法

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  6. mosquitto验证client互相踢

    cleint11A订阅topic#################################################### server发送topic消息 ############### ...

  7. Spring Security 入门(1-3-2)Spring Security - http元素 - intercept-url配置

    http元素下可以配置登录页面,也可以配置 url 拦截. 1.直接配置拦截url和对应的访问权限 <security:http use-expressions="false" ...

  8. Spring Security 入门(3-10)Spring Security 的四种使用方式

    原文链接: http://www.360doc.com/content/14/0724/17/18637323_396779659.shtml 下面是作者的一个问题处理

  9. .NET:持续进化的统一开发平台

    阅读文本大概需要 8 分钟. 标题使用的是进化这个词语,是因为 .NET 在不断的努力,也在不断的重构. 这篇文章的更多目的和意义在于科普,俗称"传教". # 持续进化的 .NET ...

  10. python实现 多叉树 寻找最短路径

    完全原创,能力有限,欢迎参考,未经允许,请勿转载 ! 完全原创,能力有限,欢迎参考,未经允许,请勿转载 ! 完全原创,能力有限,欢迎参考,未经允许,请勿转载 ! 完全原创,能力有限,欢迎参考,未经允许 ...