The Factor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2969 Accepted Submission(s): 920
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 (1≤T≤15), 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 (1≤n≤100).
2. The second line contains n integers a1,…,an (1≤a1,…,an≤2×109), which denote these n positive integers.
For each testcase, there are two lines:
1. The first line contains one integer denoting the value of n (1≤n≤100).
2. The second line contains n integers a1,…,an (1≤a1,…,an≤2×109), which denote these n positive integers.
Output
Print
T answers in
T lines.
Sample Input
231 2 356 6 6 6 6
Sample Output
64
Source
问题链接:HDU5428 The Factor。
题意简述:
对于给出的一组数 找出这组数乘积中的一个最小的至少三个质因子的数。
问题分析:
其实就是最小的两个素数因子的乘积。
将各个数分别进行质因子分解,将质因子保存在数组中,排序后找到最小的两个乘积即可。
程序说明:
给出2个C++语言程序,前一个程序使用变量maxfact记录已经找到的最大因子,限制质因子分解计算,也许会快一些。
AC的C++语言程序如下:
/* HDU5428 The Factor */
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10000;
unsigned int fact[N], maxfact;
int fcount;
void solve(unsigned int x)
{
for(int i=2; i*i<=x; i++) {
if(i >= maxfact && fcount >= 2)
break;
while(x % i == 0) {
fact[fcount++] = i;
x /= i;
if(i > maxfact)
maxfact = i;
}
}
if(x > 1) {
if(x >= maxfact && fcount >= 2)
;
else {
fact[fcount++] = x;
if(x > maxfact)
maxfact = x;
}
}
}
int main()
{
int t, n;
unsigned int a;
cin >> t;
while(t--) {
cin >> n;
fcount = 0;
maxfact = 0;
while(n--) {
cin >> a;
solve(a);
}
sort(fact, fact + fcount);
if(fcount < 2)
cout << -1 << endl;
else
cout << (long long)fact[0] * fact[1] << endl;
}
return 0;
}
AC的C++语言程序如下:
/* HDU5428 The Factor */
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10000;
unsigned int fact[N] ;
int fcount;
void solve(unsigned int x)
{
for (unsigned int i=2; i*i<=x; i++) {
while(x % i == 0) {
fact[fcount++] = i;
x /= i ;
}
}
if (x > 1)
fact[fcount++] = x;
}
int main()
{
int t, n;
unsigned int a;
cin >> t ;
while(t--) {
cin >> n ;
fcount = 0 ;
while(n--) {
cin >> a;
solve(a);
}
sort(fact, fact + fcount);
if (fcount < 2)
cout<< -1 << endl;
else
cout << (long long) fact[0] * fact[1] << endl;
}
return 0 ;
}