BestCoder#49
Untitled
There is an integer aaa and nnn integers b1,…,bnb_1, \ldots, b_nb1,…,bn. After selecting some numbers from b1,…,bnb_1, \ldots, b_nb1,…,bn in any order, say c1,…,crc_1, \ldots, c_rc1,…,cr, we want to make sure that a mod c1 mod c2 mod… mod cr=0a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ mod \ c_r = 0a mod c1 mod c2 mod… mod cr=0 (i.e., aaa will become the remainder divided by cic_ici each time, and at the end, we want aaa to become 000). Please determine the minimum value of rrr. If the goal cannot be achieved, print −1-1−1 instead.
The first line contains one integer T≤5T \leq 5T≤5, which represents the number of testcases.
For each testcase, there are two lines:
The first line contains two integers nnn and aaa (1≤n≤20,1≤a≤1061 \leq n \leq 20, 1 \leq a \leq 10^61≤n≤20,1≤a≤106).
The second line contains nnn integers b1,…,bnb_1, \ldots, b_nb1,…,bn (∀1≤i≤n,1≤bi≤106\forall 1\leq i \leq n, 1 \leq b_i \leq 10^6∀1≤i≤n,1≤bi≤106).
Print TTT answers in TTT lines.
2
2 9
2 7
2 9
6 7
2
-1 运用二进制的思想,去枚举每种可能的情况,然后算下最小的。当然,先把比 a 大的数筛掉
#include <iostream>
#include <algorithm>
using namespace std; int main()
{
int T;
cin >> T;
int n, a, b[], i, cnt;
while(T--) {
int Min = ;
bool tag = false;
cin >>n >> a;
for(int i = ; i < n; ++i)
cin >> b[i];
sort(b, b+n);
for(i = n-; i >= ; --i) {
if(a >= b[i])
break;
}
if(i == ) {
if(a % b[] == )
cout << << endl;
else
cout << - << endl;
} else {
for(int j = ; j < ( << (i+)); ++j) {
int tmp = a;
cnt = ;
for(int k = i; k >= ; --k) {
if(( << k) & j) {
tmp %= b[k];
cnt++;
//cout << j << " " <<tmp << " " << b[k] << " "<<cnt << endl;;
}
}
if(tmp == ) {
Min = min(Min, cnt);
tag = true;
}
}
if(tag)
cout << Min << endl;
else
cout << - << endl;
}
}
return ;
}
BestCoder#49的更多相关文章
- BestCoder #49 Untitled HDU 5339
BestCoder #49 Untitled HDU 5339 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5339 本题採用深搜, 数据量小,先做 ...
- Manacher BestCoder Round #49 ($) 1002 Three Palindromes
题目传送门 /* Manacher:该算法能求最长回文串,思路时依据回文半径p数组找到第一个和第三个会文串,然后暴力枚举判断是否存在中间的回文串 另外,在原字符串没啥用时可以直接覆盖,省去一个数组空间 ...
- BestCoder Round #49
呵呵哒,1001的dfs返回值写错,wa了两发就没分了,1002显然是PAM可是我没学过啊!!!压位暴力可不可以...看看范围貌似不行,弃疗...1003根本不会做,1004想了想lcc发现不可做,那 ...
- DFS BestCoder Round #49 ($) 1001 Untitled
题目传送门 /* DFS:从大到小取模,因为对比自己大的数取模没意义,可以剪枝.但是我从小到大也过了,可能没啥大数据 */ /************************************* ...
- CodeForce Round#49 untitled (Hdu 5339)
Untitled Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- hdu 5195 DZY Loves Topological Sorting BestCoder Round #35 1002 [ 拓扑排序 + 优先队列 || 线段树 ]
传送门 DZY Loves Topological Sorting Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131 ...
- Bestcoder#5 1002
Bestcoder#5 1002 Poor MitsuiTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- 49. 3种方法实现复杂链表的复制[clone of complex linked list]
[本文链接] http://www.cnblogs.com/hellogiser/p/clone-of-complex-linked-list.html [题目] 有一个复杂链表,其结点除了有一个ne ...
- BestCoder Round #80 1002
HDU 5666 Segment 题意:给你条斜率为-1,常数项为q(q为质数)的直线,连接原点与直线上整数格点,问你在有多少个格点在形成的无数个三角形内,而不在线段上,结果对P取模. 思路:best ...
随机推荐
- ASP.NET、JAVA跨服务器远程上传文件(图片)的相关解决方案整合
一.图片提交例: A端--提交图片 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string u ...
- SubMenu的setHeaderView使用时发现的问题
SubMenu android.view.SubMenu.setHeaderView(View view) 上面是这个方法的完整签名,作用就是自定义子菜单的菜单头,但是在OptionsMenu里面设置 ...
- 关于数组去重的几种方法-------javascript描述
第一种方法:借助json对象来实现,若json对象中无该属性则添加,否则不添加,最后返回json对象的属性,时间复杂度为O(n) function deleteArrayRepeat(arr) { v ...
- http://detectmobilebrowsers.com/
<%@ Page Language="C#" %> <%@ Import Namespace="System.Text.RegularExpressio ...
- tomcat 快速部署静态文件
server.conf配置: <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software ...
- nodejs 笔记
安装环境----------------------------------------------------------------1,安装nodejs 起步------------------- ...
- 报错:1130-host ... is not allowed to connect to this MySql server
报错:1130-host ... is not allowed to connect to this MySql server 解决方法: 1. 改表法. 可能是你的帐号不允许从远程登陆,只能在l ...
- Linux下安装流量监控工具iftop
在Linux系统中,top命令可以查看系统资源包括内存,CPU占用信息,查看和探测网络状态可以使用netstat,nmap等工具,实时流量监控可以使用iftop,下面是在CentOS7系列系统上安装i ...
- ztree-demo 2
<!DOCTYPE html> <HTML> <HEAD> <TITLE> ZTREE DEMO - Async</TITLE> <m ...
- Best code水题之路
BestCoder 2nd Anniversary: 1001.Oracle There is once a king and queen, rulers of an unnamed city, wh ...