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. POJ1390Blocks(DP+好题+抽空再来理解理解)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4744   Accepted: 1930 Descriptio ...

  2. BZOJ4539: [Hnoi2016]树

    复制的树缩点,主席树查k小,毫无技术含量,纯码农题. #include<bits/stdc++.h> #define u first #define v second #define F ...

  3. SharePoint Claim base authentication EnsureUser 不带claim(i:0#.w|)user Failed

    环境信息: 带有Form base authentication(FBA).Active Directory Federation Services(ADFS).以及windows Authentic ...

  4. http执行过程分析

    执行过程: 1.用户在浏览器(客户端)里输入或者点击一个网址链接: 2.浏览器通过网址域名查找ip地址.DNS查找方式是通过浏览器缓存(会记录DNS记录)→系统缓存→TCP/IP参数中设置的首选DNS ...

  5. protobuf中文教程(第一篇)

    声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...

  6. MyBatis源码分析(3)—— Cache接口以及实现

    @(MyBatis)[Cache] MyBatis源码分析--Cache接口以及实现 Cache接口 MyBatis中的Cache以SPI实现,给需要集成其它Cache或者自定义Cache提供了接口. ...

  7. Linux 新手的学习笔记

    系统分区: MBR分区(主分区不超过4个,单个分区不超过2TB) fdisk -l fdisk /dev/sdb n(添加一个分区)p(添加一个主分区,主分区)e(添加一个扩展分区)l(添加一个逻辑分 ...

  8. HiHo Coder字典树 TrieTree

    题目地址:http://hihocoder.com/problemset/problem/1014 不得不吐槽一下这个OJ,题目质量是很好的,但是提交代码后返回的信息也太少了吧!!!本机测试无误,提交 ...

  9. LUA 学习笔记

    1.C# 与 LUAC#调用LUA比较简单,但LUA调用C#,有两种方法,一种是直接反射调用,但这种方法有局限性,比如性能低,在IOS平台无法使用反射,因此一般使用WARP方法,即把C#代码注册到LU ...

  10. Excel——OFFSET函数

    1.首先看下offset函数的参数设置: 说明:height,width表面它的返回值可以是一个数组,而并非一个值.这样,它就可以用于数据有效性等. 2.使用offset实现转置: 3.offset函 ...