【素数筛+区间枚举】Help Hanzo

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

Note

A number is said to be prime if it is divisible by exactly two different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...

由于范围是1e9,首先筛出sqrt(1e9)的所有素数

之后在a到b内去除每一个素数的倍数,最后即为素数的个数

由于ab区间不能直接开vis,但是b-a<=100000,所以可以采用【a-100000,b-100000】的方式来保存数值

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define ll long long
const int MAXN=100005;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
bool vis[100005];
ll deal(ll a,ll b)
{
    ll cnt=b-a+1;
    for(int i=1; i<=prime[0]&&prime[i]*prime[i]<=b; i++) 
    {
        int now=a/prime[i];
        ll test=max(now*prime[i],prime[i]+prime[i]);
        while(test<=b)
        {
            if(test-a<0)
            {
                test+=prime[i]; //一定要采取+的形式,不要采用*的形式,否则会tle
                continue;
            }
            if(vis[test-a]==0)
                cnt--;
            vis[test-a]=1;
            test+=prime[i];
        }
    }
    return cnt;
}
int main()
{
    getPrime();
    int num;
    scanf("%lld",&num);
    for(int kk=1; kk<=num; kk++)
    {
        ll a,b;
        memset(vis,0,sizeof(vis));
        scanf("%lld%lld",&a,&b);
        ll ans=deal(a,b);
        if(a==1)ans--;
        printf("Case %d: %lld\n",kk,ans);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值