HDU 5428 The Factor 分解因式

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5428

The Factor

Accepts: 101

Submissions: 811

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

问题描述

有一个数列,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)a?1??,…,a?n?? (1≤a?1??,…,a?n??≤2×10?9??), 表示这个数列。

输出描述

输出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)以内的素数,再用这些素数去分解素因数。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn = 1e5 + 10;
 8 typedef long long LL;
 9
10 int n;
11 int p[maxn], tot;
12 int tot_f;
13 int fac[maxn];
14
15
16 int sift[maxn];
17 void prepare() {
18     //这个预处理能将时间复杂度降到o(100*sqrt(n)/(ln sqrt(n)))即o(10^6)
19     memset(sift, 0, sizeof(sift));
20     for (int i = 2; i*i <= maxn; i++) {
21         if (sift[i] == 0) {
22             for (int j = i * 2; j <= maxn; j += i) {
23                 sift[j] = 1;
24             }
25         }
26     }
27     tot = 0;
28     for (int i = 2; i<maxn; i++) if (sift[i] == 0) {
29         p[tot++] = i;
30     }
31 }
32
33 void init() {
34     tot_f = 0;
35 }
36
37 int main() {
38     prepare();
39     int tc;
40     scanf("%d", &tc);
41     while (tc--) {
42         init();
43         scanf("%d", &n);
44         while (n--) {
45             int x;
46             scanf("%d", &x);
47             for (int i = 0; i<tot && p[i] < x; i++) {
48                 while (x%p[i] == 0) {
49                     fac[tot_f++] = p[i];
50                     x /= p[i];
51                 }
52             }
53             if (x != 1) fac[tot_f++] = x;
54         }
55         if (tot_f>1) {
56             sort(fac, fac + tot_f);
57             printf("%lld\n", (LL)fac[0] * fac[1]);
58         }
59         else {
60             printf("-1\n");
61         }
62     }
63     return 0;
64 }

代码2:

  直接分解因式

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int maxn = 1e5 + 10;
 8 typedef long long LL;
 9
10 int n;
11 int fac[maxn],tot_f;
12
13 void init() {
14     tot_f = 0;
15 }
16
17 int main() {
18     int tc;
19     scanf("%d", &tc);
20     while (tc--) {
21         init();
22         scanf("%d", &n);
23         while (n--) {
24             int x;
25             scanf("%d", &x);
26             for (int i = 2; i*i<=x; i++) {
27                 while (x%i == 0) {
28                     fac[tot_f++] = i;
29                     x /= i;
30                 }
31             }
32             if (x != 1) fac[tot_f++] = x;
33         }
34         if (tot_f>1) {
35             sort(fac, fac + tot_f);
36             printf("%lld\n", (LL)fac[0] * fac[1]);
37         }
38         else {
39             printf("-1\n");
40         }
41     }
42     return 0;
43 }

The Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2126    Accepted Submission(s): 629

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

时间: 2024-11-02 02:03:02

HDU 5428 The Factor 分解因式的相关文章

HDU 5428 [The Factor] 分解质因数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5428 题目大意:给你若干个整数,让你输出这些数乘积的一个最小因子,并且这个因子至少有3个因子. 关键思想:分解质因数,我们要找的其实就是两个最小质因数的乘积.我的算法关键就是维护最小的两个质因数. 代码如下: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> u

hdu 5428 The Factor(素数筛选合数分解)

题意:求一串数乘积的因子中的最小合数: 思路:比赛时枚举因子,枚举到两个时结束,估计超时,结果果然被叉了: 将每个数分解,取最小的两个质因子,若数目少于2,则不存在: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; int t,n,m; int num[505]; const int MAXN=100000; int pr

HDU 5428 The Factor

题意:给出n个数,问这n个数的乘积中最小的有至少三个因子的因子. 解法:除了1和质数的正整数都有至少三个因子,所以只要求那个乘积里最小的不为1的非质数因子就可以了,对每个数分解质因子,所有质因子中最小的两个之积即为答案,如果找不到两个质因子则不存在答案.注意longlong!注意longlong!注意longlong!重要的事情说三遍. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #inclu

HDU 3988 n!质因数分解

Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2324    Accepted Submission(s): 569 Problem Description iSea is tired of writing the story of Harry Potter, so, l

hdu 2710 Max Factor(找最大素数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unawa

一个数分解因式

import java.util.Scanner; public class Demo1 { public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.println("请输入一个数"); int num=input.nextInt(); String str=""; int i; for(i=2;i<num;i++){ if(num%i==

HDU 5317 RGCDQ (合数分解+预处理)

题目链接:HDU 5317 RGCDQ 题意:定义函数F(x)为x的不同的素因子且小于等于x的个数,询问[l,r]区间中gcd(F(i),F(j))的最大值. 思路:暴力预处理出所有的合数分解结果,发现F(x)最大也只有7,之后就是暴力求出所有1到7出现次数的前缀和.询问的时候就打到O(1)了. AC代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; #

hdu 5108(数论-整数分解)

Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1847    Accepted Submission(s): 629 Problem Description Alexandra has a little brother. He is new to programming. One

HDOJ/HDU 2710 Max Factor(素数快速筛选~)

Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as be