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<bits/stdc++.h>

using namespace std;

const int MAXN = 1e6 + 10;
int a[MAXN], book[MAXN] , b[MAXN];

int su(int n)
{
    if(n == 1)
        return 0;
    if(n == 2)
        return 1;
    for(int i = 2 ; i <= sqrt(n); i ++)
        if(n % i == 0)
            return 0;
    return 1;
}

int main()
{
    int n , m ;
    cin >> n >> m;
    for(int i = 1; i <= 1e6 ; i ++ )
    {
        if(book[i] == -1)
            continue;
        if(su(i))
        {
            book[i] = 1;
            for(int j = i + i ; j <= 1e6 ; j += i)
                book[j] = -1;
        }
    }
    //for(int i = 1; i <= 10 ; i ++ )
    //	cout << book[i] << " ";
    //cout << endl;
    int num = 0;
    for(int i = 1; i <= 1e6 ; i ++)
    {
        if(book[i] == 1)
            num ++;
        b[i] = num;
    }
    //for(int i = 1; i <= 10 ; i ++ )
    //	cout << b[i] << " ";
    //cout << endl;
    for(int i = 1; i <= n ; i ++)
    {
        int x, y;
        cin >> x >> y;
        if( x > m || y > m || x <= 0 || y <= 0)
            cout << "Crossing the line" << endl;
        else
        {
            cout << b[y] - b[x - 1] << endl;
        }
    }
        
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Ant_e_zz/article/details/80772270