hdu 2136(质数筛选+整数分解)
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
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.
2
3
4
5
#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(质数筛选+整数分解)的更多相关文章
- hdu 2582(数论相关定理+素数筛选+整数分解)
f(n) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- POJ2429_GCD & LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- python基础练习题(题目 将一个整数分解质因数。例如:输入90,打印出90=2*3*3*5)
day9 --------------------------------------------------------------- 实例014:分解质因数 题目 将一个整数分解质因数.例如:输入 ...
- 整数分解 && 质因数分解
输入整数(0-30)分解成所有整数之和.每四行换行一次. 一种方法是通过深度优先枚举出解.通过递归的方式来实现. #include <stdio.h> #include <strin ...
- 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 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
随机推荐
- 常用模块之 re shutil configparser hashlib xldt和xlwd
shutil 高级文件处理模块 封装的更简单了 主要是文件的复制,移动,压缩解压缩 需要保证目标文件已经存在shutil.copymode('test.txt','testcopy4.txt') 压缩 ...
- (原创)task和function语法的使用讨论(Verilog,CPLD/FPGA)
1. Abstract function和task语句的功能有很多的相似之处,在需要有多个相同的电路生成时,可以考虑使用它们来实现.因为个人使用它们比较少,所以对它们没有进行更深的了解,现在时间比较充 ...
- MySQL之体系结构与存储实例
定义数据库和实例 在数据库领域中有两个词很容易混淆,这就是“数据库”(database)和“实例”(instance).作为常见的数据库术语,这两个词的定义如下: 数据库:物理操作系统文件或其他形式文 ...
- 03015_JSTL技术
1.JSTL概述 (1)JSP(JSP Standard Tap Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要替代js ...
- 部署 Windows PowerShell Web 访问
部署 Windows PowerShell Web 访问 适用对象:Windows Server 2012, Windows Server 2012 R2 Windows PowerShell® We ...
- 在 Amazon AWS 搭建及部署网站:(二)安装、配置软件,启动网站
现在,我们已经有了一台EC2主机,具备了基本的硬件环境.下面,开始软件环境的配置. 第一步:连接服务器 后面所有的一切,都需要在SSH终端窗口操作.首先,我们需要一个SSH客户端.PuTTY是很常用的 ...
- Windows核心编程小结3
内存映射和堆栈 内存映射文件 内存映射文件可以用于3个不同的目的: 系统使用内存映射文件,以便加载和执行.exe和DLL文件.这可以大大节省页文件空间和应用程序启动运行所需的时间. 可以使用内存映射文 ...
- Python Unicode与中文处理
转自:http://blog.csdn.net/dao123mao/article/details/5396497 python中的unicode是让人很困惑.比较难以理解的问题,本文力求彻底解决这些 ...
- WINDOWS批量替换不同文件夹下的相同文件
今天帮媳妇解决的问题,记录一下,也许以后有用 例子: N个文件夹下有同一个文件(common.php),但是,现在对common.php文件进行了大量修改. 现在想用最新的common.php替换掉所 ...
- Spring boot 整合jsp、thymeleaf、freemarker
1.创建spring boot 项目 2.pom文件配置如下: <dependencies> <dependency> <groupId>org.springfra ...