Codeforces 1165D. Almost All Divisors Solution
Solve in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE
///Islamic University,Bangladesh
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
long long int n,ar[1000]= {0},i,v,t,c1=0,c2=0;
cin>>t;
while(t--)
{
cin>>n;
for(i=0; i<n; i++)
{
cin>>ar[i];
}
long long int x=0,m=0;
sort(ar,ar+n);
long long int ans=ar[0]*ar[n-1];
for(i=0; i<n; i++)
{
if(ans%ar[i]!=0)
{
x=1;
break;
}
}
for(i=2; i<=sqrt(ans); i++)
{
if((ans%i)==0)
{
if(i*i==ans)
m++;
else
m=m+2;
}
}
if((x==0)&&(m==n))
cout<<ans<<endl;
else
cout<<"-1"<<endl;
}
}
No comments