素数筛选: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) == ...
随机推荐
- P2737 [USACO4.1]麦香牛块Beef McNuggets 数学题 + 放缩思想
https://www.luogu.org/problem/show?pid=2737#sub 先说一个结论:对于两个数p, q,且gcd(p, q) = 1(这个很重要,是条件来的).他们不能组合成 ...
- (转)企业配置sudo命令用户行为日志审计
原文:https://www.cnblogs.com/Csir/p/6403830.html?utm_source=itdadao&utm_medium=referral 第15章 企业配置s ...
- 开源分布式Job系统,调度与业务分离-如何创建周期性的HttpJob任务
项目介绍: Hangfire:是一个开源的job调度系统,支持分布式JOB!! Hangfire.HttpJob 是我针对Hangfire开发的一个组件,该组件和Hangfire本身是独立的.可以独立 ...
- 单个页面Request编码方式的改变,无需改动Web.config~
搞一个东西,从别人的接口接一段中文,URL传输,怎么都有乱码~~ 得到对方的编码方式是gb2312,于是用HttpUtility.UrlDecode(_smssend_content, System. ...
- WPF 动态加载主题由zip
经典主题的方式 主题战略 加载速度 本机支持 (不需要额外的代码) 支持代码为主题 (捆绑代码 & 资源成单独的文件) 支持资源层次结构中导航 动态加载 动态卸载 轻松地编辑和编译 (不需要安 ...
- 关于Linux系统启动时出现UVD not responding, Trying to reset the vcpu问题的解决
本人的老古董笔记本!不知道什么时候显卡烧坏了 每次启动Linux的时候就会出现错误,信息如下: UVD not responding, trying to reset the VCPU! 讲道理,显卡 ...
- js动态生成canvas
最近看代码发现一个小现象,就是用js动态生成的canvas在浏览器审查元素的时候,发现它没有结束标签,但是不会影响canvas上图形的绘制,同时还有一点就是在动态设置canvas宽度和高度的时候,不要 ...
- 一键部署LNMP堆栈Web应用基础架构
https://market.azure.cn/Vhd/Show?vhdId=9852&version=10884 产品详情 产品介绍LEMP/LNMP 是指一组通常一起使用来运行动态网站或者 ...
- HTML 中的特殊字符
空格符 <小于号 < >大于号 > &和好 & ¥人民币 ¥ © 版权 © ® 注册商标 ® ℃ 摄氏度 ° ...
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)
题意:在连通图中,求一条边使得加入这条边以后的消除的桥尽量多. 在同一个边双连通分量内加边肯定不会消除桥的, 求边双连通分量以后缩点,把桥当成边,实际上是要选一条最长的链. 缩点以后会形成一颗树,一定 ...