素数统计
思路:
平移区间筛质数(1e9),
对于每个质数逐个在区间中筛数
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstring>
#define LL long long
using namespace std;
int tot = 0, primsize = 0;
LL prim[2000010];
bool isprim[2000010], no[2000010];
void init(int lim){
memset(isprim,1,sizeof(isprim));
isprim[1] = false;
for(int i=2; i<=lim; i++){
if(isprim[i]){
prim[++primsize] = i;
}
for(int j=1; j<=primsize && i * prim[j]<=lim; j++){
isprim[i * prim[j]] = false;
}
}
}
int main(){
LL lim = 1e6+10;
init(lim);
LL T, L, R;
LL ans;
memset(no, 0, sizeof(no));
tot = 0;
scanf("%lld%lld", &L, &R);
for(int i=1; i<=primsize && prim[i] * prim[i] <= R ; i++){
LL rt = max(L / prim[i] * prim[i], prim[i] * prim[i]);
for(LL j=rt; j<=R; j+=prim[i]){
if(j >= L) no[j-L] = 1;
}
}
for(int i=0; i<=R-L; i++){
if( !no[i] ){
tot++;
}
}
int cc = 0;
if(L == 1) cc = 1;
printf("%d\n", tot - cc);
return 0;
}

本文介绍了如何使用平移区间筛质数的方法进行素数统计,这种方法适用于处理高达1亿的数值范围。通过针对每个质数筛选指定区间内的数,实现高效地找出该区间的素数。
&spm=1001.2101.3001.5002&articleId=78165182&d=1&t=3&u=718df00cf28f4e0eb56b61328a22540a)
1250

被折叠的 条评论
为什么被折叠?



