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} + ...
随机推荐
- js时间与毫秒互相转换
1)日期转换为毫秒 如果格式是:yyyy/mm/dd hh:mm:ss可以直接转换.var oldTime = (new Date("2018/07/09 14:13:11")). ...
- 前端用node+mysql实现简单服务端
node express + mysql实现简单服务端前端新人想写服务端不想学PHP等后端语言怎么办,那就用js写后台吧!这也是我这个前端新人的学习成果分享,如有那些地方不对,请给我指出. 1.准备工 ...
- kbmMW功能 - kbmMWProcess单元(转帖)
此贴为转发红鱼儿的文章,原贴地址: https://www.cnblogs.com/kinglandsoft/p/kbmmw-features-5-kbmmwprocess-unit.html 在新的 ...
- 利用谷歌插件破解今日头条的新闻ajax参数加密,新手都能懂
最近在学习谷歌插件,想找个项目练练手,就拿今日头条开刀 首先访问地址是:https://www.toutiao.com/c/user/50025817786/#mid=50044041847 通过抓包 ...
- 领扣-无重复字符的最长子串-Python实现
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...
- Android Log类基本用法
Log类介绍: API for sending log output.Generally, use the Log.v() Log.d() Log.i() Log.w() and Log.e() me ...
- pytorch之Tensor
#tensor和numpy import torch import numpy as np numpy_tensor = np.random.randn(3,4) print(numpy_tensor ...
- 银行业务-Excel文件的拆分逻辑
一.问题: 随着银行业务数据量的急剧增加,原始的人力统计数据已经不能满足要求, 需要开发一款可以实现自动化数据统计的系统平台,进行数据的采集.加工.过滤.统计.预测 其中数据采集方式又以[Excel] ...
- 记boost在gcc的一个库链接问题generic_category()
报错大致如下: main.cpp:(.text+0x49): undefined reference to `boost::system::generic_category()'main.cpp:(. ...
- 基于 OpenCV 的人脸识别
基于 OpenCV 的人脸识别 一点背景知识 OpenCV 是一个开源的计算机视觉和机器学习库.它包含成千上万优化过的算法,为各种计算机视觉应用提供了一个通用工具包.根据这个项目的关于页面,OpenC ...