VJ 补题
K. 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 Nnon-negative integers and an integer K, your task is to find two integers Xand Yfrom 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 Nspace-separated integers, each between 0and 100,000.
Output
For each test case, if there is no solution, print -1on a single line. Otherwise print a single line with two
space-separated integers X Y (X ≤ Y), where Xand Yare 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
612
362429
21
12
436
1218336
412
12612
26
-1
312
112
这个题目运行时间是关键
借鉴了大佬的代码:
#include
#include <bits/stdc++.h>
using namespace std;
#define maxn 111111
int main()
{
int t, n, k, num[maxn];
cin>>t;
while(t–)
{
memset(num,0,sizeof(num));
cin>>n>>k;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
num[x]++;
}
int flag = 1;
for(int i=1;ii<=k;i++)//ii<=k是关键,因为如果ii>k,要想找到j满足ij<k,那么j必然小于i,而小于i的情况i*j在前面已经完成,所以没必要在进行
{
if(num[i]&&k%i0)
{
if((ik/i&&num[i]>=2)||(i!=k/i&&num[k/i]))
{
cout<<i<<" “<<k/i<<endl;
flag = 0;
break;
}
}
}
if(flag)
cout<<”-1"<<endl;
}
return 0;
}
K. Runtime Error
最新推荐文章于 2023-01-04 15:58:34 发布