HDU 5901 Count primes( Meisell-Lehmer算法模板 )
链接:****传送门
题意:计算 [ 1 , n ] 之间素数的个数,(1 <= n <= 1e11)
思路:Meisell-Lehmer算法是计算超大范围内素数个数的一种算法,原理并不明白,由于英语太渣看不懂WIKI上的原理,附WIKI链接:Here
/*************************************************************************
> File Name: hdu5901.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月23日 星期二 19时38分39秒
************************************************************************/
// Meisell-Lehmer算法,快速计算超大范围(1e11)内素数个数
// HDU 5901 数据范围1e11 319MS
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
const int N = 5e6 + 2;
bool np[N];
int prime[N], pi[N];
int getprime(){
int cnt = 0;
np[0] = np[1] = true;
pi[0] = pi[1] = 0;
for(int i = 2; i < N; ++i){
if(!np[i]) prime[++cnt] = i;
pi[i] = cnt;
for(int j = 1; j <= cnt && i * prime[j] < N; ++j){
np[i * prime[j]] = true;
if(i % prime[j] == 0) break;
}
}
return cnt;
}
const int M = 7;
const int PM = 2 * 3 * 5 * 7 * 11 * 13 * 17;
int phi[PM + 1][M + 1], sz[M + 1];
void init(){
getprime();
sz[0] = 1;
for(int i = 0; i <= PM; ++i) phi[i][0] = i;
for(int i = 1; i <= M; ++i){
sz[i] = prime[i] * sz[i - 1];
for(int j = 1; j <= PM; ++j) phi[j][i] = phi[j][i - 1] - phi[j / prime[i]][i - 1];
}
}
int sqrt2(LL x){
LL r = (LL)sqrt(x - 0.1);
while(r * r <= x) ++r;
return int(r - 1);
}
int sqrt3(LL x){
LL r = (LL)cbrt(x - 0.1);
while(r * r * r <= x) ++r;
return int(r - 1);
}
LL getphi(LL x, int s){
if(s == 0) 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 + 1;
if(x <= prime[s]*prime[s]*prime[s] && x < N){
int s2x = pi[sqrt2(x)];
LL ans = pi[x] - (s2x + s - 2) * (s2x - s + 1) / 2;
for(int i = s + 1; i <= s2x; ++i) ans += pi[x / prime[i]];
return ans;
}
return getphi(x, s - 1) - getphi(x / prime[s], s - 1);
}
LL getpi(LL x){
if(x < N) return pi[x];
LL ans = getphi(x, pi[sqrt3(x)]) + pi[sqrt3(x)] - 1;
for(int i = pi[sqrt3(x)] + 1, ed = pi[sqrt2(x)]; i <= ed; ++i) ans -= getpi(x / prime[i]) - i + 1;
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 - 2) * (b - a + 1) / 2;
for (int i = a + 1; 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 - 1);
}
return sum;
}
int main(){
init();
LL n;
while(~scanf("%lld",&n)){
printf("%lld\n",lehmer_pi(n));
}
return 0;
}
HDU 5901 Count primes( 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 素数计数模板
转自:http://blog.csdn.net/chaiwenjun000/article/details/52589457 计从1到n的素数个数 两个模板 时间复杂度O(n^(3/4)) #incl ...
- HDU 5901 Count primes (模板题)
题意:给求 1 - n 区间内的素数个数,n <= 1e11. 析:模板题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...
- [素数个数模板] HDU 5901 Count primes
#include<cstdio> #include<cmath> using namespace std; #define LL long long ; bool np[N]; ...
- HDU 5901 Count primes (2016 acm 沈阳网络赛)
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5901 题意:输入n,输出n以内质数个数 模板题,模板我看不懂,只是存代码用. 官方题解链接:https ...
- HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛
题目链接 题意:求[1,n]有多少个素数,1<=n<=10^11.时限为6000ms. 官方题解:一个模板题, 具体方法参考wiki或者Four Divisors. 题解:给出两种代码. ...
- hdu 5901 Count primes
题意: 计数区间$[1, n](1 \leq n \leq 10^{11})$素数个数. 分析: 这里只介绍一种动态规划的做法. 首先要说一下[分层思想]在动态规划中非常重要,下面的做法也正是基于这一 ...
- HDU 5901 Count primes 大素数计数
题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...
随机推荐
- 开源分布式MySQL中间件探究与应用 dba+
- windowsclient开发--为你clientsign一个签名证书
郑重声明:该方法自娱自乐,尽管写入了签名,可是在微软系统免签证书不是合格的. 什么是签名? 话不多说,上图(没图说个xx): 微信windowsclient.exe安装文件: 再看还有一个.exe文件 ...
- 关于template 的23个问题
发现新大陆.曾经慢慢才知道的东西.原来有个集中营: 看看updated, 处理方式是这么的好 35.1 " id="link-to-faq-35_1" style=&qu ...
- iOS开发-自己定义重用机制给ScrollerView加入子视图
事实上这个问题我非常早就想过,仅仅是没有通过去写程序实现,昨天有人提起,我就巧了一下 不知道大家打印郭tableview:cellforrow中cell初始的次数,也就是重用池中的cell个数.这个是 ...
- C#之简易猜数字游戏
游戏规则说明: 由系统生成一个随机数,玩家有三次猜数字的机会,如果在三次内猜出数字反馈玩家猜对了,否则Game Over! 代码设计说明: 1.首先设计一个简易的欢迎界面,并提示玩家是否开始游戏: p ...
- 什么是鸭子类型(duck typing)
"当看到一仅仅鸟走起来像鸭子.游泳起来像鸭子.叫起来也像鸭子,那么这仅仅鸟就能够被称为鸭子." 我们并不关心对象是什么类型,究竟是不是鸭子,仅仅关心行为. 比方在python中.有 ...
- Java内存管理及垃圾回收总结
概述 Java和C++的一个很重要的差别在于对内存的管理.Java的自己主动内存管理及垃圾回收技术使得Java程序猿不须要释放废弃对象的内存.从而简化了编程的过程.同一时候也避免了因程序猿的疏漏而导致 ...
- SQL SERVER的浮点数类型及与C#的对应关系
SQL SERVER: float 与 real 7位数或15位数.这里说的位数,不是指小数位,而是包括整数和小数在内的位数. float的位数是多少,要看float[(n)]里的n数值是多少. n ...
- [Linux]history 显示命令执行的时间
显示历史命令之行时间 这里的环境是centos5.8 vim ~/.bashrc 或者 ~/.bash_profile 增加 export HISTTIMEFORMAT="%F %T & ...
- soapui icon以及resource的理解
https://www.soapui.org/getting-started/soapui-interface/main-window.html 以调用博客园的api为例 第一个是域名 第二个是res ...