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 ...
随机推荐
- sharepoint中的YesNo字段
sharepoint中的YesNo字段实际上是一个Boolean字段,性格有点特别,如果IsShow是一个YesNo字段,使用caml查询的时候值为”1“(Yes)”0“(No),Item[IsSho ...
- 什么时候用using (SPSite site = new SPSite(SPContext.Current.Web.Url))
并不是所有时候都适合用using(){},只有当需要提升用户的权限的时候才会用到using,其他时候都可以直接使用SPContext.Current.Web; using ...
- ASP.NET MVC——Controller的激活
Controller的激活是根据在路由过程得到的Controller名称来创建对应的Controller对象.相关类如图: Controller激活的过程可通过如下序列图表示: 代码示例如下: str ...
- A题笔记(2)
No. 1505 文件读相关 #include <fstream> #include <sstream> <fstream>支持文件的IO <sstream& ...
- Hadoop_Block的几种状态_DataNode
在Hadoop 2.0 中HDFS 引入了 append 和 hflush 功能之后, 需要为 数据块增加新的状态 来尽最大可能的保证数据的一致性. 参阅文档: http://files.cnblog ...
- IOS多线程知识总结/队列概念/GCD/串行/并行/同步/异步
进程:正在进行中的程序被称为进程,负责程序运行的内存分配;每一个进程都有自己独立的虚拟内存空间: 线程:线程是进程中一个独立的执行路径(控制单元);一个进程中至少包含一条线程,即主线程. 队列:dis ...
- copy和mutableCopy的深、浅拷贝
对象拷贝主要由两种方式:copy和mutableCopy.浅拷贝是指直接将指针指向原有的地址,从而达到复制的目的.深拷贝是指重新生成一个对象,将原有对象的内容复制到新的对象中.copy 返回的是一个不 ...
- string的一些更改发
/* String 类: 1.1字符串的长度 int c =对象.length(); 1.2字符串某个字符的位置 int index=对象.indexOf("字母") ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- 基于管道通知的百万并发长连接server模型
0.前言 最近突然想了解怎样设计一个支持百万连接的后台server架构. 要设计一个支持百万连接的后台server,我们首先要知道会有哪些因素限制后台server的高并发连接,这里想到的因素有以下几点 ...