hdu 5901 Count primes 素数计数模板

转自:http://blog.csdn.net/chaiwenjun000/article/details/52589457

 

计从1到n的素数个数

两个模板

 

时间复杂度O(n^(3/4))

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 ll f[340000],g[340000],n;
 5 void init(){
 6     ll i,j,m;
 7     for(m=1;m*m<=n;++m)f[m]=n/m-1;
 8     for(i=1;i<=m;++i)g[i]=i-1;
 9     for(i=2;i<=m;++i){
10         if(g[i]==g[i-1])continue;
11         for(j=1;j<=min(m-1,n/i/i);++j){
12             if(i*j<m)f[j]-=f[i*j]-g[i-1];
13             else f[j]-=g[n/i/j]-g[i-1];
14         }
15         for(j=m;j>=i*i;--j)g[j]-=g[j/i]-g[i-1];
16     }
17 }
18 int main(){
19     while(scanf("%I64d",&n)!=EOF){
20         init();
21         cout<<f[1]<<endl;
22     }
23     return 0;
24 }

 

 

第二个 时间复杂度O(n^(2/3))

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

 

posted @ 2016-11-08 18:54  良将ℓ  阅读(268)  评论(0编辑  收藏  举报