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 11and 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 allits 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个数是否包含这n个因子数,然后判断一下再判断一下这n个数是否是他的因子

代码:

#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;
}

---恢复内容结束---

原文地址:https://www.cnblogs.com/Staceyacm/p/10869742.html

时间: 2024-08-09 08:25:09

Almost All Divisors(求因子个数及思维)的相关文章

求因子个数和因子和

//求因子个数 int Facnt(int n) { int res = 1; for(int i=2;i*i<=n;i++) { if(n%i == 0) { int cnt = 0; do { n /= i; cnt++; }while(n%i==0); res *= (cnt+1); } } if(n > 1) res = 2*res; return res; } //求因子和 int Facsum(int n) { int res = 1; for(int i=2;i*i<=n;

Easy Number Challenge(暴力,求因子个数)

Easy Number Challenge Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 236B Appoint description:  System Crawler  (2016-04-26) Description Let's denote d(n) as the number of divisors of a

数学题 求因子个数

小C的倍数问题 Accepts: 1990 Submissions: 4931 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 根据小学数学的知识,我们知道一个正整数x是3的倍数的条件是x每一位加起来的和是3的倍数.反之,如果一个数每一位加起来是3的倍数,则这个数肯定是3的倍数. 现在给定进制P,求有多少个B满足P进制下,一个正整数是B的倍数的

lightoj-1028 - Trailing Zeroes (I)(素数法求因子个数)

1028 - Trailing Zeroes (I) PDF (English) Statistics ForumTime Limit: 2 second(s) Memory Limit: 32 MBWe know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols

POJ-2992 Divisors---组合数求因子数目

题目链接: https://cn.vjudge.net/problem/POJ-2992 题目大意: 给出组合数Cnk,求出其因子个数,其中n,k不大于431,组合数的值在long long范围内 解题思路: 由于只有431种阶乘,先预处理431中素数,再预处理出每一个阶乘里面所含的素因子的指数,然后对于组合数,直接用素因子指数相减即可. 求出的质因子指数,就可以用定理直接求因子个数. 1 #include<iostream> 2 #include<cstdio> 3 #inclu

hdu 6069 Counting Divisors(求因子的个数)

Counting Divisors Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 3170    Accepted Submission(s): 1184 Problem Description In mathematics, the function d(n) denotes the number of divisors of

POJ 2992 Divisors 求组合数因子个数

题目来源:POJ 2992 Divisors 题意:... 思路:素数分解的唯一性 一个数可以被分解成若干素数相乘 p1^x1*p2^x2*...*pn^xn 根据乘法原理 因子数为 (x1+1)*(x2+1)*...*(xn+1) 不能直接求出组合数 会溢出 也不能把每个乘的数分解因子 这样会超时 C(N,M)=N!/(M!*(N-M)!) 另dp[i][j] 代表为i的阶乘中j因子的个数(j是素数) 那么i素数的个数为dp[n][i]-dp[m][i]-dp[n-m][i] 最后for循环从

AtCoder Beginner Contest 142【D题】【判断素数的模板+求一个数的因子的模板】

D - Disjoint Set of Common Divisors Problem Statement Given are positive integers AA and BB. Let us choose some number of positive common divisors of AA and BB. Here, any two of the chosen divisors must be coprime. At most, how many divisors can we c

Acdream1084 寒假安排 求n!中v因子个数

题目链接:点击打开链接 寒假安排 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 128000/64000 KB (Java/Others) SubmitStatistic Next Problem Problem Description 寒假又快要到了,不过对于lzx来说,头疼的事又来了,因为众多的后宫都指望着能和lzx约会呢,lzx得安排好计划才行. 假设lzx的后宫团有n个人,寒假共有m天,而每天只能跟一位后宫MM约会,并且由于后宫