题目链接:http://codeforces.com/gym/100712/attachments
将各数字计数一下,然后枚举判断。注:x,y为0,x,y相等时的情况。
附上AC代码:
#include <bits/stdc++.h>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
const int maxn = 100005;
int num[maxn], cnt[maxn];
int n, k;
int main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &k);
memset(cnt, 0, sizeof(cnt));
for (int i=0; i<n; ++i){
scanf("%d", num+i);
++cnt[num[i]];
}
int ans = -1;
sort(num, num+n);
for (int i=0; i<n; ++i){
if (num[i] == 0)
continue;
if (k%num[i] != 0)
continue;
if (k/num[i]==num[i] && cnt[num[i]]>1){
ans = num[i];
break;
}
else if ((k/num[i])!=num[i] && cnt[k/num[i]]>0){
ans = num[i];
break;
}
}
if (ans != -1)
printf("%d %d\n", ans, k/ans);
else
printf("-1\n");
}
return 0;
}