题干:

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

Hint

OUTPUT DETAILS: 
19 is a prime factor of 38. No other input number has a larger prime factor.

解题报告:

正解的代码显然是打表预处理nlogn,单组样例o(n)。。。。

第一个的做法是nloglogn打表,然后单组样例最坏复杂度n*20000(也就是每一次都是一个素数。、。。)

AC代码1:(860ms)

#include<cstdio>
#include<cstring>
#include<iostream>
#define MAX 20000 + 18//求MAX范围内的素数
using namespace std;  long long su[MAX],cnt;
bool isprime[MAX];
void prime()
{  cnt=1;  memset(isprime,1,sizeof(isprime)); isprime[0]=isprime[1]=0;for(long long i=2;i<=MAX;i++)  {  if(isprime[i]) {su[cnt++]=i; } for(long long j=1;j<cnt&&su[j]*i<MAX;j++)  {  isprime[su[j]*i]=0;}  }
}
int main()
{prime();int n,x,maxx;int i,ans = 0; while(~scanf("%d",&n) ) {ans = 0;maxx = -1;while(n--) {scanf("%d",&x);for(int i = x; i>=2; i--) {//说明是最大素因子。 if(isprime[i] && x%i==0) {if(i>maxx) {ans = x;maxx = i;}}}}ans>1?printf("%d\n",ans):printf("1\n");}return 0 ;} 

AC代码2:(0ms)

#include <cstdio>
#include <cstring>
const int MAXN = 20017;
int s[MAXN];
int main()
{int n,m;memset(s,0,sizeof(s));s[1]=1;//此题1也是素数for(int i = 2; i < MAXN; i++)//筛选所有范围内的素数{if(s[i] == 0)//没有被更新过for(int j = i; j < MAXN; j+=i){s[j]=i;}}while(~scanf("%d",&n)){int ans;int maxx = -1;for(int i = 0; i < n; i++){scanf("%d",&m);if(s[m] > maxx){maxx = s[m];ans = m;}}printf("%d\n",ans);}return 0;
}

【POJ - 3048】Max Factor (数论,打表,水题)相关推荐

  1. HDU2521 反素数【因子数量+打表+水题】

    反素数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  2. NUC1077 Humble Numbers【数学计算+打表+水题】

    Humble Numbers 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 A number whose only prime factors are ...

  3. Codeforces 742A Arpa’s hard exam and Mehrdad’s naive cheat 打表+水题

    A. Arpa's hard exam and Mehrdad's naive cheat time limit per test 1 second memory limit per test 256 ...

  4. 2017西安交大ACM小学期数论 [水题]

    水题 发布时间: 2017年6月25日 14:06   最后更新: 2017年7月3日 09:27   时间限制: 1000ms   内存限制: 128M 描述 平均因数个数的统计对于估算数论题目复杂 ...

  5. POJ 1936 字符匹配(水题)

    题目链接: http://poj.org/problem?id=1936 题目大意: 给定字符a,b,问b中去掉一些字符后能不能得到a 解题思路: 暴力从前往后扫描一遍即可. AC代码: /*** @ ...

  6. poj 1789 kruscal水题

    继续水水题... 题目:http://poj.org/problem?id=1789 把车看成结点,车之间的距离看作权重就是一个图了,然后求最小生成树... 的确水题,但看题目花了挺长时间,不知道如果 ...

  7. poj 3095 Linear Pachinko 模拟水题

    题意: 给一个字符串,求小球随机放在上面进洞或到达两边之外的期望. 分析 水题,直接模拟. 代码: //poj 3095 //sep9 #include <iostream> using ...

  8. 有关狼人杀的专业名词及表水思路

    一个不知名大学生,江湖人称菜狗 original author: jacky Li Email : 3435673055@qq.com Last edited: 2022.11.24 目录 一.专业名 ...

  9. POJ 2291水题

    题意:给你几根绳子,每根最大承重能力都不同,从中选出几根,问最大承重力量是多少?(比如有1,10,15承重的3跟绳子,如果用一根的话,选15的,能承重15,用2根的话,用10和15的,能承重10*2= ...

  10. 水题/poj 1852 Ants

    1 /* 2 PROBLEM:poj1852 3 AUTHER:Nicole 4 MEMO:水题 5 */ 6 #include<cstdio> 7 using namespace std ...

最新文章

  1. leetcode之Valid Sudoku有效的数独(一步步改进代码)
  2. java systemoutprint_java – 为什么System.out.print()不起作用?
  3. pip 换清华源 更新所有库 windows
  4. Ubuntu里面vi编辑器在编辑文本时 如何在所有行行首或行尾插入字符
  5. Ubuntu下安装配置Phabricator
  6. 从魔兽世界到激战2看MMO网游角色成长
  7. 疯狂涨知识!最全的java手机游戏免费下载基地
  8. 电梯远程监控维护系统方案
  9. 固有模态函数IMF与经验模态分解EMD
  10. 品优购---品优购项目制作---8.4
  11. android路由器安装wifidog,openwrt下的wifidog安装及web认证界面设置
  12. VNC远程控制软件,五大容易上手的VNC远程控制软件
  13. 中国计算机学会推荐国际学术会议和期刊目录 2015
  14. linux进程管理数据结构,谢烟客---------Linux之进程管理基础概念
  15. 【开发教程14】AI语音人脸识别(会议记录仪/人脸打卡机)-AI人脸系统架构
  16. 信息学奥赛一本通-1055:判断闰年
  17. 计算机考研考线代和概率论吗,关于考研数学线代和概率论的暑期复习扫尾建议...
  18. 中科创达发布融合智能泊车技术于解决方案
  19. 立体字3D字体数字设计|造型艺术字,灵感来源,速码!
  20. 久其修改服务器地址,久其软件服务器地址

热门文章

  1. 文档排序--相似度模型--VSM
  2. [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第14篇]什么是基于线性对的密码学
  3. html的排版标题的是,HTML 5结构排版布局
  4. mysql多源复制 知乎_MySQL多主一从(多源复制)同步配置
  5. 1-5 线性表元素的区间删除 (20 分)
  6. spring security:自定义认证成功处理器
  7. html5 deckview,六本木Hills出現超大型巨人?進擊的巨人展FINAL×頂樓Sky Deck的VR體驗...
  8. 修改数据包欺骗服务器,Fiddler协议捕获编辑工具与Session欺骗原理详解
  9. 摄像头图像分析目标物体大小位置_摄像头的原理、组成、选型及应用
  10. 互斥信号量和二进制信号量