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

1197 - Help Hanzo
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 as a, 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 given a 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-8
using 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;}
//head
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值