Help Hanzo LightOJ - 1197【素数筛,求区间素数个数】

Problem 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 a, a+1, a+2, a+3 ... b 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 ) T (≤ 200) T(200), denoting the number of test cases.
Each case contains a line containing two integers a a a and b ( 1 ≤ a ≤ b &lt; 231 , b − a ≤ 100000 ) . b (1 ≤ a ≤ b &lt; 231, b - a ≤ 100000). b(1ab<231,ba100000).

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

Note

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

o(︶︿︶)o 唉!又一个数论~~~~(>_<)~~~~

题意

给出T组数据,每组数据含有一个M一个N,求出从M到N中所有素数的个数;

PS:
下面是我们做这题需要了解的:

区间 [ a , b ) [a,b) [a,b) 指的是所有满足 a &lt; = x &lt; b a&lt;=x&lt;b a<=x<b 的整数所构成的集合。
在素数判定中 b b b 以内的合数的最小质因数一定不超过 b \sqrt b b 如果有 b \sqrt b b 以内的素数表的,就可以把埃氏筛法运用在 [ a , b ) [a,b) [a,b) 上了,也就是说,先分别做好 [ 2 , b ) [2,\sqrt b) [2,b ) 的表和 [ a , b ) [a,b) [a,b) 的表,然后从 [ 2 , b ) [2,\sqrt b) [2,b ) 的表中筛得素数的同时,也将其倍数从 [ a , b ) [a,b) [a,b) 的表中划去,最后剩下的就是区间 [ a , b ) [a,b) [a,b) 内的素数了。因为求的数的可能很大由此我就把数的区间进行了转换,具体怎样做 ◔ ‸◔?

看代码╭(′▽`)╯

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int maxx=1e6+10;
int k;
bool Isprime1[100000];
bool Isprime2[100000];
ll prime[maxx];
int Prime(ll a,ll b)
{
    for(ll i=0; (ll)i*i<=b; i++)
        Isprime1[i]=false;
    for(ll i=0; i<=b-a; i++)
        Isprime2[i]=true;
    for(ll i=2; (ll)i*i<=b; i++)
    {
        if(!Isprime1[i])
        {
            for(ll j=2*i; (ll)j*j<=b; j+=i)//筛出[2,sqrt(b)]的素数
                Isprime1[j]=true;
            for(ll j=max((ll)2,(a+i-1)/i)*i; j<=b; j+=i)//筛出[a,b]的素数
                Isprime2[j-a]=false;//这里的2LL是2的长整型数,与2LL比较的意思就是j最小是i的两倍
                                 //(a+i-1)/i表示的是[a,b]区间内的第一个数至少为i的多少倍.
        }
    }
    int sum=0;
    for(int i=0; i<b-a+1; i++)
        if(Isprime2[i])
            sum++;
    return sum;
}
int main()
{
    int t,Case=0;
    cin>>t;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        int s=Prime(a,b);
        if(a==1)//因为上面函数初始化的时候把所有的数都定义为素数
            s--;//其他的数的标记虽然改变但是1没变
        printf("Case %d: %d\n",++Case,s);
    }
    return 0;
}

下面的是用 j a v a java java写的建议➹用 c c c 写,此运行时间长

import java.util.*;
public class Main {
	private static Scanner sc;
	static int k;
	private static final int maxx=10000005;
	static boolean Isprime1[]=new boolean[maxx+5];
		 static boolean Isprime2[]=new boolean[maxx+5];
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		 k=0;
      int t=sc.nextInt();
      int Case=0;
      while(t-->0)
      {
    	  int a=sc.nextInt(),b=sc.nextInt();
    	  int s=Prime(a,b);
          if(a==1)//因为上面函数初始化的时候把所有的数都定义为素数
              s--;//其他的数的标记虽然改变但是1没变
          System.out.printf("Case %d: %d\n",++Case,s);
      }	
      sc.close();
    }
	static int Prime(long a,long b)
	{
	    for(long i=0; (long)i*i<=b; i++)
	        Isprime1[(int) i]=false;
	    for(long i=0; i<=b-a; i++)
	        Isprime2[(int) i]=true;
	    for(long i=2; (long)i*i<=b; i++)
	    {
	        if(!Isprime1[(int) i])
	        {
	            for(long j=2*i; (long)j*j<=b; j+=i)//筛出[2,sqrt(b)]的素数
	                Isprime1[(int) j]=true;
	            for(long j=max((long)2,(a+i-1)/i)*i; j<=b; j+=i)//筛出[a,b]的素数
	                Isprime2[(int) (j-a)]=false;//这里的2LL是2的长整型数,与2LL比较的意思就是j最小是i的两倍
	                                 //(a+i-1)/i表示的是[a,b]区间内的第一个数至少为i的多少倍.
	        }
	    }
	    int sum=0;
	    for(int i=0; i<b-a+1; i++)
	        if(Isprime2[i])
	            sum++;
	    return sum;
	}
	private static long max(long l, long m) {
		// TODO Auto-generated method stub
		if(l>m)
			return l;
		else
			return m;
	}  
}

实践是检验真理的唯一标准

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值