Count primes

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2719    Accepted Submission(s): 1386

Problem Description
Easy question! Calculate how many primes between [1...n]!
 
Input
Each line contain one integer n(1 <= n <= 1e11).Process to end of file.
 
Output
For each case, output the number of primes in interval [1...n]
 
Sample Input
2
3
10
 
Sample Output
1
2
4
 
Source
 
#include <bits/stdtr1c++.h>

#define MAXN 100
#define MAXM 10001
#define MAXP 40000
#define MAX 400000
#define clr(ar) memset(ar, 0, sizeof(ar))
#define read() freopen("lol.txt", "r", stdin)
#define dbg(x) cout << #x << " = " << x << endl
#define chkbit(ar, i) (((ar[(i) >> 6]) & (1 << (((i) >> 1) & 31))))
#define setbit(ar, i) (((ar[(i) >> 6]) |= (1 << (((i) >> 1) & 31))))
#define isprime(x) (( (x) && ((x)&1) && (!chkbit(ar, (x)))) || ((x) == 2)) using namespace std; namespace pcf{
long long dp[MAXN][MAXM];
unsigned int ar[(MAX >> 6) + 5] = {0};
int len = 0, primes[MAXP], counter[MAX]; void Sieve(){
setbit(ar, 0), setbit(ar, 1);
for (int i = 3; (i * i) < MAX; i++, i++){
if (!chkbit(ar, i)){
int k = i << 1;
for (int j = (i * i); j < MAX; j += k) setbit(ar, j);
}
} for (int i = 1; i < MAX; i++){
counter[i] = counter[i - 1];
if (isprime(i)) primes[len++] = i, counter[i]++;
}
} void init(){
Sieve();
for (int n = 0; n < MAXN; n++){
for (int m = 0; m < MAXM; m++){
if (!n) dp[n][m] = m;
else dp[n][m] = dp[n - 1][m] - dp[n - 1][m / primes[n - 1]];
}
}
} long long phi(long long m, int n){
if (n == 0) return m;
if (primes[n - 1] >= m) return 1;
if (m < MAXM && n < MAXN) return dp[n][m];
return phi(m, n - 1) - phi(m / primes[n - 1], n - 1);
} long long Lehmer(long long m){
if (m < MAX) return counter[m]; long long w, res = 0;
int i, a, s, c, x, y;
s = sqrt(0.9 + m), y = c = cbrt(0.9 + m);
a = counter[y], res = phi(m, a) + a - 1;
for (i = a; primes[i] <= s; i++) res = res - Lehmer(m / primes[i]) + Lehmer(primes[i]) - 1;
return res;
}
} long long solve(long long n){
int i, j, k, l;
long long x, y, res = 0; for (i = 0; i < pcf::len; i++){
x = pcf::primes[i], y = n / x;
if ((x * x) > n) break;
res += (pcf::Lehmer(y) - pcf::Lehmer(x));
} for (i = 0; i < pcf::len; i++){
x = pcf::primes[i];
if ((x * x * x) > n) break;
res++;
} return res;
} int main(){
pcf::init();
long long n, res; while (scanf("%lld", &n) != EOF){
//res = solve(n);
printf("%lld\n",pcf::Lehmer(n));
//printf("%lld\n", res);
}
return 0;
}

Meissel Lehmer Algorithm 求前n个数中素数个数 【模板】的更多相关文章

  1. Help Hanzo lightof 1197 求一段区间内素数个数,[l,r] 在 [1,1e9] 范围内。r-l<=1e5; 采用和平常筛素数的方法。平移区间即可。

    /** 题目:Help Hanzo lightof 1197 链接:https://vjudge.net/contest/154246#problem/M 题意:求一段区间内素数个数,[l,r] 在 ...

  2. 求0到n之间素数个数的序列

    要求: (1) 找出0-1000之间素数(2) 设f(n)表示0-n之间的素数个数,计算出当n=0,1,2,3,.....,997时f(n)的值,并写入文件 分析: 首先找素数使用一个效率较高的方法- ...

  3. 求第 i 个素数 Meissel Lehmer Algorithm + 二分 【模板】

    1473: L先生与质数V3 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1348  Solved: 147 [Submit][Status][Web ...

  4. 洛谷 P3912 素数个数

    P3912 素数个数 题目描述 求1,2,\cdots,N1,2,⋯,N 中素数的个数. 输入输出格式 输入格式: 1 个整数NN. 输出格式: 1 个整数,表示素数的个数. 输入输出样例 输入样例# ...

  5. loj #6235. 区间素数个数 min_12.5筛

    \(\color{#0066ff}{ 题目描述 }\) 求 \(1\sim n\) 之间素数个数. \(\color{#0066ff}{输入格式}\) 一行一个数 n . \(\color{#0066 ...

  6. hdu 5265 技巧题 O(nlogn)求n个数中两数相加取模的最大值

    pog loves szh II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

    275. To xor or not to xor   The sequence of non-negative integers A1, A2, ..., AN is given. You are ...

  8. 求n个数中的最大或最小k个数

    //求n个数中的最小k个数        public static void TestMin(int k, int n)        {            Random rd = new Ra ...

  9. 设计一个算法,求非空二叉树中指定的第k层(k&gt;1)的叶子节点的个数

    思想:採用基于层序遍历的方法. 用level扫描各层节点,若某一层的节点出队后.rear指向该层中最右节点.则将rear赋值给last(对于第一层.last=1).在出队时,若front=last,表 ...

随机推荐

  1. HDU - 5934

    tarjan 视频讲解 /** * 题目链接:https://vjudge.net/problem/HDU-5934 * 题意:给你n个炸弹,引爆每个炸弹会有一定的花费.每个炸弹给出坐标x,y,半径r ...

  2. nodejs buffer 总结

    JavaScript 语言自身只有字符串数据类型,没有二进制数据类型.Buffer 类,该类用来创建一个专门存放二进制数据的缓存区. 一个 Buffer 类似于一个整数数组,但它对应于 V8 堆内存之 ...

  3. ROS 负载均衡

    [xuan89@MikroTik] > :for i from=1 to=$z do= {/ip firewall mangle add action=mark-connection chain ...

  4. SQL语言方方面面

    1 数据库和SQL 1.1 数据库 DB, DBMS DBMS的种类: 层次性数据库, 关系型数据库, 非关系型数据库 RDBMS, 关系数据库管理系统 1.2 数据库的结构 RDBMS常见的系统结构 ...

  5. Java中日期和时间的相关问题

    1.java.lang.System类 System类提供的public static long currentTimeMillis()用来返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位 ...

  6. ACM学习历程—HDU 5025 Saving Tang Monk(广州赛区网赛)(bfs)

    Problem Description <Journey to the West>(also <Monkey>) is one of the Four Great Classi ...

  7. Redo Log File(inactive、active)损坏,处理恢复对策

    redolog的生命周期中共有四种状态:current -> 正在使用的active -> 非正在使用的,对应的Dirty Block还没有完全写入到数据文件中inactive -> ...

  8. openssh for windows

  9. overflow: auto;溢出自动显示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. [bzoj1568]李超线段树模板题(标志永久化)

    题意:要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第i条被插入的线段的标号为i. 2.给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号. 解题关键:注意标 ...