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\n", ++kase, sum);
62     }
63 }
View Code

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8385646.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值