POJ 3048 Max Factor (sieve prime)

Source: Internet
Author: User

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

436384042

Sample Output

38


Test instructions: give you n number, let you find out which factorization the largest number
Idea: This is the idea of the number of sieve, in the time of the sieve prime, record the maximum factorization of each digit, so that when the number is accessed, it can find its corresponding maximum factorization with the complexity of O (1). In addition to the greedy idea, the current maximum factorization is found at each visit, and the number is recorded.

The code is as follows:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace Std;

const int n=20010;
BOOL A[n];
int ans[n];

void IsPrime ()//function: Find the maximum number of prime numbers corresponding to each
{
int k=0;
memset (A,true,sizeof (a));
A[0]=a[1]=false;
for (int i=2;i<n;i++)
{
if (A[i])
{
Ans[i]=i;
for (int j=i*2;j<n;j+=i)
Ans[j]=i;

A[j]=false;
}
}
}

int main ()
{
IsPrime ();
int m;
while (~SCANF ("%d", &m))
{
int maxn=-n;
int x=-n;
while (m--)
{
int A;
scanf ("%d", &a);
if (ANS[A]>MAXN)
{
Maxn=ans[a];
X=a;
}
}
printf ("%d\n", X);
}
return 0;
}

POJ 3048 Max Factor (sieve prime)

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.