您的位置:首页 > 其它

HDU 2709 Max Factor (素数因子)

2017-06-04 00:28 337 查看


Max Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8635    Accepted Submission(s): 2817


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

 

Source

USACO 2005 October Bronze

 

123

题目大意:   给你n个数,  问你这个n个数里的有  最大的质因子事那个; 

既是因子, 又得是质因数

先素数达标,然后判断,  找最大的质因子;

#include <iostream>
#include <stdio.h>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>

int T;
typedef long long ll;
const int MAXN=20010;
using namespace std;
int k;
in
c853
t pre[MAXN];
int num[MAXN];
int s[MAXN];
int ans[MAXN];
void tab()
{
k=1;
pre[1]=1;
for(int i=2;i<=MAXN;i++)
{
if(!pre[i])
{
num[k++]=i;
for(int j=i+i;j<=MAXN;j+=i)
pre[j]=1;
}
}

}
int main()
{
int n;
tab();
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%d",s+i);
for(int i=1;i<=n;i++)
{
if(!pre[s[i]])// 自己是素数
{
ans[i]=s[i];
continue;
}
ans[i]=1;
for(int j=1;num[j]<=s[i];j++)
{
if(s[i]%num[j]==0) //是因子,并且另一边是素数;
{
if(!pre[s[i]/num[j]])// 对应的 为素数
{
if(num[j]>ans[i])
ans[i]=s[i]/num[j];
}

}
}
}
int mmax=ans[1];
int low=1;
for(int i=1;i<=n;i++)
{
if(ans[i]>mmax)
{
mmax=ans[i];
low=i;
}
}
printf("%d\n",s[low]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: