POJ3048 Max Factor

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 

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

Hint

OUTPUT DETAILS: 
19 is a prime factor of 38. No other input number has a larger prime factor.

Source

USACO 2005 October Bronze

正解:线性筛+暴力

解题报告:

  直接筛出质数,然后暴力就可以了。

  我连线性筛都写挂了一次,真是没救了...

 1 //It is made by ljh2000
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #include <string>
14 #include <stack>
15 using namespace std;
16 typedef long long LL;
17 const int MAXN = 50011;
18 const int MAXM = 200011;
19 int n,N,a[MAXN],maxl[MAXN];
20 int cnt,prime[MAXM],ans;
21 bool vis[MAXM];
22
23 inline int getint(){
24     int w=0,q=0; char c=getchar(); while((c<‘0‘||c>‘9‘) && c!=‘-‘) c=getchar();
25     if(c==‘-‘) q=1,c=getchar(); while (c>=‘0‘&&c<=‘9‘) w=w*10+c-‘0‘,c=getchar(); return q?-w:w;
26 }
27
28 inline void work(){
29     n=getint(); for(int i=1;i<=n;i++) a[i]=getint(),N=max(a[i],N); ans=1; int x;
30     for(int i=2;i<=N;i++) { if(!vis[i]) prime[++cnt]=i; for(int j=1;j<=cnt && i*prime[j]<=N;j++) { vis[i*prime[j]]=1; if(i%prime[j]==0) break;} }
31     for(int i=1;i<=n;i++) {
32     x=a[i];
33     for(int j=1;j<=cnt;j++) {
34         if(x%prime[j]!=0) continue;
35         maxl[i]=prime[j]; while(x%prime[j]==0) x/=prime[j];
36         if(x==1) break;
37     }
38     }
39     for(int i=2;i<=n;i++) if(maxl[i]>maxl[ans]) ans=i;
40     printf("%d",a[ans]);
41 }
42
43 int main()
44 {
45     work();
46     return 0;
47 }
时间: 2024-10-12 21:17:15

POJ3048 Max Factor的相关文章

POJ3048 HDU2710 Max Factor

筛选法不仅能够用来计算最小的若干素数,也可以用来求整数的最大公因子. 问题链接:POJ3048 HDU2710 Max Factor.基础训练级的题,用C语言编写. 问题简述:测试数据有多组,每组先输入n,然后输入n个正整数,输出n个正整数中,素因子最大的那个数. 问题分析:可以使用类似于筛选法的过程求得一定范围内的各个整数的最大素因子. 程序中,打表是合适的.数组mpf[]中存放最大素因子,若mpf[i]=x,则x为i的最大素因子. AC通过的C语言程序如下: /* POJ3048 HDU27

hdu 2710 Max Factor(找最大素数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2710 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 unawa

HDU 2710 Max Factor 数论(水

Max Factor Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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

HDU2710 Max Factor【水题】【素因子】

Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4110    Accepted Submission(s): 1342 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1

抓其根本(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)

素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. 1 int prime() 2 { 3 for (int i=2; i*i<=n; i++) 4 { 5 if (n%i==0) //不是素数 6 return 1; //返回1 7 } 8 return 0; //是素数返回0 9 } 二.打表,将所有的素数一一列出,存在一个数组里. 详见代码. 1 void prime() 2 { 3 for (int i=2; i<20050; i++) //从2开始一个

HDU 2710 Max Factor (筛选求素数)

Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3995    Accepted Submission(s): 1301 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1

【poj3048】 Max Factor

http://poj.org/problem?id=3048 (题目链接) 题意 给出n个数,问其中哪个数拥有最大的质因子. Solution 线性筛求素数 细节 .. 代码 // poj3048 #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #define MOD

HDOJ/HDU 2710 Max Factor(素数快速筛选~)

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 be

POJ 3048 Max Factor (筛素数)

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 tha