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. Python实现基于协程的异步爬虫

    一.课程介绍 1. 课程来源 本课程核心部分来自<500 lines or less>项目,作者是来自 MongoDB 的工程师 A. Jesse Jiryu Davis 与 Python ...

  2. c# windows service 实现监控其他程序是否被关闭,关闭则报警

    namespace MonitorService { public partial class MonitorSv : ServiceBase { string AppName = "&qu ...

  3. PHP之this和self

    self在对象中自己调用自己使用 $this在实例化后使用$this方法 在访问PHP类中的成员变量或方法时,如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就 ...

  4. New UWP Community Toolkit - DropShadowPanel

    概述 UWP Community Toolkit  中有一个为 Frmework Element 提供投影效果的控件 - DropShadowPanel,本篇我们结合代码详细讲解  DropShado ...

  5. ArrayList、Vector、LinkedList、HashMap、HashTable的存储性能和特性

    ArrayList和Vector都是使用数组方式存储数据,次数组元素大于实际存储的数据以便添加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内存操作,所以索引数据快而插入数 ...

  6. XPath编写规则学习

    XPath编写规则学习   辅助工具:firefox安装findbugs,view Xpath firefox :Xpath验证方式:$x("xpath"); 粘贴xpath语句回 ...

  7. SpringCloud的EurekaClient : 客户端应用访问注册的微服务(无断路器场景)

    演示客户端应用如何访问注册在EurekaServer里的微服务 一.概念和定义 采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,2. ...

  8. jprofiler配置

    cataline.sh JAVA_OPTS="$JAVA_OPTS -agentlib:jprofilerti=port=8849"JAVA_OPTS="$JAVA_OP ...

  9. 转:NLP+句法结构(三)︱中文句法结构(CIPS2016、依存句法、文法)

    NLP+句法结构(三)︱中文句法结构(CIPS2016.依存句法.文法)转自:https://www.cnblogs.com/maohai/p/6453389.html 摘录自:CIPS2016 中文 ...

  10. leetcode算法:Trim a Binar Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...