题解 | 质数统计

质数统计

https://www.nowcoder.com/practice/d832b0c1a0bd4394a3229f06c6f0b50b

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+10;

int n,l,r,cnt;

vector<int> v;
int a[N];
bool f[N];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	for(int i=2;i<N;i++){
		if(!f[i]){
			cnt++;
			v.push_back(i);
			for(int j=i*2;j<N;j+=i){
				f[j]=true;
			}
		}
		a[i]=cnt;
	}
	
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>l>>r;
		cout<<a[r]-a[l-1]<<endl;
	}

    return 0;
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务