BC水题--The Factor(质因分解)
网址:http://acm.hdu.edu.cn/showproblem.php?pid=5428
roblem Description
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.
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.
answers in T
lines.
2
3
1 2 3
5
6 6 6 6 6
6
4
#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(质因分解)的更多相关文章
- Miller_Rabbin算法判断大素数,Pollard_rho算法进行质因素分解
Miller-rabin算法是一个用来快速判断一个正整数是否为素数的算法.它利用了费马小定理,即:如果p是质数,且a,p互质,那么a^(p-1) mod p恒等于1.也就是对于所有小于p的正整数a来说 ...
- 【省选水题集Day1】一起来AK水题吧! 题目(更新到B)
题解:http://www.cnblogs.com/ljc20020730/p/6937954.html 水题A: [AHOI2001]质数和分解 题目网址: https://www.luogu.or ...
- 【省选水题集Day1】一起来AK水题吧! 题解(更新到B)
题目:http://www.cnblogs.com/ljc20020730/p/6937936.html 水题A:[AHOI2001]质数和分解 安徽省选OI原题!简单Dp. 一看就是完全背包求方案数 ...
- hdu 2710 Max Factor 数学(水题)
本来是不打算贴这道水题的,自己却WA了三次.. 要考虑1的情况,1的质因子为1 思路:先打表 ,然后根据最大质因子更新结果 代码: #include<iostream> #include& ...
- gdutcode 1195: 相信我这是水题 GDUT中有个风云人物pigofzhou,是冰点奇迹队的主代码手,
1195: 相信我这是水题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 821 Solved: 219 Description GDUT中有个风云人 ...
- HDU 2674 N!Again(数学思维水题)
题目 //行开始看被吓一跳,那么大,没有头绪, //看了解题报告,发现这是一道大大大的水题,,,,,//2009 = 7 * 7 * 41//对2009分解,看它有哪些质因子,它最大的质因子是41,那 ...
- hdu 1164:Eddy's research I(水题,数学题,筛法)
Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- [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 ...
- CF451C Predict Outcome of the Game 水题
Codeforces Round #258 (Div. 2) Predict Outcome of the Game C. Predict Outcome of the Game time limit ...
随机推荐
- mysql 查看最大连接数 设置最大连接数
方法一:进入MYSQL安装目录 打开MYSQL配置文件 my.ini 或 my.cnf查找 max_connections=100 修改为 max_connections=1000 服务里重起MY ...
- Java自带工具jstack故障分析的一个案例
公司的一个web应用项目运行了很长一段时间,达半年之久,前段时间突然出现了服务不可用的情况,所有的请求都不可达,服务彻底挂了.查看tomcat进程还在,cpu使用率低,一时没找着问题,重启了服务.过了 ...
- R(三): R包原理及安装
包(package)是多个函数的集合,常作为分享代码的基本单元,代码封装成包可以方便其他用户使用.越来越多的R包正在由世界上不同的人所创建并分发,这些分发的R包,可以从CRAN 或 github 上获 ...
- Oracle常用知识小结
前言: 前一段时间项目开发数据库环境为Oracle,作为一个SQLer,表示各种不适应.所以刚开始的时候走了一些弯路,浪费了一席时间.因此就想把这些常用的东西给总结一下,算是对自己学习的总结,也希望能 ...
- CentOS下的网络配置文件说明
CentOS网络配置主要涉及到以下四个文件: 1./etc/sysconfig/network 在CentOS官方网站上给出了如下说明: The/etc/sysconfig/networkfile i ...
- JAVA获取当前时间加一天
01.获取当前时间 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.for ...
- 8张图带你深入理解Java
1.字符串的不变性 下图展示了如下的代码运行过程: String s = "abcd";s = s.concat("ef"); 备注:String refe ...
- Learning Puppet — Resource Ordering
Learning Puppet — Resource Ordering Learn about dependencies and refresh events, manage the relation ...
- WIN 8.1 x64 环境下 COM Surrogate 停止工作解决方案
我的笔记本电脑是THINKPAD T420 4180AT8,NVIDIA NVS 4200M,Intel(R) HD Graphics 3000,WIN 8.1 x64操作系统.在安装完NVIDIA独 ...
- [svn]svn merge
转:http://blog.csdn.net/keda8997110/article/details/21813035 Step by Step 完成merge 目录: Branch的必要性 1.本地 ...