hdu 5901 Count primes 素数计数模板
转自: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 素数计数模板的更多相关文章
- HDU 5901 Count primes( Meisell-Lehmer算法模板 )
链接:传送门 题意:计算 [ 1 , n ] 之间素数的个数,(1 <= n <= 1e11) 思路:Meisell-Lehmer算法是计算超大范围内素数个数的一种算法,原理并不明白,由于 ...
- HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
- hdu 5901 Count primes (meisell-Lehmer)
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 5901 Count primes 大素数计数
题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...
- [素数个数模板] HDU 5901 Count primes
#include<cstdio> #include<cmath> using namespace std; #define LL long long ; bool np[N]; ...
- HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛
题目链接 题意:求[1,n]有多少个素数,1<=n<=10^11.时限为6000ms. 官方题解:一个模板题, 具体方法参考wiki或者Four Divisors. 题解:给出两种代码. ...
- HDU 5901 Count primes (模板题)
题意:给求 1 - n 区间内的素数个数,n <= 1e11. 析:模板题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...
- 求1-1e11内的素数个数(HDU 5901 Count primes )
参考链接:https://blog.csdn.net/Dylan_Frank/article/details/54428481 #include <bits/stdc++.h> #defi ...
- HDU 5901 Count primes (2016 acm 沈阳网络赛)
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5901 题意:输入n,输出n以内质数个数 模板题,模板我看不懂,只是存代码用. 官方题解链接:https ...
随机推荐
- BroadcastReceiver基础总结
BroadcastReceiver基础总结 BroadcastReceiver是Android四大组件之一,主要负责接收系统或其他程序发出的广播,在开发中,通常用做事件驱动的起源,比如开机就要开启一个 ...
- GCD与多线程
GCD与多线程 GCD,全称Grand Central Dispath,是苹果开发的一种支持并行操作的机制.它的主要部件是一个FIFO队列和一个线程池,前者用来添加任务,后者用来执行任务. GCD中的 ...
- WPF制作的小型笔记本
WPF制作的小型笔记本-仿有道云笔记 楼主所在的公司不允许下载外部资源, 不允许私自安装应用程序, 平时记录东西都是用记事本,时间久了很难找到以前记的东西. 平时在家都用有道笔记, 因此就模仿着做了一 ...
- 数模学习笔记(五)——BP神经网络
1.BP神经网络是一种前馈型网络(各神经元接受前一层的输入,并输出给下一层,没有反馈),分为input层,hide层,output层 2.BP神经网络的步骤: 1)创建一个神经网络:newff a.训 ...
- 下载编译Chrome详细步骤
文章来源:http://blog.csdn.net/allendale/article/details/9262833 参考:http://dev.chromium.org/developers/ho ...
- jquery uploadifive使用
应为考虑到flash将逐渐被淘汰,所以选择了uploadfive完成上传 js文件和css文件自行下载,我上传了免费版(啃爹的官网竟然收费) 文件引入之后: <input type=" ...
- dom4解析xml格式文件实例
以下给4种常见的xml文件的解析方式的分析对比: DOM DOM4J JDOM SAX 解析XML文件的几种方式和区别答: Dom解析 在内存中创建一个DOM树,该结构通常需要加载整个文档然后才能做工 ...
- .Net程序员学用Oracle系列(9):系统函数(上)
<.Net程序员学用Oracle系列:导航目录> 本文大纲 1.字符函数 1.1.字符函数简介 1.2.语法说明及案例 2.数字函数 2.1.数字函数简介 2.2.语法说明及案例 3.日期 ...
- js 禁止复制粘贴全选
// 取消右键菜单document.oncontextmenu = function(e){ var t = e || window.event; var elm = t.target || t.sr ...
- python命令调用函数os.popen
参考自xerosploit 描述:利用os.popen()函数调用系统命令nmap进行扫描,并用grep命令对扫描结果关键内容进行提取 代码 #!/usr/bin/env pthon #--*--co ...