hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901

code vs 3223题目链接:http://codevs.cn/problem/3223/

思路:主要是用了一个Meisell-Lehmer算法模板,复杂度O(n^(2/3))。讲道理,我不是很懂(瞎说什么大实话....),下面输出请自己改

 #include<bits/stdc++.h>  

 using namespace std;  

 typedef long long LL;
const LL 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,m;
while(cin >> m >> n)
{
cout<<prime[n]<<endl; //输出第n个素数
cout<<pi[m]<<endl; //输出该数是第几个素数
cout << lehmer_pi(n) <<endl; //输出区间[1,n]包含的素数
cout << lehmer_pi(n)-lehmer_pi(m) << endl; //判断区间(m,n]包含的素数 cout << lehmer_pi(n)-lehmer_pi(m-1) << endl; //输出区间[m,n]包含的素数个数 }
return ;
}

还有一个O(n^(4/3))算法,我又不懂,菜的抠脚....

 #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 ;
}

hdu 5901 count prime & code vs 3223 素数密度的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 5901 Count primes 大素数计数

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

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

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

  7. HDU 5901 Count primes 论文题

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

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

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

  9. HDU 5901 Count primes (模板题)

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

随机推荐

  1. Table 表单

    <style> table th { white-space: nowrap; } .chk { white-space: nowrap; } </style> <tab ...

  2. 如何访问wikipedia 的中文版

    http://blog.csdn.net/double_wjl/article/details/52216036

  3. Linux下Redis的安装和部署

    一.Redis介绍 Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多 ...

  4. 【辅助远程连接,可穿防火墙、NAT】一次 TeamViewer 的安装与测试

    背景: 应课程老师要求帮助某化学老师维修机器(高性能电脑),并解决老师的若干问题,在解决硬件问题(上网问题:多个网络接口)之后,化学老师提出需要远程链接到该机器,试询问之前如何实现,化学老师推荐Tea ...

  5. 轮播插件unsilder 源码解析(二)

    $.fn._active = function(className) { //当前的添加class,相邻元素去除class return this.addClass(className).siblin ...

  6. GIT文档

    GIT文档http://git.oschina.net/progit/http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c ...

  7. Newtonsoft.Json高级用法

    http://blog.csdn.net/chengmodelong/article/details/46680143

  8. JS 工具类

    之前工作用的JavaScript比较多,总结了一下工具类,和大家分享一下,有不足之处还请多多见谅!! 1. 数组工具类(arrayUtils) var arrayUtils = {}; (functi ...

  9. 【前端攻略】:玩转图片Base64编码

    引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...

  10. label和input里面文字不对齐的解决方法!

    测试了集中方法,发现不行.只能用专署标签解决这个问题. <fieldset>    <legend>神光咨询后台管理登录</legend>    <br /& ...