首页 技术 正文
技术 2022年11月17日
0 收藏 890 点赞 4,744 浏览 1358 个字
  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;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290