网址: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. 用户编辑新建_AngularJS实现

    实现思路: 分步骤完成开发,逐渐添加功能:1.实现输出users对象.2.实现点击“编辑”按钮,在表单中显示firstname和lastname,并不可修改.3.实现“新建用户”和“编辑用户”的切换. ...

  2. Reporting Service 报表报 rsReportServerDatabaseError 错的解决方法

    一.打开CRM的报表出现“报表服务器数据库内出错.此错误可能是因连接失败.超时或数据库中磁盘空间不足而导致的. (rsReportServerDatabaseError) 获取联机帮助 对象名 'Re ...

  3. 将war文件解压到指定目录

    问:如何将.war文件解压到指定目录? 答:jar命令没有这样的选项. eg:将abc.war解压到当前文件夹? 答:进入目标文件即abc.war文件所在的文件夹,按住shift键并在该文件夹空白处点 ...

  4. CGI相关概念

    common gateway interface 通用网关接口 可以让客户端从浏览器向执行在服务器上的程序请求数据.CGI描述了客户端和服务器程序之间传输数据的一种标准. 编程语言perl是一种被广泛 ...

  5. C#写入登陆Cookies

    protected void Page_Load(object sender, EventArgs e) { //打开登录页面时获取客户端cookie值并写入前台控件中 HttpCookie cook ...

  6. xmodem, ymodem & zmodem

    这三个是modem文件传输协议. https://en.wikipedia.org/wiki/XMODEM https://en.wikipedia.org/wiki/YMODEM https://e ...

  7. Android Gradle实用技巧——APK文件名中加上SVN版本号,日期等

    有时候,我们会希望能把APK文件名上带上打包日期,打包时svn的版本号,应用版本号等.当然这些也可以手动添加,但是手动的话也未免太不优雅了,而且可能会出错. 利用Gradle,我们可以让打包出来的ap ...

  8. aptana studio 3 自动换行(无需插件)

    菜单-Window-Preferences-Aptana Studio-Editors,勾选“Enable word wrap”,然后重启编辑器.

  9. PLSQL_Oracle Trigger触发器的基本概念和用法

    2014-06-14 Created By BaoXinjian

  10. UVa 297 Quadtrees(树的递归)

    Quadtrees 四分树就是一颗一个结点只有4个儿子或者没有儿子的树 [题目链接]UVa 297 Quadtrees [题目类型]树的递归 &题意: 一个图片,像素是32*32,给你两个先序 ...