思路:约数是成对出现的,可以先记录最小和最大约数,求得可能的答案,再验证两个方面,判断答案的正确性:1.当前答案的约数个数是否等于给的约数。 2.给的数中是否出现不是当前答案的约数。
int main()
{
int n;
cin >> t;
while (t--)
{
int x;
int mx=0, mi=2e9;
cin >> n;
vector<int> tp;
f(i, 1, n)
{
scanf("%d", &x);tp.emplace_back(x);
mi = min(mi, x);
mx = max(mx, x);
}
ll ans = (ll)mx*mi;//因数都是成对的 ,理解因数的定义
vector<pii> v;
ll ans2 = ans;
ll cot;
f(j, 2, ans / j)
{
if (ans%j == 0)
{
cot = 0;
while (ans %j == 0)
{
ans /= j;
cot++;
}
v.emplace_back(make_pair(j, cot));//分解质因数 1e6
}
}
if (ans > 1)v.emplace_back(make_pair(ans, 1));//质数
int tmp = 1;
for (auto I : v)
tmp *= (1 + I.second);
if (tmp != n + 2)puts("-1");//少
else {//相同下,改了中间的
int fg = 1;
for (auto I : tp)
{
if (ans2%I != 0) { fg = 0;break; }
}
if (fg)cout << ans2 << endl;
else puts("-1");
}
}
return 0;
}