水题,只需要分解素因数,取最小的两个相乘即可,需要注意会爆int
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define drep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
bool vis [500500];
int p[500500],pn;
void init(){
rep(i,2,50000){
if(!vis[i]){
p[++pn]=i;
for(int j=i+i;j<=50000;j+=i)vis[j]=1;
}
}
//printf("%d\n",pn);
}
map<int,int> mp;
map<int,int> :: iterator it;
int cot [100100];
struct node{
int v,c;
}c[100100];
bool cmp(node x,node y){
return x.v<y.v;
}
int cnum;
main(){
init();
int T;
scanf("%d",&T);
rep(cas,1,T){
int n;
mp.clear();
rep(i,1,pn)cot[i]=0;
scanf("%d",&n);
rep(i,1,n){
int x;
scanf("%d",&x);
rep(j,1,pn){
while(x%p[j]==0)x/=p[j],cot[j]++;
}
if(x>1)mp[x]++;
}
cnum=0;
rep(i,1,pn){
if(cot[i]>0){
cnum++;
c[cnum].v=p[i];
c[cnum].c=cot[i];
}
}
for(it=mp.begin();it!=mp.end();it++){
int vvv=it->first;
int ccc=it->second;
if(ccc>0){
cnum++;
c[cnum].v=vvv;
c[cnum].c=ccc;
}
}
sort(c+1,c+1+cnum,cmp);
int sum=0;
rep(i,1,cnum){
// printf("%d %d\n",c[i].v,c[i].c);
sum+=c[i].c;
}
if(sum<2){
printf("-1\n");
continue;
}
if(c[1].c>=2){
printf("%lld\n",1LL*c[1].v*c[1].v);
}
else {
printf("%lld\n",1LL*c[1].v*c[2].v);
}
}
}