跳到正文

个人记录

P1865 A % B Problem---段毅阳

DYY_0710约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年11月9日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟

版本

  1. v1当前

P1865 A % B Problem

注意

  • “∈”符合代表属于

思路

  • 先用埃氏筛把所有质数找出来
  • 判断l,r是否属于[1,m]是就找出[l,r]中的质数个数并输出,否则输出Crossing the line

代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m,l,r;
bool a[N];
long long cnt;
int main(){
	cin >> n >> m;
	a[1]=1;
	for(int i=2;i<=1000000;i++){
		if(a[i]==0){
			for(int j=2*i;j<=1000000;j+=i){
				a[j]=1;
			}
		}
	}
    for(int i=1;i<=n;i++){
        cin >> l >> r;
        cnt=0;
        if(!(1<=l&&r<=m)){
            cout << "Crossing the line" << endl;
            continue;
        }
        for(int i=l;i<=r;i++){
            if(a[i]==0) cnt++;
        }
        cout << cnt << endl;
    }
	return 0;
}
原文