light oj 1197 Help Hanzo

来源:互联网 发布:国家规划教材 知乎 编辑:程序博客网 时间:2024/06/05 18:39
 

Help Hanzo

Time Limit:2000MS    Memory Limit:32768KB    64bit IO Format:%lld & %llu

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 asa, 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 givena andb 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 integersa andb (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

Help Hanzo


题意:求出a到b之间素数的个数,a,b的数据范围小于2^31,b-a<=1e5
普通打表肯定TlE+MLE,2^31用计算器算一下大概为2.2e10,暴力打表肯定炸了,竟然b-a<=1e5,可以从这里出发考虑其他方法,可以用一个数组I-a表示当前数的状态,这样就可以实现了,但是要注意的是2.2e10的因数应该小于等于sqrt(2.2e10),可以打素数表到1e6,如果打到1e5就不对,= =

 
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<queue>#include<deque>#include<stack>#include<map>#include<set>using namespace std;typedef long long ll;#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define sf scanf#define pf printf#define mem(i,a) memset(i,a,sizeof i)const int Max=1e6+50;int p[Max+1],cou,m;ll a,b;bool vis[Max+1],vpn[Max];//vis储存是否为素数,当数据较大时就要用到vpn这个数组了void prime()//素数打表,我用的是线性筛法O(n),用埃氏筛法O(nlong n)也行,只是打表范围不能太小,也不能太大,不然TLE+MLE{    mem(vis,0);    vis[1]=1;    cou=0;    for(ll i=2;i<=Max;i++)    {        if(!vis[i])p[cou++]=i;        for(ll j=0;j<cou&&i*p[j]<=Max;j++)        {            vis[i*p[j]]=1;            if(!(i%p[j]))break;        }    }}int main(){    prime();    sf("%d",&m);    for1(i,m)    {        int cnt=0;        sf("%I64d%I64d",&a,&b);        if(b<=Max)//要求的范围小于打的素数表范围,直接枚举        {            for(ll i=a;i<=b;i++)                if(!vis[i])                    cnt++;        }        else//大于打表范围        {            mem(vpn,0);            for(ll i=0;i<cou&&p[i]<=b;i++)            {                ll k=a/p[i];                if(k*p[i]<a)k++;                for(ll j=k*p[i];j<=b;j+=p[i])//枚举所有含因数的数,用vpn标记为1                    vpn[j-a]=1;            }            for(ll i=a;i<=b;i++)//枚举未被标记的数的个数即是答案                if(!vpn[i-a])                    cnt++;        }        pf("Case %d: %d\n",i,cnt);    }    return 0;}



原创粉丝点击