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 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,然后输入n个正整数(1<=a[i]<=20000),要你求这n个数里哪个数的最大素因数(即能被该数整除(包括这个数本身!)的最大素数)最大,然后输出这个数。若有两个数的最大素因数相同,则输出前面那个。

用到了素数快速筛选~不然会超时的~

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author 陈浩翔
 */
public class Main{
    static boolean db[] = new boolean[20005];
    public static void main(String[] args) {
        dabiao();
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int a[] = new int[n];
            int prime[] = new int[n];
            for(int i=0;i<n;i++){
                a[i]=sc.nextInt();
                for(int k=1;k<=a[i];k++){
                    if(db[k]&&a[i]%k==0){
                        prime[i]=k;
                    }
                }
            }
            int max=prime[0];
            int con=0;
            for(int i=1;i<n;i++){
                if(prime[i]>max){
                    max=prime[i];
                    con=i;
                }
            }
            System.out.println(a[con]);
        }
    }
    private static void dabiao() {
        Arrays.fill(db, true);
        for(int i=2;i<=Math.sqrt( db.length);i++){
            for(int j=i+i;j<db.length;j+=i){
                if(db[j]==true){
                    db[j]=false;
                }
            }
        }
    }
}
时间: 2024-08-10 19:04:42

HDOJ/HDU 2710 Max Factor(素数快速筛选~)的相关文章

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

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

hdu 2710 Max Factor

#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=20005; int a[maxn]; void isprime()//素数筛 { memset(a,0,sizeof(a)); for(int i=2;i<maxn;i++)//用a[]这个数组存的是i的最大素数值 if(a[i]==0) for(int j=i;j<max

抓其根本(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开始一个

HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

Problem Description Give you a lot of positive integers, just to find out how many prime numbers there are. Input There are a lot of cases. In each case, there is an integer N representing the number of integers to find. Each integer won't exceed 32-

hdu 5428 The Factor(素数筛选合数分解)

题意:求一串数乘积的因子中的最小合数: 思路:比赛时枚举因子,枚举到两个时结束,估计超时,结果果然被叉了: 将每个数分解,取最小的两个质因子,若数目少于2,则不存在: #include<cstdio> #include<cstring> #include<vector> #include<algorithm> using namespace std; int t,n,m; int num[505]; const int MAXN=100000; int pr

埃氏筛法(快速筛选n以内素数的个数)

给你一个数n,请问n以内有多少个素数?(n <= 10e7) 一般来说,要是对一个整数进行素数判断,首先想到的是写个函数判断是否为素数,然后调用这个函数,时间复杂度为O(n^(½)),但是要求n以内的素数就略显吃力了. 要是求n以内的素数个数的话,可以用埃式筛选.预处理一下. 先看下面的代码: 1 /* 2 |埃式筛法| 3 |快速筛选素数| |15-7-26| 4 */ 5 #include <iostream> 6 #include <cstdio> 7 using na

POJ3048 HDU2710 Max Factor

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