洲阁筛真是个不错的暴力啊。。

简单的写了个求1~n质数个数

-O2  N=1e11要2.5s

 1 #pragma GCC optimize(2)
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define LL long long
 5 int L0[1000005],L1[1000005],sn,m,P[1000005],x,ga[1000005],gb[1000005];
 6 LL n,a[1000005],b[1000005];
 7 int main(){
 8     n=100000000000;
 9 //    scanf("%lld",&n);
10     sn=sqrt(n);
11     for (int i=2;i<=sn;++i){
12         if (!P[i]) P[++m]=i;
13         for (int j=1;j<=m;++j){
14             x=P[j]*i;
15             if (x>sn) break;
16             P[x]=1;
17             if (i%P[j]==0) break;
18         }
19     }
20     for (int i=1;i<=sn;++i) a[i]=i,b[i]=n/i;
21     for (int i=1,j=1;i<=sn;++i){
22         while (j<=m&&P[j]*P[j]<=i) ++j;
23         L0[i]=j;
24     }
25     for (int i=sn,j=L0[sn];i;--i){
26         while (j<=m&&P[j]*P[j]<=b[i]) ++j;
27         L1[i]=j;
28     }
29     for (int i=1;i<=m;++i){
30         for (int j=1;j<=sn&&i<L1[j];++j){
31             LL y=j*P[i]; gb[j]=i;
32             if (y<=sn) b[j]-=b[y]+gb[y]+1-i;
33             else b[j]-=a[n/y]+ga[n/y]+1-i;
34         }
35         for (int j=sn;j&&i<L0[j];--j){
36             LL y=j/P[i]; ga[j]=i;
37             a[j]-=a[y]+ga[y]+1-i;
38         }
39     }
40     printf("%lld\n",b[1]+m-1);
41     return 0;
42 }
go ahead

相关文章:

  • 2022-12-23
  • 2021-06-22
  • 2022-12-23
  • 2021-10-10
  • 2021-12-17
  • 2021-12-24
  • 2022-01-21
  • 2022-12-23
猜你喜欢
  • 2018-06-16
  • 2022-12-23
  • 2021-09-20
  • 2018-06-14
  • 2021-11-11
  • 2021-06-06
  • 2022-12-23
相关资源
相似解决方案