Problem 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
 

对于每一个数字,它有用的部分其实只有它的所有质因子(包括相等的)。求出所有数的所有质因子中最小的两个,相乘就是答案。如果所有数字的质因子个数不到两个,那么就是无解。时间复杂度

O(n*sqrt(a))O(n∗sqrt(a))。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
multiset<ll> s;
multiset<ll>::iterator it,it1;
int main()
{
int t;
scanf("%d",&t);
while(t--){
s.clear();
ll n;
scanf("%I64d",&n);
for(ll i=;i<n;i++){
ll m;
scanf("%I64d",&m);
for(ll j=;j*j<=m;j++){
if(m%j==){
while(m%j==){
s.insert(j);
m/=j;
}
}
}
if(m>) s.insert(m);
}
ll size=s.size(); if(size<) printf("-1\n");
else{
it=s.begin();
ll ans1=(*it);
it++;
ll ans2=(*it);
printf("%I64d\n",ans1*ans2);
}
}
return ;
}

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 分解质因数

    The Factor  Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest ...

  3. HDU 5428 The Factor

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

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

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

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

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

  6. HDU 5428:The Factor

    The Factor  Accepts: 101  Submissions: 811  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65 ...

  7. HDU 5428 分解质因数

                                                                                                   The F ...

  8. HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spheric ...

  9. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

随机推荐

  1. JAVA获取oracle中sequences的最后一个值

    项目中,用到一个序列作单号,框架用的是ssh,在dao层去拿的时候,运行时报错为dual is not mapped,[select *.nextval nextvalue from dual] 后来 ...

  2. Android之Intent

    前言:大家都知道Android程序的实现一般都由四大组件构成: Activity :Android程序实现功能的主体,提供了和客户交互的界面,也提供了和后台交互的功能. Service :是一个没有界 ...

  3. mysql 存储过程 计算报表

    把用例执行情况mysql表汇总起来 proc_write_report 汇总执行用例表中的测试数据 写入report 表,report表包括字段 report_id(自增)execution_flag ...

  4. Windows下命令行直接编译程序

    D:\> cl hello.cpp Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Cop ...

  5. [Redux] Extracting Presentational Components -- Todo, TodoList

    Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...

  6. php:兄弟连之面向对象版图形计算器1

    曾经看细说PHP的时候就想做这个,可是一直没什么时间,这次总算忙里偷闲搞了代码量比較多的project. 首先,文档结构,都在一个文件夹下就好了,我的就例如以下. 一開始,进入index.php文件. ...

  7. 多路复用I/O select()

    select(),poll(),epoll()的总结:http://www.cnblogs.com/Anker/p/3265058.html 在socket编程中,仅仅使用connect,accept ...

  8. c# 鼠标操作

    1#region 3using System; 4using System.Runtime.InteropServices; 6#endregion 8namespace Windows.Forms. ...

  9. JS 图片预览功能

    <script type="text/javascript">    function DisplayImage(fileTag) {        document. ...

  10. iOS9适配+warning消除

    最近做了iOS 9的适配,程序出现大量警告也做了些处理,写出来分先给大家. 一.iOS 9适配 问题一: <Error>: CGContextSaveGState: invalid con ...