素数筛选: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) == ...
随机推荐
- 040 Combination Sum II 组合总和 II
给定候选号码数组 (C) 和目标总和数 (T),找出 C 中候选号码总和为 T 的所有唯一组合.C 中的每个数字只能在组合中使用一次.注意: 所有数字(包括目标)都是正整数. 解决方案集不 ...
- SQL server 游标,随机数使用
SELECT * FROM [goods] DECLARE test_cursor CURSOR scroll FOR SELECT goods_no, [unitprice] FROM [goods ...
- 使用Spring Security OAuth2进行简单的单点登录
1.概述 在本教程中,我们将讨论如何使用Spring Security OAuth和Spring Boot实现SSO - 单点登录. 我们将使用三个单独的应用程序: 授权服务器 - 这是中央身份验证机 ...
- ci框架数据库相关函数
返回查询影响的记录数 $res = $this->db->get_where('wx_life',array('id'=>$id)); $num = $res->num_row ...
- Aspose.word直接转pdf
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Netty之WebSocket和四种IO介绍
Netty简介 一.什么是netty? 高性能 事件驱动 异步非堵塞 基于NIO的客户端,服务器端编程框架 稳定性和伸缩性 二.Netty的使用场景 高性能领域 多线程并发领域 异步通信领域 ...
- Java基础语法(自定义类、ArrayList集合)
Java基础语法 今日内容介绍 u 自定义类 u ArrayList集合 第1章 引用数据类型(类) 1.1 引用数据类型分类 提到引用数据类型(类),其实我们对它并不陌生,如使用过的Scanner类 ...
- swift基础-3
fallthrough 贯穿 case 可以不必写break 如果不需要知道区间内 每一项的值 可以使用 下划线 —— 来代替变量名 忽略 对该值的访问 for index in 1...5{ p ...
- 图解HTTP总结
一.TCP/IP 的分层管理 二.TCP/IP通信传输流 ARP地址解析协议参考:https://www.cnblogs.com/csguo/p/7527303.html 三.各种协议与HTTP协议的 ...
- IE盒子模型和W3C盒子模型
IE盒模型出现在ie5.5以下的版本当中,ie6以上就实行W3C盒模型. box-sizing有两个属性,border-box和content-box. border-box对应传统的盒子模型,即ie ...