Largest prime factor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9993    Accepted Submission(s): 3528

Problem Description
Everybody knows any number can be combined by the prime number.
Now, your task is telling me what position of the largest prime factor.
The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc.
Specially, LPF(1) = 0.
 
Input
Each line will contain one integer n(0 < n < 1000000).
 
Output
Output the LPF(n).
 
Sample Input
1
2
3
4
5
 
Sample Output
0 1 2 1 3
 
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int N = ;
bool p[N]; ///为false代表是素数
int idx[N];
void init(){
memset(p,false,sizeof(p));
int id = ;
for(int i=;i<N;i++){
if(!p[i]){
idx[i] = id++;
for(LL j=(LL)i*i;j<N;j+=i){
p[j] = true;
}
}
}
}
int getMax(int n){
int Max = -;
for(int i=;i*i<=n;i++){
if(n%i==){
while(n%i==){
n/=i;
}
Max = max(i,Max);
}
}
if(n>) Max = max(Max,n);
return Max;
}
int main()
{
init();
int n;
while(scanf("%d",&n)!=EOF){
if(n==) printf("0\n");
else{
int Max = getMax(n);
printf("%d\n",idx[Max]);
}
}
}

O(n)的素数筛

/*求第n个质数*/
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int N = ;
int n,m;
int p[];//存储素数
bool a[N]; //O(n) 素数筛
void init() {
memset(a,false,sizeof(a));//初始全部为素数
int num=;
for(int i=;i<N;++i) {
if(!a[i]) p[num++]=i;
for(int j=;(j<num&&i*p[j]<N);++j) {
a[i*p[j]]=;
if(i%p[j] == ) break;
}
}
} int main(){
init();
int n;
int k = ;
while(scanf("%d",&n)!=EOF){
if(n==) break;
printf("Case %d: %d\n",++k,p[n-]);
}
}

最新文章

  1. 【记录】Ubuntu下安装VirtualBox
  2. 【写给大家看的CSS】定位元素:使用position/display布局
  3. memcpy内存复制
  4. iOS - Mac Apache WebServer 服务器配置
  5. HDU 2689Sort it 树状数组 逆序对
  6. flask页面操作gpn接口
  7. JPA 批量新增
  8. 通过WebClient/HttpWebRequest实现http的post/get方法
  9. C#使用互斥量(Mutex)实现多进程并发操作时进程间的同步操作(进程同步)
  10. 菜鸟学习计划浅谈之Linux系统
  11. Windows Message ID 常量列表大全
  12. JAVA核心技术第二卷 第一章
  13. Android中的分层----service 层,domain层,dao 层,action层等设计
  14. GO语言的进阶之路-网络安全之proxy
  15. 在eclipse上写代码的时候,tomcat突然不能用了,重启都是闪一下就关了
  16. 用sourceTree提交代码时遇到的问题
  17. Spring Boot + MyBatis + Pagehelper 配置多数据源
  18. mac 获取idea&amp;&amp;datagrip激活码
  19. CF 920
  20. RedHat5.4 使用 centOS5源更新

热门文章

  1. 09.VUE学习之watch监听属性变化实现类百度搜索栏功能ajax异步请求数据,返回字符串
  2. 哦?原来Python 面试题是这样的,Python面试题No19
  3. [译]The Python Tutorial#10. Brief Tour of the Standard Library
  4. Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)
  5. Hive将SQL转化为MapReduce的过程
  6. Linux之ubuntu系统操作学习笔记
  7. android studio首个项目碰到的一些问题
  8. w3wp CPU 100%问题解决
  9. HDU5726 GCD
  10. BZOJ 2818: Gcd(欧拉函数)