HDU 5428 The Factor

题意:给出n个数,问这n个数的乘积中最小的有至少三个因子的因子。

解法:除了1和质数的正整数都有至少三个因子,所以只要求那个乘积里最小的不为1的非质数因子就可以了,对每个数分解质因子,所有质因子中最小的两个之积即为答案,如果找不到两个质因子则不存在答案。注意longlong!注意longlong!注意longlong!重要的事情说三遍。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long

using namespace std;

int cnt = 0;
int prime[45000];
void init()
{
    bool isprime[45005] = {0};
    for(int i = 2; i <= 45000; i++)
    {
        if(!isprime[i])
        {
            prime[cnt++] = i;
            for(int j = i + i; j <= 45000; j += i)
                isprime[j] = 1;
        }
    }
}
int main()
{
    init();
    int T;
    while(~scanf("%d", &T))
    {
        while(T--)
        {
            int n;
            int a[105];
            vector <int> v;
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
            {
                scanf("%d", &a[i]);
                int j = 0;
                int x = a[i];
                int flag = 0;
                for(j = 0; j < cnt && a[i] >= prime[j]; j++)
                {
                    while(a[i] % prime[j] == 0)
                    {
                        v.push_back(prime[j]);
                        flag = 1;
                        a[i] /= prime[j];
                    }
                }
                if(!flag && x != 1)
                    v.push_back(x);
            }
            sort(v.begin(), v.end());
            //for(int i = 0; i < v.size(); i++)
                //cout << v[i] << endl;
            if(v.size() < 2) printf("-1\n");
            else printf("%lld\n", (LL)v[0] * v[1]);
        }
    }
    return 0;
}

  

时间: 2024-10-10 23:23:12

HDU 5428 The Factor的相关文章

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只需要找到这个巨大

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 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

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

HDU 2710 Max Factor 数论(水

Max Factor Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

hdu - 1627 Krypton Factor (dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1627 给定 n 和 L 找出第n个范围在0-L之内的字符串,字符串要求没有相邻的子串是相同的. 按照格式输出. 这个题的关键在于判断字符串是否是符合要求的字符串. 枚举字符串所有子串可能的长度,然后判断即可. #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include

HDU 2710 Max Factor (筛选求素数)

Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3995    Accepted Submission(s): 1301 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1

HDU 1627 Krypton Factor

回溯法:避免无用判断,强化回溯代码的实现过程 题目的大意就是以字典序为准,排列字符串,但要保证一个字符串中不包含相邻的重复子串. Problem Description For example, the sequence ABACBCBAD is easy, since it contains an adjoining repetition of the subsequence CB. Other examples of easy sequences are: BB ABCDACABCAB AB