Problem Description
n positive integers. Fancycoder is addicted to learn their product, but this product may be extremely huge! However, it is lucky that FancyCoder only needs to find out one factor of this huge product: the smallest factor that contains more than 2 factors(including itself; i.e. 4 has 3 factors so that it is a qualified factor). You need to find it out and print it. As we know, there may be none of such factors; in this occasion, please print -1 instead.
Input
T (1≤T≤15), which represents the number of testcases.
For each testcase, there are two lines:
1. The first line contains one integer denoting the value of
n (1≤n≤100).
2. The second line contains
n integers
a1,…,an (1≤a1,…,an≤2×109), which denote these
n positive integers.
Output
T answers in T lines.
Sample Input
2
3
1 2 3
5
6 6 6 6 6
Sample Output
6 4
没什么好说的,直接把所有数都分解了,两个最小的乘积就是答案,没有就是-1,答案可能爆int
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1000005;
int T, n, m, x;
int flag[maxn], p[maxn], tot, c[maxn];
void pre()
{
tot = 0;
for (int i = 2; i < maxn; i++)
{
if (!flag[i]) p[tot++] = i;
for (int j = 0; j < tot&&p[j] * i < maxn; j++)
{
flag[p[j] * i] = 1;
if (i%p[j] == 0) break;
}
}
}
int main()
{
pre();
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
LL ans = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
for (int j = 0; j < tot && p[j] <= x; j++)
{
while (x%p[j] == 0) { c[ans++] = p[j]; x /= p[j]; }
}
if (x != 1) c[ans++] = x;
}
if (ans < 2) printf("-1\n");
else
{
sort(c, c + ans);
ans = c[0];
ans *= c[1];
printf("%I64d\n", ans);
}
}
return 0;
}