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. C语言--第四周作业

    一.题目7-1 计算分段函数[1] 1.代码 #include <stdio.h> int main () { float x,result; scanf("%f",& ...

  2. find命令之(-atime,-ctime,-mtime)

    关于find命令,以拙见总结如下: >>>定义: find命令用来在指定目录下查找文件. 任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则fin ...

  3. 学大伟业 国庆Day2

    期望得分:30+100+0=130 实际得分:30+100+20=150 忍者钩爪 (ninja.pas/c/cpp) [问题描述] 小Q是一名酷爱钩爪的忍者,最喜欢飞檐走壁的感觉,有一天小Q发现一个 ...

  4. 用‘+=’拼接字符串,打印时总会出现一个undefined

    var str; for(var i = 0; i < 5; i++){ str += String(i); } console.log(str); 他喵的,打印的结果竟然是"unde ...

  5. 修改了SpringBoot的主类名称后,gradle build报错的解决办法

    Unable to find a single main class from the following candidates [*.*Application]

  6. hadoop2.6.0实践:引入开发依赖的jar包

    hadoop-2.5.0\share\hadoop\common  所有jar,hadoop-2.5.0\share\hadoop\common\lib  所有jar,hadoop-2.5.0\sha ...

  7. python入门(14)定义函数和接收返回值

    定义函数: 定义一个求绝对值的my_abs函数为例: def my_abs(x): if x >= 0: return x else: return -x 如果没有return语句,函数执行完毕 ...

  8. 我的第二个开源库SuperTextView——中文文档

    一个简单的TextView实现了打字机的效果让文字一个个显示出来, 方法介绍: startShow  开始打字 使用: startShow(int typeStartTime,int typeTime ...

  9. Python入门之PyCharm的快捷键与常用设置和扩展(Win系统)

    1.  PyCharm的快捷键 2 . PyCharm的常用设置和扩展 ---------------------------------------------------------------- ...

  10. 前端学习之jquery

    前端学习之jquery 1.   什么是jQuery对象? jQuery对象就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的.如果一个对象是jQuery对象,那么它 ...