转自:http://blog.csdn.net/chaiwenjun000/article/details/52589457

计从1到n的素数个数

两个模板

时间复杂度O(n^(3/4))

 #include <bits/stdc++.h>
#define ll long long
using namespace std;
ll f[],g[],n;
void init(){
ll i,j,m;
for(m=;m*m<=n;++m)f[m]=n/m-;
for(i=;i<=m;++i)g[i]=i-;
for(i=;i<=m;++i){
if(g[i]==g[i-])continue;
for(j=;j<=min(m-,n/i/i);++j){
if(i*j<m)f[j]-=f[i*j]-g[i-];
else f[j]-=g[n/i/j]-g[i-];
}
for(j=m;j>=i*i;--j)g[j]-=g[j/i]-g[i-];
}
}
int main(){
while(scanf("%I64d",&n)!=EOF){
init();
cout<<f[]<<endl;
}
return ;
}

第二个 时间复杂度O(n^(2/3))

 //Meisell-Lehmer
//G++ 218ms 43252k
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
const int N = 5e6 + ;
bool np[N];
int prime[N], pi[N];
int getprime()
{
int cnt = ;
np[] = np[] = true;
pi[] = pi[] = ;
for(int i = ; i < N; ++i)
{
if(!np[i]) prime[++cnt] = i;
pi[i] = cnt;
for(int j = ; j <= cnt && i * prime[j] < N; ++j)
{
np[i * prime[j]] = true;
if(i % prime[j] == ) break;
}
}
return cnt;
}
const int M = ;
const int PM = * * * * * * ;
int phi[PM + ][M + ], sz[M + ];
void init()
{
getprime();
sz[] = ;
for(int i = ; i <= PM; ++i) phi[i][] = i;
for(int i = ; i <= M; ++i)
{
sz[i] = prime[i] * sz[i - ];
for(int j = ; j <= PM; ++j) phi[j][i] = phi[j][i - ] - phi[j / prime[i]][i - ];
}
}
int sqrt2(LL x)
{
LL r = (LL)sqrt(x - 0.1);
while(r * r <= x) ++r;
return int(r - );
}
int sqrt3(LL x)
{
LL r = (LL)cbrt(x - 0.1);
while(r * r * r <= x) ++r;
return int(r - );
}
LL getphi(LL x, int s)
{
if(s == ) 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 + ;
if(x <= prime[s]*prime[s]*prime[s] && x < N)
{
int s2x = pi[sqrt2(x)];
LL ans = pi[x] - (s2x + s - ) * (s2x - s + ) / ;
for(int i = s + ; i <= s2x; ++i) ans += pi[x / prime[i]];
return ans;
}
return getphi(x, s - ) - getphi(x / prime[s], s - );
}
LL getpi(LL x)
{
if(x < N) return pi[x];
LL ans = getphi(x, pi[sqrt3(x)]) + pi[sqrt3(x)] - ;
for(int i = pi[sqrt3(x)] + , ed = pi[sqrt2(x)]; i <= ed; ++i) ans -= getpi(x / prime[i]) - i + ;
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 - ) * (b - a + ) / ;
for (int i = a + ; 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 - );
}
return sum;
}
int main()
{
init();
LL n;
while(~scanf("%lld",&n))
{
printf("%lld\n",lehmer_pi(n));
}
return ;
}

hdu 5901 Count primes 素数计数模板的更多相关文章

  1. HDU 5901 Count primes( Meisell-Lehmer算法模板 )

    链接:传送门 题意:计算 [ 1 , n ] 之间素数的个数,(1 <= n <= 1e11) 思路:Meisell-Lehmer算法是计算超大范围内素数个数的一种算法,原理并不明白,由于 ...

  2. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

  3. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. HDU 5901 Count primes 大素数计数

    题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...

  5. [素数个数模板] HDU 5901 Count primes

    #include<cstdio> #include<cmath> using namespace std; #define LL long long ; bool np[N]; ...

  6. HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:求[1,n]有多少个素数,1<=n<=10^11.时限为6000ms. 官方题解:一个模板题, 具体方法参考wiki或者Four Divisors. 题解:给出两种代码. ...

  7. HDU 5901 Count primes (模板题)

    题意:给求 1 - n 区间内的素数个数,n <= 1e11. 析:模板题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...

  8. 求1-1e11内的素数个数(HDU 5901 Count primes )

    参考链接:https://blog.csdn.net/Dylan_Frank/article/details/54428481 #include <bits/stdc++.h> #defi ...

  9. HDU 5901 Count primes (2016 acm 沈阳网络赛)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5901 题意:输入n,输出n以内质数个数 模板题,模板我看不懂,只是存代码用. 官方题解链接:https ...

随机推荐

  1. GEF-whole-upload教程中遇到的问题及解决方案

    最近在学习GEF开发,使用的是GEF-whole-upload这个教程.由于教程当时所使用的版本与本人使用的版本有一些差异,中间出现了不少问题,现在将解决方案分享给大家. 本人使用的Eclipse版本 ...

  2. 尽量不用char*作为hash_map的key

    引子: 同事前几天用hash_map时发现一些问题.当时的场景是有一些字符串char*,要去对应某种类型的对象.同事的做法是: 尝试用char*作为key进行hash.编译通过,但运行时不正常,ins ...

  3. 关于PDF.NET开发框架对Mysql Sqlite PostgreSQL数据库分页支持的个人看法

    关于PDF.NET开发框架的名字由来  在设计www.pwmis.com站点的时候,考虑到架构的兼容性和将来升级的可能性,最重要的是没有足够的时间去为网站添加和维护很多复杂的程序,所以在借鉴前人成功经 ...

  4. cooking eggs

    1: what is egg? what's the shape of it in details? 2: can egg run like this http://item.taobao.com/i ...

  5. js 调用父窗口的方法

    opener.show(); 父窗体需要顶一个show() 方法 父面页代码: <!DOCTYPE HTML PUBLIC "-//IETF//DTD LEVEL1//EN" ...

  6. 在OpenStack虚拟机实例中创建swap分区的一种方法

    测试组里一个同学负责MapR的搭建,MapR文档中建议每个节点上至少有24GB的swap分区,不知道MapR为啥会有这种反人类的建议……swap无非就是一块顺序读写的磁盘空间,莫非省着内存不用,用sw ...

  7. android jni——helloworld

    看了网上好多牛人写的学习系列都是用HelloWorld作为开始,我们这里也用HelloWorld来开始我们的学习,首先我们来介绍下JNI吧. JNI作为java代码和C/C++的桥梁而存在的,为了让j ...

  8. spring mvc下shiro的session,request等问题

    最近的一个项目使用的是spring mvc,权限框架使用的是shiro. 不过有一个问题一直困扰着我,现在的session到底是谁的session,是servlet的还是shiro的. 于是我把spr ...

  9. MySQL 笔记 —— 日期和时间函数

    [TOC] 获取当前日期的函数和获取当前时间的函数 CURDATE()和CURRENT_DATE()函数获取当前日期:CURTIME()和CURRENT_TIME()函数获取当前时间. mysql&g ...

  10. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第四章:更高级的数据管理

    在这一章我们将学习如何正确地删除分类信息,如何向数据库填充种子数据,如何使用Code First Migrations基于代码更改来更新数据库,然后学习如何执行带有自定义错误消息的验证. 注意:如果你 ...