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 ...
随机推荐
- requireJS define require
原文地址:http://blog.csdn.net/xiaogou56a/article/details/21340213 define 用来定义模块 require 用来加载模块 1 因为定义一个模 ...
- "V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复,提高租房效率 | 36氪
"V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复,提高租房效率 | 36氪 "V租房"搭建微信租房平台,让租房人发起求租需求并接收合适房源回复 ...
- Entify Framewrok - 学习链接
http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading- ...
- 论 <%@taglib prefix="s" uri="/struts-tags" %> 的重要性
前段时间在做项目的时候,碰到这个问题 结果是相应的内容显示不出来,原来是忘了这句很关键的引入:<%@taglib prefix="s" uri="/struts-t ...
- js高级程序设计(第三版)学习笔记(第一版)
ecma:欧洲计算机制造商协会iso/iec:国际标准化和国际电工委员会 dom级别(10*)文档对象模型1:DOM核心(映射基于xml文档)与dom html(在dom核心基础上)2:对鼠标,事件, ...
- 原生javascript 改写的tab选项卡
<!--css部分--> <style> *{ margin: 0; padding: 0; } ul,li{ list-style: none } .tabbox{ widt ...
- 站点建设10个最好的响应的HTML5滑块插件
大多数的最佳响应的HTML5滑块插件能够使用移动应用程序,站点建设项目,以及Web开发项目提供一些令人兴奋的功能,如无限的动画效果,百分之中的一个百响应布局设计和很多其它. 1.别急!慢慢来 功能丰富 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- NSLog用法,打印日志
要输出的格式化占位: %@ 对象 %d, %i 整数 %u 无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e 浮点/双字 (科 ...
- 解决android自带textview右侧文字不能对齐的问题
package com.sixin.view; import android.content.Context; import android.graphics.Canvas; import andro ...