The Factor 

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=628&pid=1001

Description

有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.

Input

输入文件的第一行有一个正整数T \ (1 \le T \le 15)T (1≤T≤15),表示数据组数。

接下去有TT组数据,每组数据的第一行有一个正整数n \ (1 \le n \le 100)n (1≤n≤100).

第二行有nn个正整数a_1, \ldots, a_n \ (1 \le a_1, \ldots ,a_n \le 2\times 10^9)a​1​​,…,a​n​​ (1≤a​1​​,…,a​n​​≤2×10​9​​), 表示这个数列。

Output

输出TT行TT个数表示每次询问的答案。

Sample Input

2
3
1 2 3
5
6 6 6 6 6

Sample Output

6
4

HINT

 

题意

给你一个n个数

有一个数是由这N个数乘起来的,然后让你输出这个数的不是素数的最小的因子

题解:

对于每一个数都分解质因数,然后取最小的两个乘起来就好了

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <bitset>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 110
#define eps 1e-9
int Num;
//const int inf=0x7fffffff; //§&szlig;§é§à§é¨f§3
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************** //****************************************************************
// Miller_Rabin 算法进行素数测试
//速度快,而且可以判断 <2^63的数
//****************************************************************
const int S=;//随机算法判定次数,S越大,判错概率越小 //计算 (a*b)%c. a,b都是long long的数,直接相乘可能溢出的
// a,b,c <2^63
long long mult_mod(long long a,long long b,long long c)
{
a%=c;
b%=c;
long long ret=;
while(b)
{
if(b&){ret+=a;ret%=c;}
a<<=;
if(a>=c)a%=c;
b>>=;
}
return ret;
} //计算 x^n %c
long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
if(n==)return x%mod;
x%=mod;
long long tmp=x;
long long ret=;
while(n)
{
if(n&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=;
}
return ret;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(long long a,long long n,long long x,long long t)
{
long long ret=pow_mod(a,x,n);
long long last=ret;
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;//合数
last=ret;
}
if(ret!=) return true;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false; bool Miller_Rabin(long long n)
{
if(n<)return false;
if(n==)return true;
if((n&)==) return false;//偶数
long long x=n-;
long long t=;
while((x&)==){x>>=;t++;}
for(int i=;i<S;i++)
{
long long a=rand()%(n-)+;//rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
} //************************************************
//pollard_rho 算法进行质因数分解
//************************************************
long long factor[];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组小标从0开始 long long gcd(long long a,long long b)
{
if(a==)return ;//??????
if(a<) return gcd(-a,b);
while(b)
{
long long t=a%b;
a=b;
b=t;
}
return a;
} long long Pollard_rho(long long x,long long c)
{
long long i=,k=;
long long x0=rand()%x;
long long y=x0;
while()
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
long long d=gcd(y-x0,x);
if(d!=&&d!=x) return d;
if(y==x0) return x;
if(i==k){y=x0;k+=k;}
}
}
//对n进行素因子分解 ll Div[];
int tot=; void findfac(long long n)
{
if(Miller_Rabin(n))//素数
{
Div[tot++]=n;
return;
}
long long p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& ll p[maxn];
int main()
{
int t=read();
while(t--)
{
int n=read();
memset(Div,,sizeof(Div));
tot=;
for(int i=;i<=n;i++)
{
scanf("%I64d",&p[i]);
if(p[i]!=)
findfac(p[i]);
}
sort(Div,Div+tot);
if(tot<=1)
printf("-1\n");
else
{
cout<<Div[]*Div[]<<endl;
//printf("%I64d\n",Div[0]*Div[1]);
}
}
}

hdu 5428 The Factor 分解质因数的更多相关文章

  1. HDU 5428 The Factor 分解因式

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5428 The Factor  Accepts: 101  Submissions: 811  Tim ...

  2. HDU 5428 The Factor (素因数分解)

    题意:给出n个数,问这n个数的乘积中至少有三个因子的最小因子.若不存在这样的因子,则输出 -1: 思路:求出每个数的最小的两个素因数,然后输出其中最小的两个数的乘积. 代码: #include< ...

  3. hdu2574 Hdu Girls' Day (分解质因数)

    Hdu Girls' Day Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. HDU 5428 The Factor

    话说这题意真的是好难懂啊,尽管搜到了中文题意,然而还是没懂,最后看到了一个题解才懂的.http://www.cnblogs.com/Apro/p/4784808.html#3470972 题意:给出n ...

  5. hdu 5428 The Factor(数学)

    Problem Description There is a sequence of n positive integers. Fancycoder is addicted to learn thei ...

  6. HDU 5428 分解质因数

                                                                                                   The F ...

  7. light oj 1236 分解质因数

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/H 题意:求满足1<=i<=j<=n ...

  8. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  9. POJ1811(SummerTrainingDay04-G miller-rabin判断素性 && pollard-rho分解质因数)

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 35528   Accepted: 9479 Case ...

随机推荐

  1. 【转】eclipse中egit插件使用

    原文网址:http://my.oschina.net/songxinqiang/blog/192567 eclipse和git这个两个工具的使用人数都是相当多的,在eclipse里面也有egit插件来 ...

  2. mysql大内存高性能优化方案

    mysql优化是一个相对来说比较重要的事情了,特别像对mysql读写比较多的网站就显得非常重要了,下面我们来介绍mysql大内存高性能优化方案 8G内存下MySQL的优化 按照下面的设置试试看:key ...

  3. 也说Autofac在MVC的简单实践:破解在Controller构造函数中的实例化 - winhu

    相信大家对Autofac并不陌生,很多人都在使用.本文只是介绍一下本人在使用时的一点想法总结. 在使用一个框架时,肯定要去它的官网查阅一下.autofac的官网给出了一些经典的使用案例.如注册容器: ...

  4. CF 577C Vasya and Petya's Game

    题意:一个游戏,A童鞋在1~n的范围里猜一个数,B童鞋询问一个集合,A童鞋要对集合里每个数做出回答,他猜的数能否给整除,B要通过这些答案得到A猜的数,最少需要猜哪些数? 解法:一个数可以由若干个质数的 ...

  5. HDU 5375 Gray code

    题意:给出一个二进制数,其中有些位的数字不确定,对于所有对应的格雷码,与一个序列a对应,第i位数字为1时得分a[i],求最大的得分. 解法:一个二进制数x对应的格雷码为x ^ (x >> ...

  6. java web 学习十四(JSP原理)

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  7. CABasicAnimation(CAKeyframeAnimation)keypath 取值

    - keyPath可以使用的key - #define angle2Radian(angle) ((angle)/180.0*M_PI) - transform.rotation.x 围绕x轴翻转 参 ...

  8. linux下配置双网卡及RAC规划——1

    使用背景: 操作系统:centos 虚拟机:virtualbox RAC系统中需要双网卡,一个为公共的网络环境,一个为私有的网络环境,从而需要搭建双网络. 在菜单FILE中选择preferences, ...

  9. 修改 myeclipse8.5 servlet 模板

    在myeclipse8.5的安装目录下找到 \Common\plugins下的com.genuitec.eclipse.wizards_8.5.0.zmyeclipse75020090612.jar ...

  10. Standalone HBase

    This is the default mode. Standalone mode is what is described in the quickstart section. In standal ...