The Factor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2642 Accepted Submission(s): 796
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
2 3 1 2 3 5 6 6 6 6 6
Sample Output
6 4
题目大意:
有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.
思路:
1、因为数据范围不大,所以我们暴力拆分每个数的因子,得到一个数组yinzi【i】;
2、对于合法的解,无非也就两种情况:
①这是一个合数因子。
②这是一个由几个素数因子相乘得到的。
③那么显然,两个素数相乘一定是合数,那么没有必要枚举多个素数因子相乘,我们只要维护出来两个最小素数因子记录下来即可。
3、ans=min(两个素数因子相乘,一个最小的合数因子);
4、注意,不要提前break;
Ac代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<set>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll __int64
ll a[150];
ll yinzi[150000000];
ll b[5];
ll judge(ll num)
{
if(num==1||num==0||num==2||num==3)return 0;
for(int i=2;i<=sqrt(num);i++)
{
if(num%i==0)return 1;
}
return 0;
}
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
for(ll i=0; i<n; i++)
{
cin>>a[i];
}
int contz=0;
for(ll i=0; i<n; i++)
{
for(int j=1; j<=sqrt(a[i]); j++)
{
if(a[i]%j==0)
{
yinzi[contz++]=j;
yinzi[contz++]=a[i]/j;
}
}
}
sort(yinzi,yinzi+contz);
int cont=0;
ll output=4000000000060000000;
for(int i=0;i<contz;i++)
{
if(yinzi[i]<=1)continue;
if(judge(yinzi[i])==1)
{
output=min(output,yinzi[i]);
}
else
{
if(cont==2)continue;
b[cont++]=yinzi[i];
}
}
if(cont==2)
output=min(output,b[0]*b[1]);
if(output==4000000000060000000)
{
cout<<"-1"<<endl;
continue;
}
cout<<output<<endl;
}
}