求第 i 个素数 Meissel Lehmer Algorithm + 二分 【模板】
1473: L先生与质数V3
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 1348 Solved: 147
[Submit][Status][Web
Board]
Description
Input
每个测试例一行,输入一个数字n(0<n<=3000000),输入0表示结束。
Output
Case X: Y
Sample Input
1
2
3
4
10
100
0
Sample Output
Case 1: 2
Case 2: 3
Case 3: 5
Case 4: 7
Case 5: 29
Case 6: 541 每次二分判断即可;
(参考别人的代码)
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
typedef long long LL;
const int N = 5e6 + 2;//通过知道前面的n^1/3的=质数可以推断后面n^2/3的质数所以可以适当减小
bool np[N];
int prime[N], pi[N];
int getprime()
{
int cnt = 0;
np[0] = np[1] = true;
pi[0] = pi[1] = 0;
for(int i = 2; i < N; ++i)
{
if(!np[i]) prime[++cnt] = i;
pi[i] = cnt;
for(int j = 1; j <= cnt && i * prime[j] < N; ++j)
{
np[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
return cnt;
}
const int M = 7;//为了减小内存可以不过是质数
const int PM = 2 * 3 * 5 * 7 * 11 * 13 * 17;//为了减小内存可以不过要按质数减小如去掉17
int phi[PM + 1][M + 1], sz[M + 1];
void init()
{
getprime();
sz[0] = 1;
for(int i = 0; i <= PM; ++i) phi[i][0] = i;
for(int i = 1; i <= M; ++i)
{
sz[i] = prime[i] * sz[i - 1];
for(int j = 1; j <= PM; ++j) phi[j][i] = phi[j][i - 1] - phi[j / prime[i]][i - 1];
}
}
int sqrt2(LL x)
{
LL r = (LL)sqrt(x - 0.1);
while(r * r <= x) ++r;
return int(r - 1);
}
int sqrt3(LL x)
{
LL r = (LL)cbrt(x - 0.1);
while(r * r * r <= x) ++r;
return int(r - 1);
}
LL getphi(LL x, int s)
{
if(s == 0) return x;
if(s <= M) return phi[x % sz[s]][s] + (x / sz[s]) * phi[sz[s]][s];
if(x <= prime[s]*prime[s]) return pi[x] - s + 1;
if(x <= prime[s]*prime[s]*prime[s] && x < N)
{
int s2x = pi[sqrt2(x)];
LL ans = pi[x] - (s2x + s - 2) * (s2x - s + 1) / 2;
for(int i = s + 1; i <= s2x; ++i) ans += pi[x / prime[i]];
return ans;
}
return getphi(x, s - 1) - getphi(x / prime[s], s - 1);
}
LL getpi(LL x)
{
if(x < N) return pi[x];
LL ans = getphi(x, pi[sqrt3(x)]) + pi[sqrt3(x)] - 1;
for(int i = pi[sqrt3(x)] + 1, ed = pi[sqrt2(x)]; i <= ed; ++i) ans -= getpi(x / prime[i]) - i + 1;
return ans;
}
LL lehmer_pi(LL x)
{
if(x < N) return pi[x];
int a = (int)lehmer_pi(sqrt2(sqrt2(x)));
int b = (int)lehmer_pi(sqrt2(x));
int c = (int)lehmer_pi(sqrt3(x));
LL sum = getphi(x, a) +(LL)(b + a - 2) * (b - a + 1) / 2;
for (int i = a + 1; i <= b; i++)
{
LL w = x / prime[i];
sum -= lehmer_pi(w);
if (i > c) continue;
LL lim = lehmer_pi(sqrt2(w));
for (int j = i; j <= lim; j++) sum -= lehmer_pi(w / prime[j]) - (j - 1);
}
return sum;
}
int main()//因为统计了质数个数可以用在线判断用二分
{
LL n,l,r,mid;
int sum=1;
init();
while(scanf("%lld",&n)!=EOF)
{
if(n==0)
break;
l=1,r=50000000;
while(l<r) {
mid=(l+r)/2; LL t=lehmer_pi(mid);
if(t>=n)
r=mid;
else
l=mid+1;
}
printf("Case %d: %lld\n",sum++,l);
}
return 0;
}
求第 i 个素数 Meissel Lehmer Algorithm + 二分 【模板】的更多相关文章
- Meissel Lehmer Algorithm 求前n个数中素数个数 【模板】
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 求1e11以内的素数
有两种做法,一种是打表,另一种是直接求. 打表 将1e11每隔len(len=2000w)个数字统计一下该区间内素数的个数,比如cnt[1] 表示[1,len]以内有多少个素数,cnt[2]表示[le ...
- 求第N个素数
埃拉托斯特尼筛法 如果求第n 个素数,有一个数学公式可以得到第n 个素数的上界:uper=n*ln(n)+n*ln(ln(n)),n>=6.如果一个数是素数那么这个数的倍数是非素数因此例如2是素 ...
- Python练习题 026:求100以内的素数
[Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...
- Python3求m以内的素数、求m个数中最小的n个数
[本文出自天外归云的博客园] 题1:求m以内的素数(m>2) def find_all_primes_in(m): def prime(num): for i in range(2, num): ...
- 【C语言】输入一个整数N,求N以内的素数之和
[C语言]输入一个整数N,求N以内的素数之和 /* ========================================================================== ...
- 求小于10000的素数的个数 Exercise06_10
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求小于10000的素数的个数 * */ public class Exercise06_10 { public static ...
- 斐波那契数列(递归)&求100以内的素数
Java 5 添加了 java.util.Scanner 类,这是一个用于扫描输入文本的新的实用程序.它是以 前的 StringTokenizer 和 Matcher 类之间的某种结合.由于任何数据都 ...
- 爆炸快求1~n有多少素数
这个求一千亿以内的素数大约用6780ms #include <stdio.h> #include <iostream> #include <string.h> #i ...
随机推荐
- rtmp与hls流媒体服务器搭建:ubuntu下Nginx搭建初探与rtmp-module的添加
关键词:Nignx(http服务器):rtmp,hls(流媒体服务) 前言:感谢开源,感谢战斗民族.现在在做流媒体服务的一些工作,流媒体服务器搭建的网上教程多入牛毛,但是细细查看,发现很多同志贴上来的 ...
- 用nginx搭建http/rtmp/hls协议的MP4/FLV流媒体服务器
前前后后搭建了两三个星期,终于可以告一段落,nginx实在是有点强大.写一篇笔记来记录一下这个过程中的思路和解决方案. 一.搭建nginx平台: 基本是基于http://blog.csdn.net/x ...
- 原来问题在这里-我的memory leak诊断历程
自从公司开始将java作为主要开发语言后,C++与java的混合应用日趋增多. java与C++的通信主要也是使用JNI来完成,这并没有什么问题.对于这样的混合应用项目来说,最大的噩梦莫过于memor ...
- spark减少提交jar包处理
spark一个应用,算上依赖一百多兆.每一次都如此,坑. 首先是<packing>jar</packing>这只为打包为jar,在plugin中增加一个assembly插件,这 ...
- docker 局域网仓库(registry)
sudo docker pull daocloud.io/registry 安装仓库(registry) 使用daocloud/aliyun镜像吧,官网仓库真心没法用 sudo docker r ...
- Azure上部署Barracuda WAF集群 --- 1
公有云上的第一层防护,一般要采用Proxy模式的安全设备. 梭子鱼的WAF是最早支持Azure China公有云的安全设备. 本文记录了在Azure上安装部署Barracuda的过程.下面就是安装部署 ...
- AutoMapper学习笔记
进入公司后学习到的东西,之前记录在github上 现在搬运过来 AutoMapperDemo 需要安装两个包 AutoMapper AutoMapper.Extensions.Microsoft.De ...
- layui 文件上传加进度条
1.页面 <div class="layui-row layui-col-space5"> <div class="layui-form-item&qu ...
- 第 2 章 Python 语言入⻔
目录 2.1低而长的学习曲线 2.2Python的优势 2.3在你的计算机中安装Python 2.4如何运行Python程序 2.5文本编辑器 2.6寻求帮助 Python语言是一种流行的编程语言,在 ...
- Umbraco 的template使用的默认Model问题
Umbraco中的Template默认都继承自 Umbraco.Web.Mvc.UmbracoTemplatePage @inherits Umbraco.Web.Mvc.UmbracoTemplat ...