hdu 5428 质因子

问题描述
有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.

这个因子包含大于两个因子
也就是说必须包含三个因子可以为本身
求出所有数的所有质因子中最小的两个,相乘就是答案。
如果所有数字的质因子个数不到两个,那么就是无解


输入样例
2
3
1 2 3
5
6 6 6 6 6
输出样例
6
4

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <string>
 6 # include <cmath>
 7 # include <queue>
 8 # define LL long long
 9 using namespace std ;
10 
11 LL a[120] ;
12 LL vis[50000] ;
13 
14 int main ()
15 {
16     //freopen("in.txt","r",stdin) ;
17     int T ;
18     cin>>T ;
19     while(T--)
20     {
21         int n , i ;
22         int cnt = 0 ;
23         cin>>n ;
24         for (i = 0 ; i < n ; i++)
25             cin>>a[i] ;
26         memset(vis , 0 , sizeof(vis)) ;
27         for (i = 0 ; i < n ; i++)
28         {
29             LL x = a[i] ;
30             for (LL j = 2 ; j*j <= x ; j++)
31             {
32                 while(x%j==0)
33                 {
34                     vis[cnt++] = j ;
35                     x /= j ;
36                 }
37             }
38             if (x > 1)
39                 vis[cnt++] = x ;
40         }
41         sort(vis , vis+cnt) ;
42         if (cnt < 2)
43             cout<<-1<<endl ;
44         else
45             cout<<vis[0]*vis[1]<<endl ;
46 
47     }
48 
49     return 0 ;
50 }
View Code
原文地址:https://www.cnblogs.com/mengchunchen/p/4823632.html