The Factor HDU - 5428(分解质因数)
传送门
题目大意:
输入一个序列,求他们所有数的乘积,然后找到最后的这个数的能整除它的最小因子。条件限制,这个最小因子必须满足有两个以上的数能整除它,包括它自己。数据范围 1—2e9
众所周知(口胡开始),一个数能分解为若干个质数相乘(1除外),这道题把序列的每个数分解为最小质因数,用数组存起来,最后排个序,把前两个相乘即可,如果数组长度为一,输出“-1”,即不成立
#include <iostream>
#include <cstdio>
#include<cstdlib>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include<time.h>
#include <stack>
#include <list>
#include <set>
#include <sstream>
#include <iterator>
using namespace std;
#define FOPI freopen("input.in", "r", stdin)
#define DOPI freopen("output.out", "w", stdout)
#define ll long long int
#define fro(i,a,n) for(ll i=a;i<n;i++)
#define pre(i,a,n) for(ll i=n-1;i>=a;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define fi first
#define se second
#define s_d(a) scanf("%d",&a)
#define s_lld(a) scanf("%lld",&a)
#define s_s(a) scanf("%s",a)
#define s_ch(a) scanf("%c",&a)
typedef pair<ll,ll> P;
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
const double PI = 3.1415926535897932;
const double EPS=1e-6;
const int INF=0x3f3f3f3f;
const int maxn = 2e5+100;
int lowbit(int x){return x&(-x);}
vector<ll> s;
void solve(ll n)
{
ll temp=sqrt(n);
for(int i=2;i<=temp;i++)
{
while(n%i==0)
{
n/=i;
s.push_back(i);
}
}
if(n!=1)
s.push_back(n);
}
int main()
{
//ios::sync_with_stdio(0);
ll t;
cin>>t;
while(t--)
{
s.clear();
ll a;
cin>>a;
fro(i,0,a)
{
ll b;
cin>>b;
solve(b);
}
sort(s.begin(),s.end());
if(s.size()<2)
cout<<-1<<endl;
else
{
ll aa=s[0]*s[1];
//cout<<1<<endl;
cout<<aa<<endl;
}
}
return 0;
}