[素数个数模板] HDU 5901 Count primes

Posted Kurokey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[素数个数模板] HDU 5901 Count primes相关的知识,希望对你有一定的参考价值。

题目链接:传送门

题目大意:给你一个 n(1 <= n <= 1e11),问1~n中素数个数

题目思路:(Meisell-Lehmer算法)

 1 #include<cstdio>  
 2 #include<cmath>  
 3 using namespace std;  
 4 #define LL long long  
 5 const int N = 5e6 + 2;  
 6 bool np[N];  
 7 int prime[N], pi[N];  
 8 int getprime()  
 9 {  
10     int cnt = 0;  
11     np[0] = np[1] = true;  
12     pi[0] = pi[1] = 0;  
13     for(int i = 2; i < N; ++i)  
14     {  
15         if(!np[i]) prime[++cnt] = i;  
16         pi[i] = cnt;  
17         for(int j = 1; j <= cnt && i * prime[j] < N; ++j)  
18         {  
19             np[i * prime[j]] = true;  
20             if(i % prime[j] == 0)   break;  
21         }  
22     }  
23     return cnt;  
24 }  
25 const int M = 7;  
26 const int PM = 2 * 3 * 5 * 7 * 11 * 13 * 17;  
27 int phi[PM + 1][M + 1], sz[M + 1];  
28 void init()  
29 {  
30     getprime();  
31     sz[0] = 1;  
32     for(int i = 0; i <= PM; ++i)  phi[i][0] = i;  
33     for(int i = 1; i <= M; ++i)  
34     {  
35         sz[i] = prime[i] * sz[i - 1];  
36         for(int j = 1; j <= PM; ++j) phi[j][i] = phi[j][i - 1] - phi[j / prime[i]][i - 1];  
37     }  
38 }  
39 int sqrt2(LL x)  
40 {  
41     LL r = (LL)sqrt(x - 0.1);  
42     while(r * r <= x)   ++r;  
43     return int(r - 1);  
44 }  
45 int sqrt3(LL x)  
46 {  
47     LL r = (LL)cbrt(x - 0.1);  
48     while(r * r * r <= x)   ++r;  
49     return int(r - 1);  
50 }  
51 LL getphi(LL x, int s)  
52 {  
53     if(s == 0)  return x;  
54     if(s <= M)  return phi[x % sz[s]][s] + (x / sz[s]) * phi[sz[s]][s];  
55     if(x <= prime[s]*prime[s])   return pi[x] - s + 1;  
56     if(x <= prime[s]*prime[s]*prime[s] && x < N)  
57     {  
58         int s2x = pi[sqrt2(x)];  
59         LL ans = pi[x] - (s2x + s - 2) * (s2x - s + 1) / 2;  
60         for(int i = s + 1; i <= s2x; ++i) ans += pi[x / prime[i]];  
61         return ans;  
62     }  
63     return getphi(x, s - 1) - getphi(x / prime[s], s - 1);  
64 }  
65 LL getpi(LL x)  
66 {  
67     if(x < N)   return pi[x];  
68     LL ans = getphi(x, pi[sqrt3(x)]) + pi[sqrt3(x)] - 1;  
69     for(int i = pi[sqrt3(x)] + 1, ed = pi[sqrt2(x)]; i <= ed; ++i) ans -= getpi(x / prime[i]) - i + 1;  
70     return ans;  
71 }  
72 LL lehmer_pi(LL x)  
73 {  
74     if(x < N)   return pi[x];  
75     int a = (int)lehmer_pi(sqrt2(sqrt2(x)));  
76     int b = (int)lehmer_pi(sqrt2(x));  
77     int c = (int)lehmer_pi(sqrt3(x));  
78     LL sum = getphi(x, a) +(LL)(b + a - 2) * (b - a + 1) / 2;  
79     for (int i = a + 1; i <= b; i++)  
80     {  
81         LL w = x / prime[i];  
82         sum -= lehmer_pi(w);  
83         if (i > c) continue;  
84         LL lim = lehmer_pi(sqrt2(w));  
85         for (int j = i; j <= lim; j++) sum -= lehmer_pi(w / prime[j]) - (j - 1);  
86     }  
87     return sum;  
88 }  
89 int main()   
90 {  
91     init();  
92     LL n;  
93     while(~scanf("%lld",&n))  
94     {  
95         printf("%lld\n",lehmer_pi(n));  
96     }  
97     return 0;  
98 }  

 

以上是关于[素数个数模板] HDU 5901 Count primes的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5901 Count primes (模板题)

Hdu 5901 Count primes

hdu5901 Count primes(大素数模版)

hdu 5901 Count primes

HDU5901 Count primes素数

hdu 5901 Count primes (2016沈阳网络赛)