HDU 5428 The Factor (素因数分解)
题意:给出n个数,问这n个数的乘积中至少有三个因子的最小因子。若不存在这样的因子,则输出 -1;
思路:求出每个数的最小的两个素因数,然后输出其中最小的两个数的乘积。
代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
#define INF 0x7fffffff
int isprime[100000],prime[100000],cnt = 0;
long long num[100000];
int init(){ // 求 1 - 100000内的素数 (100000*100000 > 1000000000)
int i,j;
memset(isprime,1,sizeof(isprime));
for(i=2;i<100000;i++){
if(!isprime[i])
continue ;
prime[cnt++] = i;
for(j =i+i;j<100000;j+=i)
isprime[j] = 0;
}
}
int main(){
int i,j,n,t,w,x,p;
init();
cin >> t;
while(t--){
w = 0;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&x);
p = 0;
for(j=0;j<cnt && x >= prime[j] && p<2;j++)
while(x%prime[j] == 0){
p++;
num[w++] = prime[j];
x /= prime[j];
if(p >=2 )
break;
}
if(x != 1)
num[w++] = x;
}
sort(num,num+w);
if(w < 2)
printf("-1\n");
else
printf("%I64d\n",num[0]*num[1]);
}
return 0;
}
HDU 5428 The Factor (素因数分解)的更多相关文章
- HDU 5428 The Factor 分解因式
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5428 The Factor Accepts: 101 Submissions: 811 Tim ...
- hdu 5428 The Factor 分解质因数
The Factor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/contest ...
- HDU 5428 The Factor
话说这题意真的是好难懂啊,尽管搜到了中文题意,然而还是没懂,最后看到了一个题解才懂的.http://www.cnblogs.com/Apro/p/4784808.html#3470972 题意:给出n ...
- hdu 5428 The Factor(数学)
Problem Description There is a sequence of n positive integers. Fancycoder is addicted to learn thei ...
- HDU 5428 分解质因数
The F ...
- HDU 5428:The Factor
The Factor Accepts: 101 Submissions: 811 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 4610 Cards (合数分解,枚举)
Cards Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- HDU 2639 01背包(分解)
http://acm.hdu.edu.cn/showproblem.php?pid=2639 01背包第k优解,把每次的max分步列出来即可 #include<stdio.h> #incl ...
随机推荐
- 【剑指offer】面试题37:两个链表的第一个公共结点
题目: 输入两个链表,找出它们的第一个公共结点. 思路: 由链表的定义知是单链表.对于单链表,如果两个链表有公共结点,则两个链表必然是像Y型相交.则先计算出各个链表的长度,让长链表的头指针先走多出来的 ...
- JAVA获取oracle中sequences的最后一个值
项目中,用到一个序列作单号,框架用的是ssh,在dao层去拿的时候,运行时报错为dual is not mapped,[select *.nextval nextvalue from dual] 后来 ...
- 源码分析之spring-JdbcTemplate日志打印sql语句
对于开源的项目来说的好处就是我们遇到什么问题可以通过看源码来解决. 比如近期有个同事问我说,为啥JdbcTemplate中只有在Error的时候才打印出sql语句呢.我一想,这和log的配置有关系吧. ...
- github atom 试用
github的编辑器atom 1.0已经出来了,在https://atom.io/ 我之前在win上一直用notepad++写了两年脚本.最近改写lua了,项目组统一用的sublime text.su ...
- 在Windows 下为PHP5.4安装PEAR, PHPUnit , phpDoc2
1.安装PEAR 官方网站: http://pear.php.net/ PHP 5.4 的 Windows 包中没有自带 PEAR,下载 http://pear.php.net/go-pear ...
- c++ 之 字符和字符串
字符 1.字符的分类 字符主要包括字母.数字.标点符号.控制字符等 在ASCII编码表中,每一个字符都用一个十进制数来表示 注:ASCII的全称是American Standard Code for ...
- itoa的源代码实现
由于通过socket传递数据的时候,仅仅能够通过字符串类型,可是,当我们要传递的数据是整型的是,应该怎么办呢?本来我想着使用for循环,可是,总感觉太麻烦了,后来别人告诉我能够使用itoa,以下是it ...
- Caused by: java.lang.ClassNotFoundException: javassist.ClassPool
1.错误原因 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- 在 VS2008 下操作 Excel 的方法总结
这些天做个软件,需要读取 Excel 并导入到数据库中,所以研究了一下在 VC 下操作 Excel 的方法,这里做个总结,以作备忘. 一.最常用的 OLE 自动化方式 这个方式应该说是功能最全的方 ...
- CentOS6.6(单用户模式)重设root密码
1.开机时手要快按任意键,因为默认时间5s 2.grub菜单,只有一个内核,没什么好上下选的,按e键.不过如果你升级了系统或安装了Xen虚拟化后,就会有多个显示了. 3.接下来显示如下,选择第二项,按 ...