网址:http://acm.hdu.edu.cn/showproblem.php?pid=5428
roblem 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 ofn (1≤n≤100) .
2. The second line containsn
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
2. The second line contains
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
Recommend
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <stdio.h> #include <math.h> #include <string> #include <queue> #include <string.h> #include <map> #include <set> #include <vector> #include <algorithm> #include <stdlib.h> using namespace std; #define eps 1e-8 #define INF 200000005 #define rd(x) scanf("%d",&x) #define rdLL(x) scanf("%I64d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define ll long long #define mod 998244353 #define maxn 100005 #define maxm 1000 #define minn 0.00000001; ll minfac1=INF,minfac2=INF; #define N 100010 bool isprm[N]; vector <int> vec; void isprime() { int i,j; int s,e=sqrt( double(N) )+1; //sqrt是对于double数开平方 memset(isprm,1,sizeof(isprm)); //prm[k++]=2; isprm[0] = isprm[1] = 0; for(i=4 ;i < N; i=2+i) isprm[i]=0; for(i=3;i<e;i=2+i) if(isprm[i]) for(s=i*2,j=i*i;j<N;j=j+s) isprm[j]=0; //因为j是奇数,所以+奇数后是偶数,不必处理 for(int i=2 ; i<N ; i++) if(isprm[i]) vec.push_back(i); } void getfac(int n) { for(int i=0 ; i < vec.size() ; i++) { int x=vec[i]; if( (x >= minfac1 && x >= minfac2) || n < x ) break; ///节省时间,去掉可以 while(n%x==0){ n=n/x; if(x<minfac1) minfac1=x; else if(x<minfac2) minfac2=x; } } if( n!=1 && (minfac1>n||minfac2>n) ) ///可能最后还剩下一个质数 千万不能忘 minfac1>minfac2 ? minfac1 = n : minfac2 = n; } int main () { isprime(); int Case,temp; rd(Case); while(Case--) { int n; rd(n); for(int i=0 ; i < n ; i++) { rd(temp); getfac(temp); } printf("%lld ", ((minfac1==INF||minfac2==INF) ? -1 : minfac1*minfac2) ); minfac1 = minfac2 = INF; } return 0; }