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 < 2^31, 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

因为a,b很大,所以无法打出这么大的表,而且还会超时,但是a,b的差值很小,所以可以筛选出a,b间的非素数。
具体做法可以使先打出一个小的素数表(2 到 sqrt(b)),然后利用这个表来筛。

//区间筛选素数

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<algorithm>

#define ll long long
#define inf 0x3f3f3f3f
#define maxn 600000

using namespace std;

int vis[maxn];
int prime[100000];
int cnt= 0;

void find_prime()
{
    vis[0] = 1;
    vis[1] = 1;
    for(int i = 2; i < maxn; i ++)
    {
        if(vis[i] == 0)
        {
            prime[cnt++] = i;

            for(int j = i+i; j < maxn; j += i)
            {
                vis[j] = 1;
            }
        }
    }
}

int flag[100010];

int main()
{
    find_prime();

    int t;  
    int a,b;

    scanf("%d",&t);

    for(int i = 1;i <= t; i ++)
    {
        scanf("%d%d",&a,&b);
        memset(flag,0,sizeof(flag));

        int tmp = sqrt( b );
        int j = 0;
        ll tmp2;

        while( j < cnt && prime[j] <= tmp )  // 找到比 a 大的第一个非素数,然后一直筛
        {
            tmp2 = max( (ll)a / prime[j] * prime[j] , (ll)prime[j] * prime[j]);  

//          tmp2 = a / prime[j] * prime[j];
//          if( tmp2 < a )
//              tmp2 += prime[j];
//          if( tmp2 < (ll)prime[j] * prime[j] )
//              tmp2 = prime[j] * prime[j];

            if( tmp2 < a)  tmp2 += prime[j];

            for(ll k = tmp2; k <= b; k += prime[j])
            {

                flag[ k - a ] = 1;
            }
            j ++;
        }

        tmp = b - a;
        int ans = 0;
        for(int j = 0; j <= tmp; j ++)
            if( flag[j] == 0)
                ans ++;

        if( a == 1)   // 特判下
            ans --;

        printf("Case %d: %d\n",i,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值