这题觉得挺有意思的,就写篇博客记录一下,主要还是学到了一些东西吧
结论:
1. 如果一个序列
a
a
a中的数是 一个数
x
i
x_i
xi的除1和本身的所有因子,那么这个
x
i
x_i
xi的值将为
m
i
n
(
a
)
∗
m
i
n
(
b
)
min(a)*min(b)
min(a)∗min(b)
2. 如何得到一个数的所有因子(2 <= i < x)
for(int i = 2; i < x; ++i){
if(!(x%i)){
++tot;
if(i*i!=x) ++tot;
}
}
本题思路: 首先把数读进来,然后排序,算出
x
=
m
i
n
(
a
)
∗
m
i
n
(
b
)
x = min(a)*min(b)
x=min(a)∗min(b)
然后判断是不是序列a中的每个数能被x整除,如果不能就出-1
然后再判断x的因子个数(除去n和1)是不是等于n
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MAX = 3e4+5;
ll arr[MAX];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T; cin >> T;
while(T--){
int n; cin >> n;
for(int i = 0; i < n; ++i){
cin >> arr[i];
}
sort(arr,arr+n);
ll ans = arr[0] * arr[n-1];
bool istrue = true;
for(int i = 0; i < n; ++i){
if(ans%arr[i]){
istrue = false;
break;
}
}
if(!istrue){
cout << "-1" << endl;
continue;
}
int tot = 0;
for(ll i = 2; i*i <=ans; ++i){
if(!(ans%i)){
++tot;
if(i*i!=ans) ++tot;
}
}
if(tot!=n){
cout << -1 << endl;
}
else cout << ans <<endl;
}
return 0;
}
···