素数筛选:HDU2710-Max Factor
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
解题心得:
- 就是一个简单的素数筛选,然后暴力找一下因子,判断记录就可以了。没有什么好说的。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e4+100;
bool pre_num[maxn];
//先吧素数给筛选出来
void get_pre_num()
{
for(int i=2; i<=sqrt(maxn); i++)
{
if(pre_num[i])
continue;
for(int j=i*i; j<maxn; j+=i)
pre_num[j] = true;
}
pre_num[1] = pre_num[0] = true;
}
int main()
{
get_pre_num();
int n;
while(scanf("%d",&n) != EOF)
{
int Max = 0,pos = 1;
for(int i=0; i<n; i++)
{
int now;
scanf("%d",&now);
int N = now;
for(int j=2; j<=sqrt(now); j++)
{
if(now%j == 0)//暴力找因子
{
if(!pre_num[j])
{
if(j > Max)//记录一下就可以了
{
Max = j;
pos = N;
}
}
while(now%j == 0)
now /= j;
}
}
if(now > 1 && !pre_num[now])//别忘了最后还剩下一个
{
if(now > Max)
{
Max = now;
pos = N;
}
}
}
printf("%d\n",pos);
}
}
素数筛选:HDU2710-Max Factor的更多相关文章
- 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...
- HDU-2710 Max Factor
看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 素数筛选-hdu2710
题目描述: 题目大意:找出具有最大素数因子的整数.如果有不止一个,则输出在输入文件中出现最早的一个. 解题思路:刚开始时,p数组中的元素全为0,刚开始对于素数 i,p[i]=0,用一个for循环,将是 ...
- hdu2710 Max Factor
题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...
- Max Factor(素数筛法)题解
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU_2136——最大质因数,素数筛选法
Problem Description Everybody knows any number can be combined by the prime number. Now, your task i ...
- POJ2689 Prime Distance(数论:素数筛选模板)
题目链接:传送门 题目: Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Des ...
- PAT甲题题解-1059. Prime Factors (25)-素数筛选法
用素数筛选法即可. 范围long int,其实大小范围和int一样,一开始以为是指long long,想这就麻烦了该怎么弄. 而现在其实就是int的范围,那难度档次就不一样了,瞬间变成水题一枚,因为i ...
- 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) == ...
随机推荐
- 我的grunt配置
module.exports = function(grunt) { // 配置. grunt.initConfig({ pkg: grunt.file.readJSON('package.json' ...
- Spring Cloud 熔断器
目录 Spring Cloud 熔断器 Hystrix ribbon中使用hystrix feign中使用hystrix Spring Cloud 熔断器 在微服务架构中,根据业务来拆分成一个个的服务 ...
- java中两个map比较
一 /** * 用map的keySet()的迭代器(性能效率较低) * */ public void compareMap1 (){ Map<String, String> m1 = ne ...
- arcgis python 保存当前窗口图形为jpg
1,第一步打开arcgis 将图形加载进去 第二步,将要保存的图形调到合适的比例尺,然后点击下面按钮 第三步,将写好的python 语句放到里面去: import arcpy mxd = arcpy. ...
- 《超实用的Node.js代码段》连载三:Node.js深受欢迎的六大原因
<超实用的Node.js代码段>连载一:获取Buffer对象字节长度 <超实用的Node.js代码段>连载二:正确拼接Buffer Node.js是一种后起的优秀服务器编程语言 ...
- 对fgets末尾'\0'的处理
之所以要对fgets自动添加的字符进行处理的原因之一是:当你想比较输入的字符时,你会发现输入的字符和源码用来进行对比的字符一模一样,但是使用strcmp比较时就是不一样,原因就是fgets对输入字符添 ...
- SqlServer 填充因子的说明
CREATE NONCLUSTERED INDEX IX_d_name ON department(d_name) with fillfactor=30 使用 fill factor 选项可以指定 M ...
- Spring boot 集成ActiveMQ(包含双向队列实现)
集百家之长,成一家之言. 1. 下载ActiveMQ https://mirrors.tuna.tsinghua.edu.cn/apache/activemq/5.15.9/apache-activ ...
- IDEA安装及破解
一.下载(IDEA 2019.1.2) 1.下载地址:https://www.jetbrains.com/idea/download/#section=windows 2.选择版本,并选择最终版(.e ...
- java中插入myslq的datetime类型的
java.util.Date d = new java.util.Date(); Timestamp tt=new Timestamp(d.getTime()); ps.setTimestamp(4, ...