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 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

题意:给你n个数,让你求出其中质因数最大的数思路:这是筛素数的思想,在筛素数的时候,记录每个数对应的最大质因数,这样在访问该数的时候可以以o(1)的复杂度找到它所对应的最大质因数。再加上贪心的想法,在每次访问的时候找到当前最大质因数,并且记录该数。

代码如下:

#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() //作用:找到每个数所对应的最大素数
{
  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;
}

 
时间: 2024-10-16 10:13:52

POJ 3048 Max Factor (筛素数)的相关文章

poj 2689 Prime Distance(大区间筛素数)

http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 因为L<U<=2147483647,直接筛素数是不行的,数组就开不了.但是可以根据素数筛的原理.我们先筛出sqrt(2147483647)以内的素数,然后拿这些素数去筛[L,U]之间的素数,即两次素数筛.但是L,U还是很大,但U-L<=1000000,所以进行区间平移,将[L,U]平移为[0,U-L],就能用数组放得下. #include &l

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

poj 2635 The Embarrassed Cryptographer 筛素数+高精度除法

题意: 给K(<10^100),L(<10^6),求K小于L的最小素因子并输出,如果没有则输出GOOD. 分析: 枚举小于L的素数用高精度除法判断是否是因子,关键是怎么高效筛素数,先给一种比较慢的筛法: primes[max_prime_num],num=0; memset(vis,0,sizeof(vis)) for(int i=2;i<maxL;++i) if(vis[i]==0){ prime[num++]=i; for(int j=2*i;j<maxL;j+=i) vis[

抓其根本(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

POJ 2262 Goldbach&#39;s Conjecture (素数判断)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37693   Accepted: 14484 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

poj 2478 Farey Sequence(基于素数筛法求欧拉函数)

http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它基本的性质. 1.欧拉函数是求小于n且和n互质(包括1)的正整数的个数.记为φ(n). 2.欧拉定理:若a与n互质,那么有a^φ(n) ≡ 1(mod n),经常用于求幂的模. 3.若p是一个质数,那么φ(p) = p-1,注意φ(1) = 1. 4.欧拉函数是积性函数: 若m与n互质,那么φ(nm) = φ(n) * φ(m). 若n = p^k且p为质数,那么φ(n) = p^k - p

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

POJ3048 HDU2710 Max Factor

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