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冲刺Day7

    一.会议照片 二.项目进展 1.今日安排 今天都是课,主要就是用空闲时间熟悉一下框架使用以及继续进行框架搭建. 2.问题困难 前台界面框架vue和element-ui的写法要适应. 3.心得体会 vu ...

  2. 20162323周楠《Java程序设计与数据结构》第八周总结

    20162323周楠 2016-2017-2 <程序设计与数据结构>第八周学习总结 教材学习内容总结 一个异常是一个对象,它定义了并不轻易出现的或是错误的情形 异常由程序或运行时环境抛出, ...

  3. GitChat招募IT类写作作者

    GitChat是一个移动端的IT知识.技术分享平台,于2017.10和CSDN合并,成为其旗下独立品牌. 我们正在寻求有互联网基因的人来一起分享IT人员的关切,诚挚邀请您来做一次分享(让IT类文章变现 ...

  4. LeetCode & Q283-Move Zeroes-Easy

    Array Two Pointers Description: Given an array nums, write a function to move all 0's to the end of ...

  5. SiteMesh入门(1-1)SiteMesh是什么?

    1.问题的提出 在开发Web 应用时,Web页面可能由不同的人参与开发,因此开发出来的界面通常千奇百怪.五花八门,风格难以保持一致. 为了统一界面的风格,Struts 框架提供了一个标签库Tiles ...

  6. Excel 日期截取(函数)

    需求:时间段截取,去掉年月日,保留时分. 实现函数:    =TEXT(A2,"HH:MM")&"-"&TEXT(B2,"HH:MM& ...

  7. WebStorm2018破解

    参考网站http://www.sdbeta.com/wg/2018/0302/220048.html修改整理如下: webstorm 2018.1正式版破解summary jetbrainscrack ...

  8. 解决Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Student_recruit]]

    查看web.xml文件的书写,特别注意路径与命名一致

  9. Ubuntu下发射wifi

    iphone要连接的话,mode选Ad-hoc, wifi密码要用WEP 40/128-bit key模式

  10. 07、NetCore2.0依赖注入(DI)之生命周期

    07.NetCore2.0依赖注入(DI)之生命周期 NetCore2.0依赖注入框架(DI)是如何管理注入对象的生命周期的?生命周期有哪几类,又是在哪些场景下应用的呢? -------------- ...