题目链接: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开始。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e5+; int prime[MAXN+];
bool notprime[MAXN+];
void getPrime()
{
memset(notprime, false, sizeof(notprime));
notprime[] = notprime[] = true;
for(int i = ; i<=MAXN; i++)
{
if(!notprime[i]) prime[++prime[]]=i;
for(int j = ; j<=prime[]&&prime[j]<=MAXN/i; j++)
{
notprime[prime[j]*i] = ;
if(i%prime[j]==) break;
}
}
} void getPrime2(int L, int R)
{
memset(notprime, false, sizeof(notprime));
for(int i = ; i<=prime[]&& prime[i]<=R/prime[i]; i++)
for(int j = max(,(int)ceil(1.0*L/prime[i])); j<=R/prime[i]; j++)
notprime[j*prime[i]-L+] = true;
} int main()
{
getPrime();
int T, kase = ;
scanf("%d", &T);
while(T--)
{
int L, R;
scanf("%d%d",&L,&R);
getPrime2(L, R);
int sum = ;
for(int i = ; i<=R-L+; i++)
if(!notprime[i])
sum++; if(L==) sum--; //1不是素数,需要特判
printf("Case %d: %d\n", ++kase, sum);
}
}

LightOJ1197 Help Hanzo —— 大区间素数筛选的更多相关文章

  1. LightOj 1197 Help Hanzo (区间素数筛选)

    题目大意: 给出T个实例,T<=200,给出[a,b]区间,问这个区间里面有多少个素数?(1 ≤ a ≤ b < 231, b - a ≤ 100000) 解题思路: 由于a,b的取值范围 ...

  2. LightOJ 1197 LightOJ 1197(大区间素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1197 题目大意: 就是给你一个区间[a,b]让你求这个区间素数的个数 但a.b的值太大没法直接进 ...

  3. LightOJ 1197 Help Hanzo(区间素数筛选)

    E - Help Hanzo Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit ...

  4. 大区间素数筛选 POJ2689

    题意: 给一个区间[L,U],(1<=L< U<=2,147,483,647),U-L<=1000000,求出[L,U]内距离近期和距离最远的素数对. 因为L,U都小于2^32 ...

  5. 大区间素数筛选(POJ 2689)

    /* *POJ 2689 Prime Distance *给出一个区间[L,U],找出区间内容.相邻的距离最近的两个素数和距离最远的两个素数 *1<=L<U<=2147483647 ...

  6. 2017ACM暑期多校联合训练 - Team 4 1003 HDU 6069 Counting Divisors (区间素数筛选+因子数)

    题目链接 Problem Description In mathematics, the function d(n) denotes the number of divisors of positiv ...

  7. M - Help Hanzo LightOJ - 1197 (大区间素数筛法)

    题解:素数区间问题.注意到a和b的范围是1<<31,所以直接暴力打表肯定不可以.如果一个数是合数,他的两个因子要么是两个sqrt(x),要么就分布在sqrt(x)两端,所以我们可以根据sq ...

  8. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  9. Prime Count 求大区间素数个数

    http://acm.gdufe.edu.cn/Problem/read/id/1333 https://www.zhihu.com/question/29580448/answer/44874605

随机推荐

  1. 分享Kali Linux 2017年第11周镜像文件

     分享Kali Linux 2017年第11周镜像文件 Kali?Linux官方于3月12日发布2017年的第11周镜像.这次维持了11个镜像文件的规模.默认的Gnome桌面的4个镜像,E17.KDE ...

  2. [Bzoj5358][Lydsy1805月赛]口算训练(预处理+动态开点线段树)

    5358: [Lydsy1805月赛]口算训练 Time Limit: 5 Sec  Memory Limit: 512 MBSubmit: 318  Solved: 105[Submit][Stat ...

  3. CodeForces - 600F Edge coloring of bipartite graph

    Discription You are given an undirected bipartite graph without multiple edges. You should paint the ...

  4. SpringBoot中@EnableAutoConfiguration注解用法收集

    参考: http://blog.csdn.net/xiaoyu411502/article/details/52770723 https://docs.spring.io/spring-boot/do ...

  5. android 打开软键盘

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  6. 【Linxu】CentOS7下安装程序报错:

    进入root用户,然后编辑 vi /usr/libexec/urlgrabber-ext-down 将首行换成 #!/usr/bin/python2.

  7. weblogic运维时经常遇到的问题和常用的配置

      希望这篇能把weblogic运维时经常遇到的问题.常用的配置汇总到一起. 1.配置jvm参数: 一般在domain启动过程中会看到以下启动的日志信息,如下图所示: 图中红色方框部分为启动weblo ...

  8. 转:国内外著名开源b2c电子商务系统比较包括asp.net和php

    from: http://longdick.iteye.com/blog/1122879 国内外著名开源b2c电子商务系统比较包括asp.net和php 博客分类: 电子商务   国内外著名开源b2c ...

  9. SolidEdge 工程图中如何给零件添加纹理或贴图

    格式-检视-勾选纹理   选中一个零件之后,点击格式-面,在纹理选项卡中找到纹理的贴图   最后效果如下图所示,如果不勾选检视纹理,则虽然的确贴图了,但是不显示出来给你看.如果贴图文件没了,也不会显示 ...

  10. 以面试官的角度看strcpy函数

    一:笔试或者面试的总结 之 一 (1)在笔试或者面试中常常会被问道,strcpy  memmove memcpy 函数的实现.有时也会问你STL 中string的 split 和 trim的实现.有的 ...