lightoj 1197 奇怪筛法

本文介绍了一种高效的算法来计算指定区间内的素数数量,通过预处理小范围内的素数并利用这些信息来快速判断大范围内哪些数字是素数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                                                                                         奇怪筛法
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 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


找一个区间中素数的个数,由于数字很大,但是区间范围并不大,那么可以把a看成0,,作为起始点,只筛一个区间的素数个数。


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#define N 100090
using namespace std;
typedef long long ll;

ll pri[N];
int vis[N];
int cnt;
int f[N];

void eulerpri()
{
    cnt=0;
    for(ll i=2;i<=70000;i++)
    {
        if(vis[i]==0) pri[cnt++]=i;
        for(int j=0;j<cnt && i*pri[j]<=70000;j++)
        {
            vis[i*pri[j]]=1;
            if(i%pri[j]==0) break;
        }
    }
}


int main()
{

    //cout<<(77086800/209475)<<endl;
    ll a,b;
    int T;

    eulerpri();

   scanf("%d",&T);

   for(int ca=1;ca<=T;ca++)
   {
       scanf("%lld%lld",&a,&b);
       printf("Case %d: ",ca);

//       for(int i=0;i<=50;i++)
//       cout<<pri[i]<<" "<<endl;

       int n=b-a;
       memset(f,0,sizeof f);//由于a,b比较大,为了方便存储,把a看成0位置

       for(int i=0;pri[i]*pri[i]<=b && i<cnt;i++)
       {
           int j=0;//用来存标号

          j=(a/pri[i])*pri[i];//找a到b中第一个要被标记的数
          if(j<a) j+=pri[i];//如果小于a,那么在前进一个素数周期
          j-=a;//存的是下标

           for(;j<=n;j+=pri[i])
           {
               if(j+a==pri[i])continue;//如果第一个数是该素数本身,那么不标记
               f[j]=1;
           }
       }

       int ans=0;
       for(int i=0;i<=n;i++)
       {
           if(f[i]==0)
           {
               ans++;
           }
       }

        if(a==1)
        ans--;
        printf("%d\n",ans);

   }

    return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值