ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

lightoj1197大区间筛素数

2019-05-04 10:56:43  阅读:212  来源: 互联网

标签:int safe 素数 territories lightoj1197 Hanzo 区间 include he


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

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 1000005
#define ll long long
using namespace std;
int p[maxn];
bool vis[maxn];
bool prime[maxn];
int cnt;
int t;
void init()
{
    cnt=0;

    memset(vis,0,sizeof(vis));
    
    for(int i=2;i<maxn;i++)
    {
        if(!vis[i])
            p[cnt++]=i;
        for(int j=0;j<cnt&&i*p[j]<maxn;j++)
        {
            vis[i*p[j]]=1;
            if(i%p[j]==0)
                break;
        }
    }
}
int main()
{
    init();
    scanf("%d",&t);
    int w=0;
    while(t--)
    {w++;
     for(int i=0;i<maxn;i++)
        prime[i]=1;
        ll a,b;
        scanf("%lld%lld",&a,&b);
        ll ans=b-a+1;
        if(a==1)
            ans--;
        for(int i=0;i<cnt&&1LL*p[i]*p[i]<=b;i++)
        {
            for(ll j=a/p[i]*p[i];j<=b;j+=p[i])
                if(j>=a&&j>p[i])
                prime[j-a]=0;
        }
        for(int i=0;i<=b-a;i++)
            if(!prime[i])
                ans--;
           printf("Case %d: %lld\n",w,ans);
    }
    return 0;

}

 

标签:int,safe,素数,territories,lightoj1197,Hanzo,区间,include,he
来源: https://blog.csdn.net/sdauguanweihong/article/details/89810812

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有