Max Factor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 10245    Accepted Submission(s): 3304

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

436384042
 
Sample Output

38

思路:

忘记了memset()只能初始化0、-1,手贱赋了个1,然后疯狂WA

这里用到了素数筛选法:单击666查看大神详解

代码:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 20010
using namespace std;
int vis[N]; void prime(){ //素数筛选
memset(vis,0,sizeof(vis));
vis[0]=1,vis[1]=0; //这里1都算素数
for(int i=2;i<=20000;i++){
if(!vis[i]){
for(int j=i*i;j<=20000;j+=i){
vis[j]=1;
}
}
}
} int main(){
int n,ans,maxp,i,a;
prime();
while(~scanf("%d",&n)){
maxp=0;
ans=0;
while(n--){
scanf("%d",&a);
for(i=a;i>0;i--){
if(!vis[i]){
if((i>maxp) && a%i==0){
maxp=i;
ans=a;
break;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}

Max Factor(素数筛法)题解的更多相关文章

  1. 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)

    素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...

  2. HDOJ/HDU 2710 Max Factor(素数快速筛选~)

    Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...

  3. HDU-2710 Max Factor

    看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. hdu6069(简单数学+区间素数筛法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意: 给出 l, r, k.求:(lambda d(i^k))mod998244353,其中 ...

  5. HDU 6069 Counting Divisors(区间素数筛法)

    题意:...就题面一句话 思路:比赛一看公式,就想到要用到约数个数定理 约数个数定理就是: 对于一个大于1正整数n可以分解质因数: 则n的正约数的个数就是 对于n^k其实就是每个因子的个数乘了一个K ...

  6. NowCoder猜想(素数筛法+位压缩)

    在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~  明明有 3 万多 k ...

  7. [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】

    拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...

  8. 数学#素数筛法 HDU 4548&POJ 2689

    找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数 ...

  9. POJ 3292 Semi-prime H-numbers (素数筛法变形)

    题意:题目比较容易混淆,要搞清楚一点,这里面所有的定义都是在4×k+1(k>=0)这个封闭的集合而言的,不要跟我们常用的自然数集混淆. 题目要求我们计算 H-semi-primes, H-sem ...

随机推荐

  1. JS "trycatch工厂模式"

    大家都用过Ajax的异步交互,下面的代码中使用  "trycatch工厂模式"  来进行针对Ajax请求对象的变化点进行封装 <script type="text/ ...

  2. elasticsearch数据转移,elasticdump的安装使用

    模拟: 将本地的my_index的products的一条document转移到http://192.168.111.130的一个es服务器上. (一)安装elasticdump 先安装node.js, ...

  3. 010-mac下常用命令

    1.查看某个端口是否运行 lsof -i tcp:port lsof -i:8080 2.强制关闭进程 kill -9 PID

  4. 十天精通CSS3(3)

    颜色之RGBA RGB是一种色彩标准,是由红(R).绿(G).蓝(B)的变化以及相互叠加来得到各式各样的颜色.RGBA是在RGB的基础上增加了控制alpha透明度的参数. 语法: color:rgba ...

  5. Centos上把新安装的程序添加到系统环境变量的两种方法

    1.软链接 通过命令查看当前系统的环境变量信息,然后软连接形式把程序的地址连接到已经在环境变量中的目录中 echo "$PATH" > /root/tmp 结果如下: /us ...

  6. [Leetcode] 336. Palindrome Pairs_Hard

    Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...

  7. Java-单向链表算法

    /** * 数据结构之链表(单向链表) * @author Administrator * */ public class LinkNodeTest { public static void main ...

  8. Qt5

    最简单的分割窗体 #include <QApplication> #include <QLabel> #include <QSplitter> int main(i ...

  9. Lintcode: Heapify && Summary: Heap

    Given an integer array, heapify it into a min-heap array. For a heap array A, A[0] is the root of he ...

  10. GreenPlum安装greenplum-cc-web监控

    一. GreenPlum集群安装环境 由虚拟机搭建的一台master两台segment. 二.安装前准备 1) 所需安装包 GreenPlum监控安装包: greenplum-cc-web-3.0.2 ...