题目链接

题意:求[1,n]有多少个素数,1<=n<=10^11。时限为6000ms。

官方题解:一个模板题, 具体方法参考wiki或者Four Divisors

题解:给出两种代码。

   第一种方法Meisell-Lehmer算法只需265ms。

   第二种方法不能运行但是能AC,只需35行。

第一种:

//Meisell-Lehmer
#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 ;
}

第二种:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e11;
const ll maxp=sqrt(maxn)+;
ll f[maxp],g[maxp];
ll solve(ll n)
{
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-];
}
return f[];
}
int main()
{
ll n;
while(scanf("%lld",&n)!=EOF)
printf("%lld\n",solve(n));
return ;
}

HDU 5901 Count primes (1e11内的素数个数) -2016 ICPC沈阳赛区网络赛的更多相关文章

  1. HDU 5878 I Count Two Three (打表+二分查找) -2016 ICPC 青岛赛区网络赛

    题目链接 题意:给定一个数n,求大于n的第一个只包含2357四个因子的数(但是不能不包含其中任意一种),求这个数. 题解:打表+二分即可. #include <iostream> #inc ...

  2. HDU 5894 hannnnah_j’s Biological Test (组合数学) -2016 ICPC沈阳赛区网络赛

    题目链接 #include <map> #include <queue> #include <math.h> #include <stdio.h> #i ...

  3. HDU 5898 odd-even number (数位DP) -2016 ICPC沈阳赛区网络赛

    题目链接 题意:一个数字,它每个数位上的奇数都形成偶数长度的段,偶数位都形成奇数长度的段他就是好的.问[L , R]的好数个数. 题解:裸的数位dp, 从高到低考虑每个数位, 状态里存下到当前位为止的 ...

  4. HDU 5884 Sort -2016 ICPC 青岛赛区网络赛

    题目链接 #include <iostream> #include <math.h> #include <stdio.h> #include<algorith ...

  5. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

  6. HDU 5881 Tea -2016 ICPC 青岛赛区网络赛

    题目链接 题意:有一壶水, 体积在 L和 R之间, 有两个杯子, 你要把水倒到两个杯子里面, 使得杯子水体积几乎相同(体积的差值小于等于1), 并且使得壶里剩下水体积不大于1. 你无法测量壶里剩下水的 ...

  7. HDU 5879 Cure -2016 ICPC 青岛赛区网络赛

    题目链接 题意:给定一个数n,求1到n中的每一项的平方分之一的累加和. 题解:题目没有给数据范围,而实际上n很大很大超过long long.因为题目只要求输出五位小数,我们发现当数大到一定程度时值是固 ...

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

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

  9. HDU 5901 Count primes (模板题)

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

随机推荐

  1. ThikPHP3.1 常用方法(one)

    公司常用但没学过的一些函数,记录一下备份. 1,在Rest操作方法中,可以使用$this->_type获取当前访问的资源类型,用$this->_method获取当前的请求类型. 2.uns ...

  2. html5开发制作,漂亮html5模板欣赏,H5网站建设

    html5是什么? HTML5 是下一代的 HTML(超文本标记语言,网页的组成部分),HTML5是web开发世界的一次重大的改变,能适配pc.手机等各终端,跨平台性能极强,移动互联网是未来的趋势,h ...

  3. php删除字符串中的所有空格

    function trimall($str)//删除空格 { $qian=array(" "," ","\t","\n" ...

  4. PHPCMS系统使用的弹出窗口插件artDialog

    来源: http://aui.github.io/artDialog/doc/index.html  (官方) http://lab.seaning.com/ http://www.mb5u.com/ ...

  5. [Unity3d][NGUI]打包NGUI预制件成Assetbundle 两种思路.

    http://www.58player.com/blog-2537-85030.html 接上文,项目中因为需要UI热更新,所以我使用了AssetBundle这个解决方案.               ...

  6. NumPy的详细教程

    原文  http://blog.csdn.net/lsjseu/article/details/20359201 主题 NumPy 先决条件 在阅读这个教程之前,你多少需要知道点python.如果你想 ...

  7. 最简单的Android教程之自定义控件

    新建title.xml,完成布局 新建一个TitleLayout继承 LinearLayout. activity_main.xml中引用 Run your applicaiton , and try ...

  8. 一个简单的Python网络爬虫(抓图),针对某论坛.

    #coding:utf-8 import urllib2 import re import threading #图片下载 def loadImg(addr,x,y,artName): data = ...

  9. python 模块之间的变量共享

    才疏学浅,只知道两种方式: 1. 通过__builtin__实现: builtin1.py import __builtin__ __builtin__.some_global_var_among_m ...

  10. git clone

    raw text for ssh: git@github.com:TommyU/avbot_config.git git command on linux: git clone git://githu ...