Help Hanzo 题解

博客围绕求闭区间 [a,b] 内素数个数展开。因直接用欧拉大表标记不完、暴力求解会超时,需预处理。先预处理出 1e6 范围内的素数,再标记相应素数的倍数,最后筛选出未标记的数,即得到区间内素数个数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Amakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.

Before reaching Amakusa's castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 ... b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.

He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a line containing two integers a and b (1 ≤ a ≤ b < 231, b - a ≤ 100000).

Output

For each case, print the case number and the number of safe territories.

Sample Input

3

2 36

3 73

3 11

Sample Output

Case 1: 11

Case 2: 20

Case 3: 4

 

 

题意给闭区间 [a,b],求这个区间内的素数个数。想了很久,如果直接欧拉大表肯定不能标记完,直接暴力肯定回T,所以我门需要预处理。2^31 大概是1e10的样子。

我们可以按照欧拉筛找素数的方法。先预处出 1e6范围内的素数。后面我直接标记相应素数的倍数。最后筛选出没有标记的即可。

 

思路:根据筛选出的素数标记相应素数对应的合数

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const long long inf=1e10;
const int maxn = 1e6;
long long prime[maxn+10];
long long vis[maxn+10];
long long vis1[maxn+10];///标记合数
long long  cnt;
/*欧拉筛*/
void shu()
{
    cnt =0;
    for(long long  i=2; i<=maxn; i++)
    {
        if(!vis[i])
            prime[cnt++]=i;
        for(long long  j=0; j<cnt&&prime[j]*i<=maxn; j++)
        {
            vis[prime[j]*i]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}
/*方式发类似素数筛*标记每个素数的倍数*/
void prime2(long long l,long long r)
{

    long long i,j;
    memset(vis1,0,sizeof(vis1));
   // cout<<l<<" "<<r<<endl;
    for(i=0; i<cnt&&prime[i]<=r; i++)
    {
        long long d=l/  prime[i];///找到这个区间的当前素数的第一个位置位置
        while(d*prime[i]<l||d<=1)
            d++;
        for(j=d*prime[i]; j<=r; j+=prime[i])///j+prime[i] 就是找到相应的倍数。
        {
            if(j>=l)
                   vis1[j-l]=1;///标记这个区间的合数
        }
    }
    if(l==1)/*1 不是素数*/
        vis1[0]=1;
}
int main()
{
    shu();
    long long a,b,T,cas=1;
    cin>>T;
    while(T--)
    {
        cin>>a>>b;
        prime2(a,b);
        long long  ans=0;
        for(long long  i=0; i<=b-a; i++)
            if(vis1[i]==0)
                 ans++;
        cout<<"Case "<<cas++<<": ";
        cout<<ans<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值