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.

题目大意:给出一个区间,求这个区间的素数个数。

思路:因为范围值大,所以需要用区间筛法。

b以内的合数最小质因数一定不超过sqrt(b);如果有sqrt(b)以内的素数表的话,就可以把埃氏筛法运用在[a,b)上了。也就是说,先分别做好[a,sqrt(b))的表和[a,b)的表,然后从[a,sqrt(b))的表中删除的同时,也将倍数从[a,b)的表中划去,最后剩下的就是区间[a,b)内的素数了。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define ll long long
ll sum;
bool su[1000010];
bool prime[1000010];
void solve(ll a,ll b)
{
    for(ll i=0; i*i<b; i++)//sqrt(b)
        su[i]=false;
    for(ll i=0; i<b-a; i++)//[a,b)
        prime[i]=false;
    for(ll i=2; i*i<b; i++)
    {
        if(su[i]==false)
        {
            for(ll j=i*2; j*j<b; j+=i)//sqrt(b)
                su[j]=true;
            for(ll k=max(2ll,(a+i-1)/i)*i; k<b; k+=i)//[a,b)
                prime[k-a]=true;//2ll是2的长整型
                //((a+i-1)/i)*i是满足>=a&&%i==0的离a最近的数
        }
    }
    for(int i=0; i<b-a; i++)
    {
        if(prime[i]==false)
            sum++;
    }
}
int main()
{
    int t;
    int o=1;
    scanf("%d",&t);
    while(t--)
    {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        sum=0;
        solve(a,b+1);
        if(a==1)
            sum--;
        printf("Case %d: %lld\n",o++,sum);
    }
    return 0;
}

 

08-12 01:13