Help Hanzo LightOJ - 1197 区间素数筛

问题:

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, ...

题意:给两个整数a和b,求a到b之间的素数的个数。

思路:这个是区间筛法模板题;区间筛法:b以内的合数的最小质因数一定不超过根号b,打印

根号b以内的素数表,也就是说,先分别做好[2,根号b)的表和[a,b)的表,然后从[a,根号b)的表

中筛得素数得同时,也将其倍数从[a,b)得表中划去,最后剩下的就是区间[a,b)内的素数。

代码:
 

#include
#include
#include
using namespace std;
int book[200005];
long long tag[200005],vis[200005];
void su()
{
    int t=0;
    book[1]=1;
    for(int i=2; i<=200002; i++)
        if(book[i]==0)
        {
            tag[t++]=i;
            for(int j=i*2; j<=200005; j=j+i)
                book[j]=1;
        }
}
int main()
{
    int t;
    su();
    int kk=1;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        int sum = 0;
        if(m <200005)
        {
            for(int i=n; i<=m; i++)
            {
                if(!book[i])
                    sum++;
            }
        }
        else
        {
            memset(vis,0,sizeof(vis));
            for(int i=0; tag[i]*tag[i]<=m; i++)
            {
                long long k=n/tag[i];
                if(k*tag[i]

 

你可能感兴趣的:(数论)