A % B Problem

P1865 A % B Problem

  • 难度普及/提高-

 

题目背景

题目名称是吸引你点进来的 实际上该题还是很水的

题目描述

区间质数个数

输入输出格式

输入格式:

一行两个整数 询问次数n,范围m

接下来n行,每行两个整数 l,r 表示区间

输出格式:

对于每次询问输出个数 t,如l或r∉[1,m]输出 Crossing the line

输入输出样例

输入样例#1:
2 5
1 3
2 6
输出样例#1:
2
Crossing the line

说明

【数据范围和约定】

对于20%的数据 1<=n<=10 1<=m<=10

对于100%的数据 1<=n<=1000 1<=m<=1000000 -10^9<=l<=r<=10^9 1<=t<=1000000

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long n,m,l,r,f[1000001],s[1000001],sum;
int main()
{
    cin>>n>>m;
    memset(f,1,sizeof(f));
    f[1]=0;
    for(int i=2; i<=m; i++)//素数筛
    {
        if(f[i])
        {
            for(int j=i+i; j<=m; j+=i)
                f[j]=0;
        }    
    }
    for(int i=1; i<=m; i++)//1到i之间的素数个数
    {
        if(f[i])
            sum++;
        s[i]=sum;
    }
    for(int i=1; i<=n; i++)//输出处理
    {
        cin>>l>>r;
        if(r>m||r<1||l<1||l>m)
            cout<<"Crossing the line"<<endl;
        else
        {
            if(f[l])
                cout<<s[r]-s[l]+1<<endl;
            else
                cout<<s[r]-s[l]<<endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dxy1174868024/p/5567241.html