wenbao与网络赛(百亿内素数)
@ 求1到一百亿之内的素数个数
http://acm.hdu.edu.cn/showproblem.php?pid=5901
@ 复杂度大概O(n^(3/4))
1 //G++ 1560ms 6544k 2 #include <bits/stdc++.h> 3 #define ll long long 4 using namespace std; 5 ll f[340000],g[340000],n; 6 void init(){ 7 ll i,j,m; 8 for(m=1;m*m<=n;++m)f[m]=n/m-1; 9 for(i=1;i<=m;++i)g[i]=i-1; 10 for(i=2;i<=m;++i){ 11 if(g[i]==g[i-1])continue; 12 for(j=1;j<=min(m-1,n/i/i);++j){ 13 if(i*j<m)f[j]-=f[i*j]-g[i-1]; 14 else f[j]-=g[n/i/j]-g[i-1]; 15 } 16 for(j=m;j>=i*i;--j)g[j]-=g[j/i]-g[i-1]; 17 } 18 } 19 int main(){ 20 while(scanf("%I64d",&n)!=EOF){ 21 init(); 22 cout<<f[1]<<endl; 23 } 24 return 0; 25 }
@ 复杂度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 }
只有不断学习才能进步!