Meissel Lehmer Algorithm 求前n个数中素数个数 【模板】
2024-09-05 03:45:54
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
3
10
Sample Output
1
2
4
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;
}
最新文章
- 《HeadFirst SQL》笔记
- 写给自己的 程序集&;msil 扫盲
- SQL中CHARINDEX()/INSTR()函数和SUBSTRING()/SUBSTR()函数
- 040医疗项目-模块四:采购单模块—采购单创建好之后跳转到采购单修改页面(editcgd.action)
- onscroll事件的浏览器支持
- SQL Server2008 MERGE指令用法
- Apache Benchmark测试工具
- 如何设置textarea光标默认为第一行第一个字符
- underscorejs-max学习
- C++0x新特性
- 【百度地图API】如何使用suggestion--下拉列表方式的搜索建议
- Java动态代理简单应用
- Google mobile test
- MongoDB 原子操作
- 控制结构(6): 最近最少使用(LRU)
- 【题解】Luogu P2605 [ZJOI2010]基站选址
- Codeforces 233 D - Table
- LeetCode Anagrams My solution
- Linux运维工程师面试-部分题库
- 一种解决新版本API完全兼容老版本API的方法
热门文章
- L106 Three things we learned from day one at the World Cup
- log4j报错ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
- vc6++Release和Debug
- LuoguP4383 [八省联考2018]林克卡特树lct
- bzoj 2510: 弱题 概率期望dp+循环矩阵
- 在Global Azure上用Azure CLI创建ARM的VM和面向公网的负载均衡
- GCC提供的几个內建函数
- linux命令-vim一般模式下复制剪切粘贴
- Linux下UDP发送大量请求导致Operation not permitted的问题探讨
- Packet for query is too large