Hdu2710_max Factor "Water Problem" "sieve method to find prime number"

Source: Internet
Author: User

Max Factor

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 3966 Accepted S Ubmission (s): 1289
Problem Description
To improve the organization of he farm, Farmer John labels each of its n (1 <= n <= 5,000) cows with a distinct ser Ial number in the range 1..20,000. Unfortunately, he is unaware, the cows interpret some serial numbers as better than others. In particular, a cow whose serial number have the highest prime factor enjoys the highest social standing among all the oth ER cows.

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

Given a set of n (1 <= n <= 5,000) serial numbers in the range 1..20,000, determine the one, and the largest PRI Me factor.

Input
* Line 1: A single integer, N

* Lines 2..n+1:the serial numbers to being tested, one per line

Output
* Line 1:the integers with the largest prime factor. If there is more than one, the output of the one that is appears earliest in the input file.

Sample Input
4
36
38
40
42

Sample Output
38

Source

Usaco 2005 October Bronze


The main topic: give you the number of N, to find out which number in the N number of the maximum factor,

Output this number, if there are multiple results, the output depends on the number of the front.


Idea: The Sieve method for the prime number change a bit. If I is a prime number, then I of 1, 2, 3 ... Times the

The maximum factor is I, when the sieve, the assignment is prime[j] = I, that is, the largest element of J

The child is I.

Note: Initialize all the numbers for the season to 0,prime[0] = prime[1] = 1.

That is, Prime[i] is a prime number of 0, Prime[i] is a prime number 1. After the change Prime[i]

maximum cause of I The child.


#include <stdio.h> #include <string.h> #include <math.h>int prime[20005];void isprime () {    prime[ 1] = 1;    for (int i = 2; I <= 20000; i++)    {        if (prime[i]==0)        {            prime[i]=i;            for (int j = i+i; J <= 20000; j+=i)                prime[j] = i;        }    } int main () {    int n,x;    IsPrime ();    while (~SCANF ("%d", &n))    {        int Max = 0;        while (n--)        {            scanf ("%d", &x);            if (Prime[x] > Prime[max])                Max = x;        }        printf ("%d\n", Max);    }    return 0;}



Hdu2710_max Factor "water Problem" "Sieve method for prime number"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.