HDU 5428 The Factor——BestCoder Round #54(div.1 div.2)

The Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
There is a sequence of  n  positive integers. Fancycoder is addicted to learn their product, but this product may be extremely huge! However, it is lucky that FancyCoder only needs to find out one factor of this huge product: the smallest factor that contains more than 2 factors(including itself; i.e. 4 has 3 factors so that it is a qualified factor). You need to find it out and print it. As we know, there may be none of such factors; in this occasion, please print -1 instead. 
 

Input
The first line contains one integer  T (1T15) , which represents the number of testcases. 

For each testcase, there are two lines:

1. The first line contains one integer denoting the value of  n (1n100) .

2. The second line contains  n  integers  a1,,an (1a1,,an2×109) , which denote these  n  positive integers. 
 

Output
Print  T  answers in  T  lines.
 

Sample Input
   
   
   
   
2 3 1 2 3 5 6 6 6 6 6
 

Sample Output
   
   
   
   
6 4
 

Source
BestCoder Round #54 (div.2)
 


/************************************************************************/

附上该题对应的中文题

The Factor

 
 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.
输入描述
输入文件的第一行有一个正整数T \ (1 \le T \le 15)T (1T15),表示数据组数。

接下去有TT组数据,每组数据的第一行有一个正整数n \ (1 \le n \le 100)n (1n100).

第二行有nn个正整数a_1, \ldots, a_n \ (1 \le a_1, \ldots ,a_n \le 2\times 10^9)a1,,an (1a1,,an2×109), 表示这个数列。
输出描述
输出TTTT个数表示每次询问的答案。
输入样例
2
3
1 2 3
5
6 6 6 6 6
输出样例
6
4
/****************************************************/

出题人的解题思路:

The Factor

对于每一个数字,它有用的部分其实只有它的所有质因子(包括相等的)。求出所有数的所有质因子中最小的两个,相乘就是答案。如果所有数字的质因子个数不到两个,那么就是无解。时间复杂度O(n*sqrt(a))O(nsqrt(a))

此题就是让我们求数列中所有元素的乘积的一个因子,该因子需满足有3个因子,即该因子除1和它本身以外还有一个因子
这题其实也不难,因为1这个因子是必定有的,我们只需对数列里的每个元素进行分解质因数,找到最小的两个质因子,那么两者的乘积即为所求解;

若数列中所有元素的质因子个数也达不到两个的话,那就是无解的,比如数列中的元素全为1,或除1之外仅有一个质因子

5

1 1 1 1 1

4

1 1 1 1 3

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 120;
const int inf = 2000000001;
const int mod = 2009;
int min1,min2;
void fun(__int64 s)
{
    for(__int64 i=2;i*i<=s;i++)
        while(s%i==0)
        {
            if(min1>i||min2>i)
            {
                if(min2>min1)
                    min2=i;
                else
                    min1=i;
            }
            s/=i;
        }
    if(s>1)
        if(min1>s||min2>s)
        {
            if(min2>min1)
                min2=s;
            else
                min1=s;
        }
}
int main()
{
    int t,n,i;
    __int64 s;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);min1=min2=inf;
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&s);
            fun(s);
        }
        if(min1==inf||min2==inf)
            puts("-1");
        else
            printf("%I64d\n",(__int64)min1*min2);
    }
    return 0;
}
菜鸟成长记


你可能感兴趣的:(ACM)