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;
}
相关文章推荐
- Hdu 6069 Counting Divisors【素数区间筛+预处理素因子分解】
- HDU 2710 Max Factor (素数筛选)
- hdu 2136(最大素数因子)
- hdu 2136(最大素数因子)
- 素数因子(hdu ****)
- HDU 1164 Eddy's research I 素数因子
- HDU 2710 Max Factor【素数筛选】
- HDU2710 Max Factor【水题】【素因子】
- HDU 1058 素数因子DP
- HDU Largest prime factor && nyoj 520 最大素因子【素数】
- hdu 2710 最大的素数因子
- hdu 1164(一个数的素数因子)
- HDU 2136 找出某数的最大素数因子
- HDU 5108 最大素数因子
- HDU 1164 找素数因子
- hdu 2136Largest prime factor n的最大因子是第几个素数
- poj3048--Max Factor(最大素数因子)
- HDU 2710 Max Factor 素数先打表
- HDU - 1999 不可摸数 (所有的真因子和,类似于素数筛选的那个过程写的)
- HDU 1333 分解素数因子