hdu5428

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
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100002];
const int inf=2e9;

int main(){
	memset(a,1,sizeof(a));
	int i=2;
	vector<int> su;
	int limit=sqrt(inf)+1;
	while(i<limit){
		if(a[i]){
			for(int j=2;j*i<limit;++j){
				a[i*j]=0;
			}
		}	
		++i;
	}
	for(int i=2;i<limit;++i){
		if(a[i])
			su.push_back(i);
	}

	int t;
	cin>>t;
	
	// for(auto x:su) cout<<x<<"\t";
	while(t--){
		int n;
		cin>>n;
		priority_queue<int> q;
		q.push(inf);
		q.push(inf);
		for(int i=0;i<n;++i){
			int tem;
			cin>>tem;
			if(tem>1){
				// q.push(tem);
				// q.pop();
				int cnt=0,j=0;
				while(j<su.size() && su[j]<=tem){
					if(tem%su[j]==0){
						q.push(su[j]);
						q.pop();
						++cnt;
						if(cnt==2) break;
						tem/=su[j];
					}
					else ++j;
				}
				if(cnt<2 && tem>1){
					q.push(tem);
					q.pop();
				}
			}
		}
		long long x=q.top();
		q.pop();
		long long y=q.top();
		q.pop();
		if(x==inf)
			puts("-1");
		else
			cout<<x*y<<endl;
	}	
	return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章