Codeforces Round #560 (Div. 3) D Almost All Divisors

题目传送门

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 11 and xx in the list.

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.

You have to answer tt independent queries.

Input

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

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

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

Output

For each query print the answer to it.

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 all its divisors, print -1. Otherwise print the minimum possible xx.

Example

input

Copy

2
8
8 2 12 6 4 24 16 3
1
2

output

Copy

48
4

题目大意:给出n个数,问是否存在一个数m,使得这n个数是m除去1和它本身所有的因子,若存在输出m,否则输出-1

解题思路:昨天晚上做的时候以为是要求最小公倍数,然后就在一错再错的道路上越走越远。首先,将输入的n个数排序,得到排序后的a数组,如果存在这个数m那么m一定是虽大的数和最小的数的乘积,找到m以后就要判断输入的数是否是m所有的因子了。求出m除去1和它本身以外的所有因子存在另一个数组b中,同样对b数组排序,最后判断a数组和b数组是否完全一样即可,若完全一样,输出m,否则输出-1

AC代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define io ios::sync_with_stdio(0),cin.tie(0)
#define ms(arr) memset(arr,0,sizeof(arr))
#define mc(a,b) memcpy(a,b,sizeof(b))
#define inf 0x3f3f3f
#define fin freopen("in.txt", "r", stdin)
#define fout freopen("out.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ULL;
const int mod=1e9+7;
const int N=1e5+7;
int t,n;
ll a[500],b[500];
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
        sort(a,a+n);
        ll ans=a[0]*a[n-1];
        int cnt=0;
        for(ll i=2;i*i<=ans;i++){
            if(ans%i==0){
                b[cnt++]=i;
                if(i*i!=ans) b[cnt++]=ans/i;
            }
        }
        if(cnt!=n) printf("-1\n");
        else{
            sort(b,b+cnt);
            int f=0;
            for(int i=0;i<n;i++){
                if(b[i]!=a[i]){
                    f=1;
                    break;
                }
            }
            if(f==1) printf("-1\n");
            else printf("%I64d\n",ans);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值