Count primes

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2625    Accepted Submission(s): 1337

Problem Description
Easy question! Calculate how many primes between [1...n]!
 
Input
Each line contain one integer n(1 <= n <= 1e11).Process to end of file.
 
Output
For each case, output the number of primes in interval [1...n]
 
Sample Input
2
3
10
 
Sample Output
1
2
4

help

C/C++:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
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(cin>>n)
cout<<lehmer_pi(n)<<endl;
return ;
}

hdu 5901 Count primes (meisell-Lehmer)的更多相关文章

  1. HDU 5901 Count primes 论文题

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

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

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

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

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

  4. hdu 5901 Count primes

    题意: 计数区间$[1, n](1 \leq n \leq 10^{11})$素数个数. 分析: 这里只介绍一种动态规划的做法. 首先要说一下[分层思想]在动态规划中非常重要,下面的做法也正是基于这一 ...

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

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

  6. hdu 5901 Count primes 素数计数模板

    转自:http://blog.csdn.net/chaiwenjun000/article/details/52589457 计从1到n的素数个数 两个模板 时间复杂度O(n^(3/4)) #incl ...

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

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

  8. HDU 5901 Count primes 大素数计数

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

  9. HDU 5901 Count primes (模板题)

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

随机推荐

  1. java集合之HashSet哈希SET基础

    hashSet实现set接口.serializable.cloneable接口.不允许有重复值,但可以null,不保证与插入顺序一致. HashSet迭代的时间与HashSet实例的大小(元素的数量) ...

  2. std::weak_ptr

    weak_ptr 是一种不控制对象生命周期的智能指针, 它指向一个 shared_ptr 管理的对象. 进行该对象的内存管理的是那个强引用的 shared_ptr. weak_ptr只是提供了对管理对 ...

  3. vue —— Toast 内 加变量

    toast正常使用: 在页面内引入: import { Toast } from 'mint-ui' 使用的时候,简单到飞起: Toast('领取成功'); 而如果想在toast中加入变量,也很简单: ...

  4. linux系统状态网络、权限、用户大杂烩

    来来来,我们聊一下liunx系统相关的知识!! 首先从查询网络配置开始 ifconfig 查询.设置网卡和ip等参数 ifup,ifdown 脚本命令,更简单的方式启动关闭网络 ip命令是结合了ifc ...

  5. XML利用接口显示并导入到数据库

    //控制器代码 /// <summary> /// 页面 /// </summary> /// <returns></returns> public A ...

  6. The usage of Markdown---代码块

    目录 1. 序言 2. 代码块 3. 引用中的代码 4. 列表中的代码块 更新时间:2019.09.14 1. 序言   在写技术博客的时候,我们常常需要添加一下代码块用来做演示说明,实际上在这篇博客 ...

  7. python3.8安装flask出现错误“ModuleNotFoundError: No module named '_ctypes'”

    本想在CentOS下配置flask+nginx+uwsgi环境,结果安装最基础的flask包都出了问题...以下是我的环境: 服务器:阿里云ECS CentOS7 python版本:3.8.0 问题描 ...

  8. unity image 设置图片

    从任意文件目录下读取文件并在unity中显示: 1)读取目标文件 byte[] imageByte = File.ReadAllBytes(imagePath); 2)转换成纹理 texture.Lo ...

  9. linux小白的入门和目标。

    大家好! 我是一名Linux小白,有幸来到马哥教育这个大家庭与各位同学在未来的五个月里一起学习Linux技术!尽管Linux对于刚接触到的新手会很难,但是我知道痛苦只是暂时的,满路荆棘的后面必是明亮宽 ...

  10. fenby C语言 P32

    a[0] a[0][0] a[0][1] a[0][2] a[1] a[1][0] a[1][1] a[1][2]//一维数组 int a[2][3]//二维数组int (*p)[3]=a; #inc ...