HDU 5428 The Factor 分解因式
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5428
The Factor
有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大。幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个因子(包括它本身;比如,4有3个因子,因此它是满足这个要求的一个数)。你需要找到这个数字并输出它。但是我们知道,对于某些数可能没有这样的因子;在这样的情况下,请输出-1.
输入文件的第一行有一个正整数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)a1,…,an (1≤a1,…,an≤2×109), 表示这个数列。
输出TT行TT个数表示每次询问的答案。
2
3
1 2 3
5
6 6 6 6 6
6
4
题解:
对每个数分解素因子,找其中最小的两个(可以相等)相乘就是结果(这个数可以很大,所以要long long输出。
对一个小于等于n的数分解素因数的时间复杂度为sqrt(n)(你用<=sqrt(n)的数去筛,筛完之后最后的一个数要么是1要么是一个质数,这个很好证明),所以暴力解这道题的时间复杂度就为100*sqrt(2*10^9)<10^7,即时间复杂度为o(10^7),完全够解这道题。
代码1:
预处理,先筛出sqrt(n)以内的素数,再用这些素数去分解素因数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1e5 + ;
typedef long long LL; int n;
int p[maxn], tot;
int tot_f;
int fac[maxn]; int sift[maxn];
void prepare() {
//这个预处理能将时间复杂度降到o(100*sqrt(n)/(ln sqrt(n)))即o(10^6)
memset(sift, , sizeof(sift));
for (int i = ; i*i <= maxn; i++) {
if (sift[i] == ) {
for (int j = i * ; j <= maxn; j += i) {
sift[j] = ;
}
}
}
tot = ;
for (int i = ; i<maxn; i++) if (sift[i] == ) {
p[tot++] = i;
}
} void init() {
tot_f = ;
} int main() {
prepare();
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
while (n--) {
int x;
scanf("%d", &x);
for (int i = ; i<tot && p[i] < x; i++) {
while (x%p[i] == ) {
fac[tot_f++] = p[i];
x /= p[i];
}
}
if (x != ) fac[tot_f++] = x;
}
if (tot_f>) {
sort(fac, fac + tot_f);
printf("%lld\n", (LL)fac[] * fac[]);
}
else {
printf("-1\n");
}
}
return ;
}
代码2:
直接分解因式
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = 1e5 + ;
typedef long long LL; int n;
int fac[maxn],tot_f; void init() {
tot_f = ;
} int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
while (n--) {
int x;
scanf("%d", &x);
for (int i = ; i*i<=x; i++) {
while (x%i == ) {
fac[tot_f++] = i;
x /= i;
}
}
if (x != ) fac[tot_f++] = x;
}
if (tot_f>) {
sort(fac, fac + tot_f);
printf("%lld\n", (LL)fac[] * fac[]);
}
else {
printf("-1\n");
}
}
return ;
}
HDU 5428 The Factor 分解因式的更多相关文章
- 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 (素因数分解)
题意:给出n个数,问这n个数的乘积中至少有三个因子的最小因子.若不存在这样的因子,则输出 -1: 思路:求出每个数的最小的两个素因数,然后输出其中最小的两个数的乘积. 代码: #include< ...
- 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 4143 A Simple Problem 分解因式
求一个最小的正整数x,使得(y + x) (y - x) = n成立 考虑一下n的分解因式. 可能会想到枚举n的约数,那么a * b = n成立,取最小的x即可 但是要枚举到n / 2,这样会超时. ...
- HDU 5428 分解质因数
The F ...
- HDU 5428:The Factor
The Factor Accepts: 101 Submissions: 811 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- hdu2574 Hdu Girls' Day (分解质因数)
Hdu Girls' Day Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1299 基础数论 分解
给一个数n问有多少种x,y的组合使$\frac{1}{x}+\frac{1}{y}=\frac{1}{n},x<=y$满足,设y = k + n,代入得到$x = \frac{n^2}{k} + ...
随机推荐
- react-navigation的超级大坑
本文针对react-navigation^3.0.0版本,版本不对的话,请不要看本文,直接看官方英文文档 最近一直在学习RN,没找到什么好的视频,所以一直看文档,一路上来虽然遇到一些乱七八糟的bu ...
- python面试题之基础
一.基础语法 1. 输入与输出 1.1 代码中要修改不可变数据会出现什么问题? 抛出什么异常? (2018-3-29-lxy) 代码不会正常运行,抛出 TypeError 异常. 1.2a=1,b=2 ...
- thinkphp3.2+cropper上传多张图片剪切图片
实现效果截图 点加号可以继续上传第二张图片 代码部<--引入cropper相关文件--> <link rel="stylesheet" href="/h ...
- CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- 生产环境rails console spring自动启动的问题
在生产环境执行rails console没反应无法进入控制台,或者执行rails console的时候spring自动启动,导致所有的类名都无法识别,报错:NameError: uninitializ ...
- Python学习之——Python安装
环境:Centos6.5+python2.7.5 1.centons6.5系统中是已经安装了python的,先查看版本是不是需要的 python --version 2.安装一些必要的包,防止后面需要 ...
- 有限差分法解矩形波导内场值、截止频率 MATLAB
利用有限差分法,解矩形波导内场解和截止频率: 这里以解TM11模为例,利用双重迭代法,每4次场值,更新一次Kc: %% % 求矩形波导中TM11模 截面内场分布.截止频率kc和特性阻抗Zc % // ...
- WPF GDI+字符串绘制成图片(一)
原文:WPF GDI+字符串绘制成图片(一) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...
- mysql常用的命令
一.事件操作: (1)查看事件启动状态 show variables like 'event_scheduler'; select @@event_scheduler; (2)启动事件 set glo ...
- day1 HTML - <head>
1.html是什么? 超文本标记语言(Hypertext Markup Language,HTML) <!DOCTYPE html> <html lang="en" ...