原题链接:https://codeforces.com/contest/1165/problem/D
样例:
Input
2
8
8 2 12 6 4 24 16 3
1
2
Output
48
4
解题思路: 首先对于我们要求的它必需满足这个n长的序列是这个
的因子,其次这些因子一定要是
的所有因子(不包括1和它本身。)。其次在这个序列中
一定是等于最小的数乘以最大的数。再这样配对过去,所以我们要先对序列进行排序,之后再判断这些因子满不满足要求,最后再统计我们求得的结果
的因子是不是满足
个。
AC代码:
/*
*
*/
//POJ不支持
//i为循环变量,a为初始值,n为界限值,递增
//i为循环变量, a为初始值,n为界限值,递减。
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e3;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
ll t,n;
while(cin>>t){
while(t--){
ll temp1;
ll nums[maxn];
cin>>n;
rep(i,1,n){
cin>>nums[i];
}
sort(nums+1,nums+n+1);
temp1=nums[1]*nums[n];
bool flag=false;
for(ll i=2;i<=(n+1)/2;i++){
if(nums[i]*nums[n-i+1]!=temp1){
flag=true;
break;
}
}
if(flag){
cout<<"-1"<<endl;
continue;
}
else{
int cnt=0;
//统计因子数是不是达到要求。
for(ll i=2;i*i<=temp1;i++){
if(temp1%i==0){
if(i*i==temp1)cnt++;//一个因子。
else cnt+=2;//两个因子。
}
}
if(cnt!=n)cout<<"-1"<<endl;
else cout<<temp1<<endl;
}
}
}
return 0;
}