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;
}
}
}
}
}
HDOJ/HDU 2710 Max Factor(素数快速筛选~)的更多相关文章
- HDU 2710 Max Factor(数论,素数筛法)
#include<iostream> #include<stdio.h> #include<string.h> #include<cmath> usin ...
- hdu 2710 Max Factor 数学(水题)
本来是不打算贴这道水题的,自己却WA了三次.. 要考虑1的情况,1的质因子为1 思路:先打表 ,然后根据最大质因子更新结果 代码: #include<iostream> #include& ...
- 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)
Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...
- HDOJ 1397 Goldbach's Conjecture(快速筛选素数法)
Problem Description Goldbach's Conjecture: For any even number n greater than or equal to 4, there e ...
- Max Factor(素数筛法)题解
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 埃氏筛法(快速筛选n以内素数的个数)
给你一个数n,请问n以内有多少个素数?(n <= 10e7) 一般来说,要是对一个整数进行素数判断,首先想到的是写个函数判断是否为素数,然后调用这个函数,时间复杂度为O(n^(½)),但是要求n ...
- HDU-2710 Max Factor
看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- win7下配置apache和php
1.软件装备 PHP:http://php.net/downloads.php non-thread-safe是非安全线程主要与IIS搭配环境. thread-safe安全线程与Apache搭配环境. ...
- 搭建linux环境下jenkins可移植环境
1:背景 项目领域:android. 项目需求为:建立一个网站用于产品经理(以下称为PM)配置该apk所需服务插件,打包出包(包含:apk,文档,demo等等)给厂商并且记录打包出包等信息. 项目设计 ...
- SQL Prompt Snippet Manager 妙用
SQL Prompt有一个很好用的工具叫Snippet Manager,SQL脚本片段管理器. 使用它可以快速的键入一段脚本,如输入ii+Tab,即可变成INSERT INTO 同理,我们可以定义一些 ...
- Access restriction:The type JPEGCodec is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar
解决方法: Project -> Properties -> libraries, 先remove掉JRE System Library,然后再Add Library重新加入. ===== ...
- C++ trivial和non-trivial构造函数及POD类型(转)
原博客地址http://blog.csdn.net/a627088424/article/details/48595525 最近正纠结这个问题就转过来了,做了点补充(参考<深度探索C++对象模型 ...
- 【POJ1811】【miller_rabin + pollard rho + 快速乘】Prime Test
Description Given a big integer number, you are required to find out whether it's a prime number. In ...
- 自动化运维工具之ansible
自动化运维工具之ansible 一,ansible简介 ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fab ...
- hibernate一些方法
session.flush() 同步缓存与数据库数据 session.evict(obj) 关闭指定对象缓存 session.clear() 关闭所有缓存 iterator(会把数据放入缓存) 下次 ...
- [转] 博闻强识:了解CSS中的@ AT规则 ---张鑫旭
by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.com/wordpress/?p=4900 大家可能在CS ...
- str、__str__ 、repr、 __repr__、``
内建函数str()和repr() 或反引号操作符(``)可以方便地以字符串的方式获取对象的内容.类型.数值属性等信息. str()函数得到的字符串可读性好(故被print调用),而结果通常无法用eva ...