1. Runtime Error

 

Bahosain was trying to solve this simple problem, but he got a Runtime Error on one of the test cases, can you help him by solving it?

Given an array of N non-negative integers and an integer K, your task is to find two integers X and Y from the given array such that X × Y = K.

The chosen numbers must have different indices in the   array.

Input

 

The first line of input contains T (1 ≤ T ≤ 128), the number of test   cases.

The first line of each test case contains two integers: N (2 ≤ N ≤ 100,000) and K (1 ≤ K ≤ 100,000). The next line contains N space-separated integers, each between 0 and   100,000.

Output

 

For each test case, if there is no solution, print -1 on a single line. Otherwise print a single line with two space-separated integers X Y (X ≤   Y), where X and Y are two numbers from the given array and X × Y = K.

If there is more than one possible solution, print the one with the minimum   X.

Sample Input

Sample Output

4

2 6

6 12

-1

3 6 2

4 2

9

3 12

2 1

1 12

1 2

4 36

12 18

3 36

4 12

1 2 6

12

/*
题意:
从给出的N个数中找出两个数,乘积为 K; 枚举x 二分搜索 y
*/ #include"iostream"
#include"algorithm"
#include"cstdio"
#include"cstring"
#include"cmath"
#define MX 100000 + 50
using namespace std; int a[MX]; int main() {
int T,k,n;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&k);
for(int i=0; i<n; i++) {
scanf("%d",&a[i]);
}
sort(a,a+n);
int ans1=0,ans=0;
for(int i=0; i<n-1; i++) {
ans1=i; int l=i+1,r=n-1,mid;
while(l<=r) {
mid=(r+l)/2;
if(a[i]*a[mid]>k) {
r=mid-1;
} else if(a[i]*a[mid]<k) {
l=mid+1;
} else if(a[i]*a[mid]==k){
ans=mid;
break;
}
}
if(ans)break; }
if(ans)
printf("%d %d\n",a[ans1],a[ans]);
else printf("-1\n");
}
return 0;
}

  

ACM: 限时训练题解-Runtime Error-二分查找的更多相关文章

  1. ACM: 限时训练题解-Rock-Paper-Scissors-前缀和

    Rock-Paper-Scissors   Rock-Paper-Scissors is a two-player game, where each player chooses one of Roc ...

  2. ACM: 限时训练题解-Heavy Coins-枚举子集-暴力枚举

    Heavy Coins   Bahosain has a lot of coins in his pocket. These coins are really heavy, so he always ...

  3. ACM: 限时训练题解- Travelling Salesman-最小生成树

    Travelling Salesman   After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends mos ...

  4. ACM: 限时训练题解-Epic Professor-水题

    Epic Professor   Dr. Bahosain works as a professor of Computer Science at HU (Hadramout    Universit ...

  5. ACM: 限时训练题解-Street Lamps-贪心-字符串【超水】

    Street Lamps   Bahosain is walking in a street of N blocks. Each block is either empty or has one la ...

  6. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  7. [ACM] poj 1064 Cable master (二分查找)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 Des ...

  8. what the fuck!(二分查找 / 暴力模拟)

    what the fuck! Description 现在有一家公司有nnn个员工(nnn为奇数),他们的工资发放是基本工资+提成,现在这家公司计划再招一批人.要写一篇招聘启事,但是对于这个招聘启事中 ...

  9. 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找

    C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. C#学习笔记----.net操作进程

    进程(Process)是Windows系统中的一个基本概念,它包含着一个运行程序所需要的资源.进程之间是相对独立的,一个进程无法直接访问另一个进程的数据(除非分布式),一个进程运行的失败也不会影响其他 ...

  2. Java集合源码学习(四)HashMap分析

    ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...

  3. Elasticsearch在Windows下的安装

    下载Elasticsearch,地址:elasticsearch.org/download 下载jdk,百度搜索jdk下载即可 配置JAVA_HOME变量,配置方法在此文:http://jingyan ...

  4. 【PHP&&mysqli】

    msyqli和mysql只有一个字母的差别,真正的含义是msyql的增强版扩展. MySQL可以处理满足程序员对MySQL数据库操作的各种需要了,为什么还需要mysqli呢?因为mysqli支持面性对 ...

  5. AOP常用术语

    1.连接点(Joinpoint) 程序执行的某个特定位置:如类开始初始化前,类初始化后,类某个方法调用前,调用后,方法跑出异常后.一个类或一段程序代码拥有一些具有边界性质的特定点.这些代码中的特定点就 ...

  6. HP SAN Switch參考文檔地址

    HP SAN Switch其實也是基於Linux內核 http://h20566.www2.hpe.com/portal/site/hpsc/template.PAGE/public/psi/manu ...

  7. git checkout 命令详解

    转自:http://www.cnblogs.com/hutaoer/archive/2013/05/07/git_checkout.html?utm_source=tuicool&utm_me ...

  8. mybatis 中#和$的区别

    #{…}是一个参数标记,将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是1,那么解析成sql时的值为order by " ...

  9. 高效jQuery的奥秘

    讨论jQuery和javascript性能的文章并不罕见.然而,本文我计划总结一些速度方面的技巧和我本人的一些建议,来提升你的jQuery和javascript代码.好的代码会带来速度的提升.快速渲染 ...

  10. POJ 3140 Contestants Division 树形DP

    Contestants Division   Description In the new ACM-ICPC Regional Contest, a special monitoring and su ...