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 ...
随机推荐
- 111个知名Java项目集锦,包括url和描述
转:http://www.cnblogs.com/wangs/p/3282183.html 项目名称 项目描述 ASM Java bytecode manipulation framework A ...
- XAMPP 在windows下无法启动Apache解决方案
XAMPP 在windows下无法启动Apache解决方案 一.现象 XAMPP 点击Start Apache时出现如下错误 20:41:12 [Apache] Error: Apache shut ...
- [转] Neutron FWaaS
OpenStack Neutron FWaaS 学习 ( by quqi99 ) 作者:张华 发表于:2013-06-24 版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息 ...
- 轻量级开源内存数据库SQLite性能测试
[IT168 专稿]SQLite是一款轻型的数据库,它占用资源非常的低,同时能够跟很多程序语言相结合,但是支持的SQL语句不会逊色于其他开源数据库.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品 ...
- LinearLayout属性baselineAligned的作用及baseline
相信大家对LinearLayout已经相当熟悉,但你们是否了解它的属性baselineAligned呢? Android官方文档是这么描述的:
- iMac一体机安装苹果和Win7双系统
前几天,有个客户说有一苹果的一体机,想装苹果和Win7双系统.约好了时间,带上工具就过去了.去的路上,用手机上网查了一下苹果电脑装双系统的过程.虽然以前也有给苹果的电脑安装过双系统,但次数不多而且时间 ...
- android学习笔记21——消息提示Toast
消息提示可细分为两种:大量消息提示——当程序有大量图片.信息需要展示时,采用对话框消息提示: 小量消息提示——当程序只有少量信息需要呈现给用户时,采用轻量级的对话框——Toast; Toast ==& ...
- windows下制作u盘启动的工具
推荐rufus,快捷方便
- .NET中值得体验的精妙设计
转自: http://developer.51cto.com/art/201104/255455_all.htm .NET 是 Microsoft XML Web services 平台.MEF是.N ...
- (WPF) MVVM: ComboBox Binding, XML 序列化
基本思路还是在View的Xmal里面绑定ViewModel的属性,虽然在View的后台代码中也可以实现binding,但是还是在Xmal里面相对的代码量要少一些. 此例子要实现的效果就是将一个List ...