爆零求助
查看原帖
爆零求助
1097314
封禁用户楼主2024/5/14 19:55

埃氏筛 + 前缀和,不知道哪里错了,请教大佬:

#include<bits/stdc++.h>
using namespace std;
int n,m,l,r,ans[1000001];
bool prime[1000001];
void precompute(){
	for(int i=2;i<=m;i++) if(!prime[i]) for(int j=i*2;j<=m;j++) prime[j]=true;
	ans[1]=0;
	for(int i=2;i<=m;i++){
		if(!prime[i]) ans[i]=ans[i-1]+1;
		else ans[i]=ans[i-1];
	}
}
int main(){
	cin>>n>>m;
	precompute();
	while(n--){
		cin>>l>>r;
		if(l<1||r>m) cout<<"Crossing the line\n";
		else cout<<ans[r]-ans[l-1]<<endl;
	}
}
2024/5/14 19:55
加载中...