Max Factor

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

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


解题心得:

  1. 就是一个简单的素数筛选,然后暴力找一下因子,判断记录就可以了。没有什么好说的。

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 2e4+100;
  4. bool pre_num[maxn];
  5. //先吧素数给筛选出来
  6. void get_pre_num()
  7. {
  8. for(int i=2; i<=sqrt(maxn); i++)
  9. {
  10. if(pre_num[i])
  11. continue;
  12. for(int j=i*i; j<maxn; j+=i)
  13. pre_num[j] = true;
  14. }
  15. pre_num[1] = pre_num[0] = true;
  16. }
  17. int main()
  18. {
  19. get_pre_num();
  20. int n;
  21. while(scanf("%d",&n) != EOF)
  22. {
  23. int Max = 0,pos = 1;
  24. for(int i=0; i<n; i++)
  25. {
  26. int now;
  27. scanf("%d",&now);
  28. int N = now;
  29. for(int j=2; j<=sqrt(now); j++)
  30. {
  31. if(now%j == 0)//暴力找因子
  32. {
  33. if(!pre_num[j])
  34. {
  35. if(j > Max)//记录一下就可以了
  36. {
  37. Max = j;
  38. pos = N;
  39. }
  40. }
  41. while(now%j == 0)
  42. now /= j;
  43. }
  44. }
  45. if(now > 1 && !pre_num[now])//别忘了最后还剩下一个
  46. {
  47. if(now > Max)
  48. {
  49. Max = now;
  50. pos = N;
  51. }
  52. }
  53. }
  54. printf("%d\n",pos);
  55. }
  56. }

素数筛选:HDU2710-Max Factor的更多相关文章

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

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

  2. HDU-2710 Max Factor

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

  3. 素数筛选-hdu2710

    题目描述: 题目大意:找出具有最大素数因子的整数.如果有不止一个,则输出在输入文件中出现最早的一个. 解题思路:刚开始时,p数组中的元素全为0,刚开始对于素数 i,p[i]=0,用一个for循环,将是 ...

  4. hdu2710 Max Factor

    题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...

  5. Max Factor(素数筛法)题解

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

  6. HDU_2136——最大质因数,素数筛选法

    Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...

  7. POJ2689 Prime Distance(数论:素数筛选模板)

    题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...

  8. PAT甲题题解-1059. Prime Factors (25)-素数筛选法

    用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...

  9. LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)

    题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...

随机推荐

  1. AI入门丨开源学习资源推荐

    现在AI大热,网上的资源也非常多,让人眼花缭乱.非科班的我,经过半年的摸索,也算马马虎虎入了坑.下面整理了我认为不错的学习资源,大部分我都看过,以分享给更多的人.我会不断保持更新,也欢迎大家补充. P ...

  2. redis和mysql同步 终极解决方案

    使用Canal,类似mysql的主从复制,实时更新 具体使用之后更新

  3. Tensorflow版Faster RCNN源码解析(TFFRCNN) (1) VGGnet_test.py

    本blog为github上CharlesShang/TFFRCNN版源码解析系列代码笔记第1篇   VGGnet_test.py ----作者:Jiang Wu(吴疆),未经允许,禁止转载--- -- ...

  4. Python网络编程中的服务器架构(负载均衡、单线程、多线程和同步、异步等)

    这篇文章主要介绍服务器架构. 网络服务需要面对两个挑战. 第一个问题是核心挑战,要编写出能够正确处理请求并构造合适响应的代码. 第二个挑战是如何将网络代码部署到随系统自动启动的Windows服务或者是 ...

  5. ruby YAML.load 和YAML.load_file区别

    1. load( io ) Load a document from the current io stream. File.open( 'animals.yaml' ) { |yf| YAML::l ...

  6. Ionic开发-常用命令

      $ionic start myApp [tabs | sidemenu | blank] $ionic platform add android $ionic build android $ion ...

  7. P4876 近似排列计数50

    时间限制:1s 内存限制:256MB [问题描述] 对于一个1-n的排列,如果满足第i个数|ai-i|<=k,则称该排列为K-近似排列. 现在排列的若干位置已经确定,你需要计算剩下的数有多少种排 ...

  8. 如何优化Mysql执行查询数据的速度

    在项目中数据量小的情况下使用like查询速度还行,但是随着数据一天一天增加,再使用like进行模糊查询的时候速度上就会显得比较慢,现提供两套解决方案: 问题: 使用like查询效率很慢 select ...

  9. sqlserver跟据当天年月日日期查询数据库当天数据

    select * from Client where  CONVERT(varchar(100), Cli_Datetime, 23) ='2017-11-06' 在查询之前要对表中datetime类 ...

  10. 【Java】 jar解压与压缩

    jar解压与压缩 命令格式:jar {c t x u f }[ v m e 0 M i ][-C 目录]文件名 # 解压,到当前目录 jar -xvf source.jar # 打包,不进行压缩 ja ...