洛谷 P1865 A % B Problem

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34416123/article/details/82744951

水题水题

#include<cstdio>
#include<vector>
#include<cstring>
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;

const int MAXN = 1123456;
bool is_prime[MAXN];
vector<int> prime;
int ans[MAXN];

void get_prime()
{
    memset(is_prime, true, sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
    REP(i, 2, MAXN)
    {
        if(is_prime[i]) prime.push_back(i);
        REP(j, 0, prime.size())
        {
            if(i * prime[j] >= MAXN) break;
            is_prime[i * prime[j]] = false;
            if(i % prime[j] == 0) break;
        }
        ans[i] = ans[i-1] + is_prime[i];
    }
}

int main()
{
    get_prime();
    int n, m, l, r;
    scanf("%d%d", &n, &m);
    while(n--)
    {
        scanf("%d%d", &l, &r);
        if(l < 1 || r > m) puts("Crossing the line");
        else printf("%d\n", ans[r] - ans[l-1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34416123/article/details/82744951