E - Help Hanzo(LightOJ 1197)

  1. 云栖社区>
  2. 博客>
  3. 正文

E - Help Hanzo(LightOJ 1197)

angel_imp 2016-03-17 21:02:00 浏览1675

传送门
Pssword: nefu


Description


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

题目大意:
就是给定一个区间 让你求这个区间有多少个素数,数据范围(1 ≤ a ≤ b < 2^31, b - a ≤ 100000).

解题思路:
首先看到数据范围还是比较大的, 但是他们之间的区间差很小,所以我们就考虑用区间差值计算,因为我们不可能开一个 2^31次方的数组,根本开不了那么大,所以我们采用的方法就是筛法,首先进行第一次素数筛,将 1—1e6之间的素数筛出来,筛完之后我们要做的是在[0,b-a]区间进行筛,怎么筛呢,我就详细的说一下啦:
首先进行 for 循环,从0—b-a,而且得保证p[i]*p[i]<=b(p[i]是存的素数值)让 a 对每一个p[i]值进行取余,我们要筛的就是(a+(p[i]-a%p[i]))%p[i] == 0的数,然后就跟素数筛差不多啦…具体还得看我的代码 代码还是挺好理解的 ^_^
上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = 1e6+5;
const int mod = 1000000007;
const double eps = 1e-7;
bool prime[MAXN];
LL p[MAXN];
LL k;
void isprime()///素数筛
{
    k = 0;
    prime[1] = false;
    memset(prime, true, sizeof(prime));
    for(LL i=2; i<MAXN; i++)
    {
        if(prime[i])
        {
            p[k++] = i;
            for(LL j=i*i; j<MAXN; j+=i)
                prime[j] = false;
        }
    }
}
LL a, b, tmp;
bool ok[MAXN];
void ShaiXuan()
{
    memset(ok, true, sizeof(ok));
    tmp = b - a;
    for(LL i=0; p[i]*p[i]<=b&&i<k; i++)
    {
        LL tt = 0;
        if(a%p[i])///第一个筛掉的数是(tt+a)%p[i] == 0
            tt = p[i] - a%p[i];
        if(a <= p[i])///防止是素数
            tt += p[i];
        for(; tt<=tmp; tt+=p[i])///筛掉p[i]的倍数
            ok[tt] = 0;

    }
}
int main()
{
    isprime();
    int T;
    cin>>T;
    for(int cas=1; cas<=T; cas++)
    {
        cin>>a>>b;
        ShaiXuan();
        LL ret = 0;
        if(a == 1)
            ret = -1;
        for(int i=0; i<=tmp; i++)
            if(ok[i])
                ret++;
        printf("Case %d: %lld\n",cas,ret);
    }
    return 0;
}