LightOJ 1197 - Help Hanzo (模拟筛素法区间筛素)

来源:互联网 发布:淘宝客服平均工资 编辑:程序博客网 时间:2024/05/22 00:46
1197 - Help Hanzo
  PDF (English)StatisticsForum
Time Limit: 2 second(s)Memory Limit: 32 MB

Amakusa, the evil spiritual leader has captured thebeautiful princess Nakururu. The reason behind this is he had a little problemwith Hanzo Hattori, the best ninja and the love of Nakururu. After hearing thenews Hanzo got extremely angry. But he is clever and smart, so, he kept himselfcool and made a plan to face Amakusa.

Before reaching Amakusa's castle, Hanzo has to pass someterritories. The territories are numbered asa, a+1, a+2, a+3 ... b. Butnot all the territories are safe for Hanzo because there can be other fighterswaiting for him. Actually he is not afraid of them, but as he is facingAmakusa, he has to save his stamina as much as possible.

He calculated that the territories which are primes are safefor him. Now givena and b he needs to know how many territoriesare safe for him. But he is busy with other plans, so he hired you to solvethis small problem!

Input

Input starts with an integer T (≤ 200),denoting the number of test cases.

Each case contains a line containing two integers aand b (1 ≤ a ≤ b < 231, b - a ≤ 100000).

Output

For each case, print the case number and the number of safeterritories.

Sample Input

Output for Sample Input

3

2 36

3 73

3 11

Case 1: 11

Case 2: 20

Case 3: 4

Note

A number is said to be prime if it is divisible by exactlytwo different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...


思路:模拟一下素数筛的思想,因为区间最大为10w,所以说标记数组可以,过程减去边界就行了

ac代码:

#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 101000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-8using namespace std;ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}//headint prime[MAXN];int v[MAXN],cnt;void makeprime(){    mem(v);v[1]=1;cnt=0;    for(int i=2;i<=100000;i++)    {        if(!v[i])        {            prime[cnt++]=i;            for(int j=i*2;j<=100000;j+=i)                v[j]=1;        }    }}int main(){    makeprime();//打出素数表    ll l,r,t,i,j;    int cas=0;    scanf("%I64d",&t);    while(t--)    {        scanf("%I64d%I64d",&l,&r);        mem(v);        for(i=0;i<cnt;i++)        {            ll k=(l/prime[i])*prime[i];            if(k<l) k+=prime[i];            if(k<(ll)prime[i]*prime[i]) k=(ll)prime[i]*prime[i];//可能会溢出RE            for(j=k;j<=r;j+=prime[i])                //if(j-l>=0)                v[j-l]=1;        }        int ans=0;        if(l==1)//特殊情况            v[0]=1;        for(i=0;i<=r-l;i++)            if(v[i])                ans++;        printf("Case %d: %d\n",++cas,r-l-ans+1);    }return 0;}


0 0