Almost All Divisors(求因子個數及思惟)

---恢復內容開始---ios

We guessed some integer number xx. You are given a list of almost all its divisors. Almost all means that there are all divisors except 11and xx in the list.ide

Your task is to find the minimum possible integer xx that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number.spa

You have to answer tt independent queries.code

Input

The first line of the input contains one integer tt (1t251≤t≤25) — the number of queries. Then tt queries follow.blog

The first line of the query contains one integer nn (1n3001≤n≤300) — the number of divisors in the list.ci

The second line of the query contains nn integers d1,d2,,dnd1,d2,…,dn (2di1062≤di≤106), where didi is the ii-th divisor of the guessed number. It is guaranteed that all values didi are distinct.input

Output

For each query print the answer to it.string

If the input data in the query is contradictory and it is impossible to find such number xx that the given list of divisors is the list of almost allits divisors, print -1. Otherwise print the minimum possible xx.it

Example
input
Copy
2
8
8 2 12 6 4 24 16 3
1
2
output
Copy
48
4

 

 

 

思路:求出因子個數,看是否這n個數是否包含這n個因子數,而後判斷一下再判斷一下這n個數是不是他的因子io

代碼:

#include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<set> #include<vector> #include<map> #include<cmath>
const int maxn=1e5+5; typedef long long ll; using namespace std; ll count(ll n){ ll s=1; for(ll i=2;i*i<=n;i++){ if(n%i==0){ int a=0; while(n%i==0){ n/=i; a++; } s=s*(a+1); } } if(n>1) s=s*2; return s; } ll a[maxn]; int main() { int T; cin>>T; int n; while(T--) { scanf("%d",&n); ll maxx=2; ll minn=10000000; ll x; for(int t=0;t<n;t++) { scanf("%lld",&a[t]); maxx=max(a[t],maxx); minn=min(a[t],minn); } ll ans=maxx*minn; bool flag=false; for(int t=0;t<n;t++) { if(ans%a[t]!=0) { flag=true; } } if(count(ans)-2==n&&flag==false) printf("%lld\n",ans); else { printf("-1\n"); } } return 0; }

 

---恢復內容結束---

相關文章
相關標籤/搜索