码迷,mamicode.com
首页 > 其他好文 > 详细

lightoj 1197 奇怪筛法

时间:2015-07-12 09:49:46      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

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









版权声明:本文为博主原创文章,未经博主允许不得转载。

lightoj 1197 奇怪筛法

标签:

原文地址:http://blog.csdn.net/wust_zjx/article/details/46843797

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!