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. Django2.2使用mysql数据库pymysql版本不匹配问题的解决过程与总结

    前置条件 django版本:2.2.1 python版本:3.6.6 mysql版本:mysql-community8.0.15 问题 在搭建django项目,配置mysql数据库时遇到无法迁移数据库 ...

  2. [译]The Python Tutorial#7. Input and Output

    [译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...

  3. (ADO.NET小知识点汇总)看到什么记什么

    1.数据库连接池:在同时连接数不多的情况下, 打开一个链接往数据库导1W条数据的耗时 跟 导一条数据就打开跟关闭数据库连接的耗时 两者其实相差不大,这是为什么呢?打开关闭的本身不是有很多耗时吗?这是因 ...

  4. vim编辑器最简单使用方法

    i 输入模式 :q 不保存退出 :q! 强制退出 :wq 保存退出 j 下 k 上 h 左 l 右 gg start G end x 往后删 X 往前删 yy 复制行 p 粘贴 dd 剪切行 u 撤销 ...

  5. 学好java,做好工程师必读的15本书

    学好java,做好工程师必读的15本书   一.Java编程入门类 对于没有Java编程经验的程序员要入门,随便读什么入门书籍都一样,这个阶段需要你快速的掌握Java基础语法和基本用法,宗旨就是“囫囵 ...

  6. Python + Selenium 自动化环境搭建过程

    1.  所需组建 1.1  Selenium for python 1.2  Python 1.3  Notepad++ 作为刚初学者,这里不建议使用Python IDE工具,选择一个功能强大的记事本 ...

  7. python 学习分享-面向对象2

    面向对象进阶 静态方法 一种普通函数,就位于类定义的命名空间中,它不会对任何实例类型进行操作.使用装饰器@staticmethod定义静态方法.类对象和实例都可以调用静态方法: class Foo: ...

  8. python安装pattern失败

    做文本分类需要用到pattern.en进行词形还原,安装了一上午都没有成功,mysqlclient安装失败.最后解决办法,pip install --only-binary :all: mysqlcl ...

  9. linux系统mysql密码修改脚本

    编写了一个适用于阿里云linux系统 mysql密码修改脚本,使用阿里云提供的一键安装包配置后,如果account.log文件删除,并且忘记mysql密码时,可以通过脚本来重置mysql密码. 附:一 ...

  10. 【bzoj4836】[Lydsy2017年4月月赛]二元运算 分治+FFT

    题目描述 定义二元运算 opt 满足   现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c  你需要求出有多少对 (i, j) 使得 a_ ...