您的位置:首页 > 其它

HDU 2710 Max Factor

2017-07-28 23:17 453 查看


Max Factor

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

Total Submission(s): 8942    Accepted Submission(s): 2924


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

 

Recommend

teddy   |   We have carefully selected several similar problems for you:  2700 2716 2709 2715 2568 

题意:计算n个数中含的最大的素因子

水题,直接暴力,但是要注意输入的数可能全为1,自己做的时候还因为这个WA了一次,自己还是太菜

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<cmath>
#include<vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;

#define pi acos(-1.0)
#define eps 1e-10
#define pf printf
#define sf scanf
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define e tree[rt]
#define _s second
#define _f first
#define all(x) (x).begin,(x).end
#define mem(i,a) memset(i,a,sizeof i)
#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)
#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)
#define mi ((l+r)>>1)
#define sqr(x) ((x)*(x))

const int inf=0x3f3f3f3f;
const int Max=2e2+1;
bool vis[Max+1];
int m,p[Max/2],t,ans,num;

void prime()//打素数表,打到200就可以了
{
p[0]=0;
mem(vis,0);
for(int i=2;i<=Max;i++)
{
if(!vis[i])p[++p[0]]=i;
for(int j=1;j<=p[0]&&(ll)i*p[j]<=Max;j++)
{
vis[i*p[j]]=1;
if(!(i%p[j]))break;
}
}
}

void fenjie()//唯一分解就最大素因子
{
int n=m;
for(int i=1;i<=p[0]&&(ll)p[i]*p[i]<=n;i++)
{
while(!(n%p[i]))
n/=p[i];
if(ans<p[i])
num=m,ans=p[i];//num保存最终结果
}
if(ans<n)//不需要判断n是否大于1,如果全是1,num赋值为1
ans=n,num=m;
}

int main()
{
prime();
while(~sf("%d",&t))
{
ans=0;
while(t--)
{
sf("%d",&m);
fenjie();
}
pf("%d\n",num);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: