网址: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 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
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5431 5430 5429 5427 5426

#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\n", ((minfac1==INF||minfac2==INF) ? -1 : minfac1*minfac2) );
minfac1 = minfac2 = INF;
}
return 0;
}

BC水题--The Factor(质因分解)的更多相关文章

  1. Miller_Rabbin算法判断大素数,Pollard_rho算法进行质因素分解

    Miller-rabin算法是一个用来快速判断一个正整数是否为素数的算法.它利用了费马小定理,即:如果p是质数,且a,p互质,那么a^(p-1) mod p恒等于1.也就是对于所有小于p的正整数a来说 ...

  2. 【省选水题集Day1】一起来AK水题吧! 题目(更新到B)

    题解:http://www.cnblogs.com/ljc20020730/p/6937954.html 水题A: [AHOI2001]质数和分解 题目网址: https://www.luogu.or ...

  3. 【省选水题集Day1】一起来AK水题吧! 题解(更新到B)

    题目:http://www.cnblogs.com/ljc20020730/p/6937936.html 水题A:[AHOI2001]质数和分解 安徽省选OI原题!简单Dp. 一看就是完全背包求方案数 ...

  4. hdu 2710 Max Factor 数学(水题)

    本来是不打算贴这道水题的,自己却WA了三次.. 要考虑1的情况,1的质因子为1 思路:先打表 ,然后根据最大质因子更新结果 代码: #include<iostream> #include& ...

  5. gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,

    1195: 相信我这是水题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 821  Solved: 219 Description GDUT中有个风云人 ...

  6. HDU 2674 N!Again(数学思维水题)

    题目 //行开始看被吓一跳,那么大,没有头绪, //看了解题报告,发现这是一道大大大的水题,,,,,//2009 = 7 * 7 * 41//对2009分解,看它有哪些质因子,它最大的质因子是41,那 ...

  7. hdu 1164:Eddy's research I(水题,数学题,筛法)

    Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  9. CF451C Predict Outcome of the Game 水题

    Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit ...

随机推荐

  1. C#-提取网页中的超链接

    转载:http://www.wzsky.net/html/Program/net/26849.htmlusing System; using System.Xml; using System.Text ...

  2. kindle相关工具

    里是与 Kindle 电子书相关的工具软件.它们可以帮助我们解决在日常使用电子书时所可能遇到的问题,比如 kindle 管理工具.kindle 转换工具.kindle电子书制作工具.kindle 推送 ...

  3. apache commons vfs 文件夹监控

    package test.vfs; import java.io.File; import org.apache.commons.logging.Log; import org.apache.comm ...

  4. tree 查询出数据遍历tree

    $('#tree1').tree({ url:'${contextPath}/pedition/treelistJc.html?editionUid=${ formatEdition.ppmId}', ...

  5. 文件上传工具类 UploadUtil.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  6. WCF学习心得----(五)生成客户端

    WCF学习心得----(五)生成客户端 1.    通过Svcutil.exe工具直接生成客户端 1.1     将服务承载于IIS上 1.1.1 在IIS中新建网站,所示效果如下图: 1.1.2   ...

  7. Spring boot配合Spring session(redis)遇到的错误

    背景:本MUEAS项目,一开始的时候,是没有引入redis的,考虑到后期性能的问题而引入.之前没有引用redis的时候,用户登录是正常的.但是,在加入redis支持后,登录就出错!错误如下: . __ ...

  8. VBA 插入一行保留样式

    Rows(processingRow).Insert ' 在指定的行数processingRow处插入一行 Rows(processingRow - 1).Select ' 选择上一行的整行 Sele ...

  9. android学习笔记16——对话框

    android支持丰富的对话框,常用4中对话框: 1.AlertDialog: 2.ProgressDialog:进度对话框,这个对话框只是对进度条的封装 3.DatePickerDialog:日期选 ...

  10. 如何在VBA窗体中使用 DataGrid 控件?

    1.下载MSDATGRD.cab,下载路径如下:http://www.findthatzipfile.com/search-10500733-hZIP/winrar-winzip-download-m ...