Almost All Divisors(积性函数之因子个数)

本篇博客介绍了一种算法,用于解决给定一个数的大部分因子(除1和自身外)列表,如何找到这个数的最小可能值x。通过质因数分解和因子个数的计算,算法验证了输入数据的一致性并找到了满足条件的最小x值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Your task is to find the minimum possible integer x 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 t independent queries.

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

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

The second line of the query contains n integers d1,d2,…,dn (2≤di≤106), where di is the i-th divisor of the guessed number. It is guaranteed that all values di 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 x that the given list of divisors is the list of almost all its divisors, print -1. Otherwise print the minimum possible x.

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

求一个数n因子的个数f(n)是一个积性函数,如果n可以表示为
n=x1p1+x2p2+x3p3+...+xmpmn=x1^{p1}+x2^{p2}+x3^{p3}+...+xm^{pm}n=x1p1+x2p2+x3p3+...+xmpm(其中x1,x2…为n的质因子)
则:
f(n)=(p1+1)∗(p2+1)∗...∗(pm+1)f(n)=(p1+1)* (p2+1)*... *(pm+1)f(n)=(p1+1)(p2+1)...(pm+1)
由此用质因子分解算法即可求出f(n);

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn=350;
int N,t;
ll a[maxn];
int f(ll n)//求因子个数(包含1和n本身)
{
    int sum=1;
    for(ll i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            int p=0;
            while(n%i==0){
                n/=i;
                p++;
            }
            sum*=(p+1);
        }
    }
    if(n>1)sum*=2;
    return sum;
}
int main()
{
    ios::sync_with_stdio(0);
    cin>>t;
    while(t--)
    {
    	ll ans=0;
        cin>>N;
        for(int i=0;i<N;i++)
            cin>>a[i];
        sort(a,a+N);
        ans=a[0]*a[N-1];
        int f=0,p=-1;
        for(int i=0;i<N;i++)
        {
            if(ans%a[i]!=0){
                f=1;break;
            }
        }
        if(f==1)cout<<p<<endl;
        else {
            if(f(ans)==N+2)cout<<ans<<endl;
            else cout<<p<<endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值