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 <= 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 better than others. In particular,
a cow whose serial number has the highest prime factor enjoys the highest social standing among all the other cows.

(Recall that a prime number is just a number that has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, is not).

Given a set of N (1 <= N <= 5,000) serial numbers in the range 1..20,000, determine the one that has the largest prime factor.

Input

* Line 1: A single integer, N

* Lines 2..N+1: The serial numbers to be tested, one per line

Output

* Line 1: The integer with the largest prime factor. If there are more than one, output the one that appears earliest in the input file.

Sample Input

4
36
38
40
42

Sample Output

38

题目大意:给你N个数,求这N个数中哪个数的最大素因子最大,输出这个数,如果有多种结果,输出靠前边的那个数。

本例写的代码中prime具有二义性,但是这样能够减少空间利用

#include<iostream>
using namespace std;
int prime[20010];
int getprime()
{
    prime[1]=1;
    for(int i=2;i<20010;++i)
    {
        if(prime[i]==0)
        {
            for(int j=i;j<20010;j+=i)
            {
                prime[j]=i;
            }
        }
    }
}
int main(int argc, char *argv[])
{
    //freopen("2710.in", "r",stdin);
    int MAX=0;
    getprime();
    int n;
    int a;
    int N;
    while(scanf("%d",&n)!=EOF)
    {
        MAX=0;
        while(n--){
            scanf("%d",&a);
            if(MAX<prime[a]){
                MAX=prime[a];
                N=a;
            }
        }
        printf("%d\n",N);
    }
    return 0;
}
时间: 2024-10-13 16:35:31

HDU 2710 Max Factor (筛选求素数)的相关文章

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

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

#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=20005; int a[maxn]; void isprime()//素数筛 { memset(a,0,sizeof(a)); for(int i=2;i<maxn;i++)//用a[]这个数组存的是i的最大素数值 if(a[i]==0) for(int j=i;j<max

抓其根本(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)

素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. 1 int prime() 2 { 3 for (int i=2; i*i<=n; i++) 4 { 5 if (n%i==0) //不是素数 6 return 1; //返回1 7 } 8 return 0; //是素数返回0 9 } 二.打表,将所有的素数一一列出,存在一个数组里. 详见代码. 1 void prime() 2 { 3 for (int i=2; i<20050; i++) //从2开始一个

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

HDU2710_Max Factor【水题】【筛法求素数】

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

Facebook Hacker Cup 2015 Round 1--Homework(筛选法求素数)

题意:给定A,B,K(A<=B)三个数,问在[A,B]范围内的数素数因子个数为K的个数. 题解:典型的筛选法求素数.首先建立一个保存素数因子个数的数组factorNum[],以及到n为止含有素数因子个数为k的二维数组sumNum[n][k]. factorNum可以由筛选法确定,初始化数组为0. 1. 从小到大遍历给定最大范围内的数,若遍历到数n时,factorNum[n]=0则说明这个数是素数(前面没有它的因子). 2. 然后通过增加n的倍数,来筛选出最大范围内含有素数因子n的数. sumNu

筛选法求素数

筛选法求素数,不断的用3,5,7,等素数作为筛子,筛除这些数的倍数,即将合数筛除.用辅助数组p记录数i是否是素数. vector<int> prime(int n) { vector<int> p(n+1); for(int i=2;i<=n;i+=2) { if(i%2==0&&i>2) p[i]=0; else p[i]=1; } for(int i=3;i<=(int)(sqrt((double)n));i+=2) { if(p[i]) fo