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-]);
}
}

hdu 2136(质数筛选+整数分解)的更多相关文章

  1. hdu 2582(数论相关定理+素数筛选+整数分解)

    f(n) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  2. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

  3. POJ2429_GCD &amp; LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】

    GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...

  4. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  5. python基础练习题(题目 将一个整数分解质因数。例如:输入90,打印出90=2*3*3*5)

    day9 --------------------------------------------------------------- 实例014:分解质因数 题目 将一个整数分解质因数.例如:输入 ...

  6. 整数分解 && 质因数分解

    输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <strin ...

  7. POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)

    题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd   lcm/gcd=a/gcd*b/gcd 可知a/gc ...

  8. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  9. POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】

    Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...

随机推荐

  1. 笔记-python-standard library-8.10 copy

    笔记-python-standard library-8.10 copy 1.      copy source code:Lib/copy.py python中的赋值语句不复制对象,它创建了对象和目 ...

  2. (Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明

    Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif"); pictureBox1.Image =img.Clo ...

  3. OpenCV学习笔记(七) 图像金字塔 阈值 边界

    转自: OpenCV 教程 使用 图像金字塔 进行缩放 图像金字塔是视觉运用中广泛采用的一项技术.一个图像金字塔是一系列图像的集合 - 所有图像来源于同一张原始图像 - 通过梯次向下采样获得,直到达到 ...

  4. PostgreSql基础命令及问题总结

    本章内容: 1.基本命令 基本命令 1.psql -U cdnetworks_beian -d cdnetworks_beian         #-U指定用户,-d指定数据库 2.\l        ...

  5. WCF,WebServices,WebApi区别

    http://www.cnblogs.com/hetring/p/4493137.html

  6. 50、转自知乎上android开发相见恨晚的接口

      原文链接:http://www.zhihu.com/question/33636939     程序员软件开发Android 开发JavaAndroid修改 Android开发中,有哪些让你觉得相 ...

  7. 【Luogu P1637】 三元上升子序列

    对于每个数$a_i$,易得它对答案的贡献为 它左边比它小的数的个数$\times$它右边比它大的数的个数. 可以离散化后再处理也可以使用动态开点的线段树. 我使用了动态开点的线段树,只有需要用到这个节 ...

  8. MFC深入浅出读书笔记第二部分2

    第七章  MFC骨干程序 所谓骨干程序就是指有AppWizard生成的MFC程序.如下图的层次关系是程序中常用的几个类,一定要熟记于心. 1 Document/View应用程序 CDocument存放 ...

  9. call、apply与bind在理解

    call() 方法在使用一个指定的 this 值和若干个指定的参数值的前提下调用某个函数或方法. fun.call(thisArg[, arg1[, arg2[, ...]]]) apply() 方法 ...

  10. BZOJ 1452:[JSOI2009]Count(二维树状数组)

    [JSOI2009]Count 描述 输入 输出 1 2 分析: 裸二维bit,对每个颜色建一颗bit. program count; var bit:..,..,..]of longint; a:. ...