LightOJ1197 Help Hanzo —— 大区间素数筛选

题目链接:https://vjudge.net/problem/LightOJ-1197

1197 - Help Hanzo
Time Limit: 2 second(s) Memory Limit: 32 MB

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

Output for Sample Input

3

2 36

3 73

3 11

Case 1: 11

Case 2: 20

Case 3: 4

题解:

1. 可知如果要求出 1~n 内的素数,那么只需要知道 1~sqrt(n)内的素数,然后通过这些素数,就可以进一步筛选出 1~n 内的素数。因为把 i*j 筛掉, 其中i、j都是 sqrt(n) 内的素数,所以最大能筛到 sqrt(n) * sqrt(n)  = n 。

2.此题为单纯的大数区间筛选,由于n<=2^31,因此只需先求出 1e5内的素数,然后再对大区间进行筛选就可以了。

3.因为1不是素数,所以需要特判大区间是否从1开始。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int mod = 1e9+7;
17 const int MAXM = 1e5+10;
18 const int MAXN = 1e5+10;
19 
20 int prime[MAXN+10];
21 bool notprime[MAXN+10];
22 void getPrime()
23 {
24     memset(notprime, false, sizeof(notprime));
25     notprime[0] = notprime[1] = true;
26     for(int i = 2; i<=MAXN; i++)
27     {
28         if(!notprime[i]) prime[++prime[0]]=i;
29         for(int j = 1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
30         {
31             notprime[prime[j]*i] = 1;
32             if(i%prime[j]==0) break;
33         }
34     }
35 }
36 
37 void getPrime2(int L, int R)
38 {
39     memset(notprime, false, sizeof(notprime));
40     for(int i = 1; i<=prime[0]&& prime[i]<=R/prime[i]; i++)
41         for(int j = max(2,(int)ceil(1.0*L/prime[i])); j<=R/prime[i]; j++)
42             notprime[j*prime[i]-L+1] = true;
43 }
44 
45 int main()
46 {
47     getPrime();
48     int T, kase = 0;
49     scanf("%d", &T);
50     while(T--)
51     {
52         int L, R;
53         scanf("%d%d",&L,&R);
54         getPrime2(L, R);
55         int sum = 0;
56         for(int i = 1; i<=R-L+1; i++)
57             if(!notprime[i])
58                 sum++;
59 
60         if(L==1) sum--; //1不是素数,需要特判
61         printf("Case %d: %d
", ++kase, sum);
62     }
63 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8385646.html