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-09-29 16:46:58

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

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

HDOJ 1397 Goldbach&amp;#39;s Conjecture(快速筛选素数法)

Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. This conjecture has not been proved nor refused yet. No one is sure whether

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-

如何利用win7系统中搜索功能的快速筛选器删除指定大小的文件?

  电脑使用的时间长了,自然会出现各种各样的垃圾文件,因为归类不一的原因,咱们若是要手动删除这些垃圾文件的话,便需要一层一层的进入到win7 64位旗舰版iso的磁盘中,然后一个一个的找到它们并将它们删除掉,但是无疑的,这是一个十分浩大的工程,需要花费很长的时间,需要耗费很多的心力才能完成,不过win7旗舰版之后,很多朋友在搜索功能中发现了一个快速筛选器,也许咱们可以利用这个功能帮我们快速的删除掉一些文件.下面,小编就来说说具体的操作技巧吧! 1.首先,咱们返回到win7旗舰版的桌面位置,然后找

HDOJ(HDU) 2161 Primes(素数打表)

Problem Description Write a program to read in a list of integers and determine whether or not each number is prime. A number, n, is prime if its only divisors are 1 and n. For this problem, the numbers 1 and 2 are not considered primes. Input Each i

在Windows 8.1中按不同条件快速筛选应用

Win8.1的开始屏幕有一个小小的改进--在左下角增加了一个小小的箭头按钮,这个按钮可以帮我们迅速带到Win8.1系统的应用列表中,在这里我们可以看到当前安装的所有应用. 点击Win8.1开始屏幕左下方的小箭头图标,触摸屏用户向上滑动即可,切换到应用列表界面. 图示:点击箭头按钮或者触控向上滑动切换到Win8.1应用视图 注意,在"应用"二字的右边有一个排序分类的选项,点击下图中红框所示的小箭头,在下拉菜单中选择分类方式为"按安装日期".瞬间所有Win8.1系统内安

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2107 Founding of HDU(找最大值)

Problem Description 经过慎重的考虑,XHD,8600, LL,Linle以及RPG等ACM队员集体退役,甚至正在酝酿退学. 为什么?要考研?那也不用退学呀- 当然不是!真正的原因是他们想提前创业,想合伙成立一家公司,据说公司的名称都想好了,为了感谢多年的ACM集训队队长XHD,公司就叫海东集团(HaiDong Union),简称HDU.(对于这个公司名称,几个人私下里开玩笑说,外面的人看到HDU,可别以为是"胡捣集团",呵呵) 公司成立了,谁来做老大呢?这对于合伙的

魅蓝Max手机如何快速Root 魅蓝Max一键ROOT教程

魅蓝Max一键ROOT教程 步骤如下: 一.我们在魅蓝Max系统手机界面点击「设置」-「Flyme账户」然后使用 Flyme账号登录,这里没有帐号注册一个,如下图所示. 还没有账号?先注册一个 二.有的话登录Flyme账号.密码登录成功之后,找到界面中的[指纹和安全]选项,如下图所示. 进入指纹和安全设置 三.点在这里直接就有一个[Root权限]选项,然后我们可以点击它,在弹出界面点击「同意接受以上内容」并点击下方的->[确定]即可,如下图所示. 四.为了安全保障起见,这个时候在进行魅蓝Max