POJ3048 Max Factor
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
(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
* Lines 2..N+1: The serial numbers to be tested, one per line
Output
Sample Input
4
36
38
40
42
Sample Output
38
Hint
19 is a prime factor of 38. No other input number has a larger prime factor.
Source
我连线性筛都写挂了一次,真是没救了...
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <stack>
using namespace std;
typedef long long LL;
const int MAXN = ;
const int MAXM = ;
int n,N,a[MAXN],maxl[MAXN];
int cnt,prime[MAXM],ans;
bool vis[MAXM]; inline int getint(){
int w=,q=; char c=getchar(); while((c<''||c>'') && c!='-') c=getchar();
if(c=='-') q=,c=getchar(); while (c>=''&&c<='') w=w*+c-'',c=getchar(); return q?-w:w;
} inline void work(){
n=getint(); for(int i=;i<=n;i++) a[i]=getint(),N=max(a[i],N); ans=; int x;
for(int i=;i<=N;i++) { if(!vis[i]) prime[++cnt]=i; for(int j=;j<=cnt && i*prime[j]<=N;j++) { vis[i*prime[j]]=; if(i%prime[j]==) break;} }
for(int i=;i<=n;i++) {
x=a[i];
for(int j=;j<=cnt;j++) {
if(x%prime[j]!=) continue;
maxl[i]=prime[j]; while(x%prime[j]==) x/=prime[j];
if(x==) break;
}
}
for(int i=;i<=n;i++) if(maxl[i]>maxl[ans]) ans=i;
printf("%d",a[ans]);
} int main()
{
work();
return ;
}
POJ3048 Max Factor的更多相关文章
- HDU-2710 Max Factor
看懂: Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Max Factor(素数筛法)题解
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 抓其根本(一)(hdu2710 Max Factor 素数 最大公约数 最小公倍数.....)
素数判断: 一.根据素数定义,该数除了1和它本身以外不再有其他的因数. 详见代码. int prime() { ; i*i<=n; i++) { ) //不是素数 ; //返回1 } ; //是 ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- ACM Max Factor
To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) co ...
- hdu 2710 Max Factor 数学(水题)
本来是不打算贴这道水题的,自己却WA了三次.. 要考虑1的情况,1的质因子为1 思路:先打表 ,然后根据最大质因子更新结果 代码: #include<iostream> #include& ...
- poj 3048 Max Factor(素数筛)
这题就是先写个素数筛,存到prime里,之后遍历就好,取余,看是否等于0,如果等于0就更新,感觉自己说的不明白,引用下别人的话吧: 素数打表,找出20000之前的所有素数,存入prime数组,对于每个 ...
- Max Factor 2710 最大的合数的质数因子
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2710 思路:用类似“埃氏筛法”求素数的方法 只是不在把合数标记为1 而是标记为他是因子数. 最后比较大小即 ...
- hdu2710 Max Factor
题目 //下面这个是最先用的方法,因为学姐先讲完这个,所以懒得写代码,就将就着这个用,结果搞了老半天,还是错了,心累.. #include<stdio.h> #include<str ...
随机推荐
- Basic: Fisher's transform
来源:http://bbs.chinahrd.net/thread-709742-1-1.html,Kenneth的回答. z = 0.5 * ln [ (1+r)/(1-r) ]" C0 ...
- SQL80001: Incorrect syntax near ':'
原文连接:http://geekswithblogs.net/tonyt/archive/2010/03/05/138363.aspx SQL80001: Incorrect syntax nea ...
- Post model至Web Api创建或是保存数据
前一篇<Post model至Web Api>http://www.cnblogs.com/insus/p/4343538.html中,使用Post来从Web Api获取数据.由于Post ...
- Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- Linux 网络编程详解六(多进程服务器僵尸进程解决方案)
小结:在点对点p2p程序中,服务器端子程序退出,子进程会主动发送信号,关闭父进程,但是这种模式导致服务器只能支持一个客户端连接,本章节中使用新的框架,子进程退出,不主动发送信号关闭父进程,而是父进程安 ...
- web学习第一章
web学习第一章 我是大概9月10日开始走上IT之路的,一开始学习了小段时间的自动化办公软件, 昨天我开始学习客户端网页编程,我了解什么是WEB,一些比较老古董的计算模式和发展历史,印象最让我深刻 ...
- Autofac中的属性注入功能使用
使用依赖注入容器时,大部分都是使用构造函数来注入或者是xml配置文件.也有很多支持属性注入.Autofac就是其中一个. 1 为什么要有属性注入? 对于一些使用特频繁的类或者方法,很多类都会用到,那么 ...
- 获取图片base64编码的几种方法
前文中我们聊了 Data URI 和 base64编码,稍微回顾下.base64编码 是将数据用 64 个可打印的字符进行编码的方式,任何数据底层实现都是二进制,所以都可以进行 base64编码,ba ...
- 深入理解OOP(三):多态和继承(动态绑定和运行时多态)
在前面的文章中,我们介绍了编译期多态.params关键字.实例化.base关键字等.本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定. 深入理解OOP(一):多态和继承(初期绑定和编译时 ...
- keepalived+LVS 实现双机热备、负载均衡、失效转移 高性能 高可用 高伸缩性 服务器集群
本章笔者亲自动手,使用LVS技术实现实现一个可以支持庞大访问量.高可用性.高伸缩性的服务器集群 在读本章之前,可能有不少读者尚未使用该技术,或者部分读者使用Nginx实现应用层的负载均衡.这里大家都可 ...