跳到正文

个人记录

P1865 A % B Problem

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

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 1 分钟

版本

  1. v1当前

P1865 A % B Problem

思路

C++
埃氏筛+前缀和

代码

C++
#include<bits/stdc++.h>
using namespace std;
int a[1000005],sum[1000005], n, m;
int main(){
    int c=0, x=0;
    cin >> n >> m;
    a[1]=1;
    for(int i = 2;i <= m;i++){
        if(a[i]==0){
            for(int j = i*2;j <= m;j+=i){
                a[j]=1;
            }
        }
    }
    for(int i = 1;i <= m;i++)sum[i] = sum[i-1] + 1 - a[i];
    while(n--){
        int l, r;
        cin >> l >> r;
        if(l < 1||r > m)cout << "Crossing the line\n";
        else cout << sum[r]-sum[l-1] << '\n';
    }
    return 0;
}
原文