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 prime[MAXN+1];
void getprime(){
  memset(prime,0,sizeof(prime));
  for(int i=2;i<=MAXN;i++){
    if(!prime[i]) prime[++prime[0]]=i;
    for(int j=1;j<=prime[0]&&prime[j]*i<=MAXN;j++){
        prime[prime[j]*i]=1;
        if(i%prime[j]==0) break;
    }
  }
}
long long factor[100][2];
int cnt;
int getfactor(long long x){
  cnt=0;
  long long tmp=x;
  for(int i=1;prime[i]<=tmp/prime[i];i++){
    factor[cnt][1]=0;
    if(tmp%prime[i]==0){
        factor[cnt][0]=prime[i];
        while(tmp%prime[i]==0){
            factor[cnt][1]++;
            tmp/=prime[i];
        }
        cnt++;
    }
  }
  if(tmp!=1){
    factor[cnt][0]=tmp;
    factor[cnt++][1]=1;
  }
  return cnt;
}
int main(){
    int i,j,k;
    getprime();
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        vector<int> ans;
        for(i=0;i<n;i++){
            scanf("%d",&num[i]);
            getfactor(num[i]);
            for(j=0;j<cnt;j++){
                while(factor[j][1]--){
                    ans.push_back(factor[j][0]);
                }
            }
        }
        sort(ans.begin(),ans.end());
        if(ans.size()<2){
            printf("-1\n");
        }
        else{
            printf("%I64d\n",(long long)ans[0]*ans[1]);
        }
    }
    return 0;
}
时间: 2024-10-01 04:12:38

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 2161 Primes (素数筛选法)

题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; typedef long long LL; const int maxn = 16000 + 10; int p

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 5428 [The Factor] 分解质因数

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

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 5428 The Factor

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

hdu 5407 CRB and Candies(素数筛选法,除法取模(乘法逆元))

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5407 解题思路: 官方题解: The problem is just to calculate g(N) =\ LCM(C(N,0), C(N,1), ..., C(N, N))g(N) = LCM(C(N,0),C(N,1),...,C(N,N)). Introducing function f(n) =\ LCM(1, 2, ..., n)f(n) = LCM(1,2,...,n), the

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

HDU 1164 Eddy&#39;s research I【素数筛选法】

思路:将输入的这个数分成n个素数的相乘的结果,用一个数组存储起来.之后再输出就可以了 Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6633    Accepted Submission(s): 3971 Problem Description Eddy's interest is very ext