E - Help Hanzo

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

题意:t组数据,每组数据给定a和b,求ab之间素数的个数。

题解:ab范围是2的31次方,打10^5的素数表筛选即可。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+;
ll prime[maxn],cnt=;
bool isprime[maxn];
void get() //素筛模板 细节问题要注意
{
for(int i=; i<=maxn; i++)
{
if(!isprime[i])
{
prime[cnt++]=i;
for(int j=i+i; j<=maxn; j+=i)
isprime[j]=true;
}
}
}
int main()
{
get();
int t,cas=;
cin>>t;
while(t--)
{
ll a,b; //用int会Re 能long long 就long long
cin>>a>>b;
if(a<) //这里一定要注意 没有这句会wa 1 这个数要跳过去
a=; //1和0既非素数也非合数
bool vis[maxn];
memset(vis,,sizeof(vis));
for(int i=; prime[i]*prime[i]<=b; i++)
{
ll j;
j=prime[i]*(a/prime[i]);
if(j<a) //这句话相对于上一句一定要后加上 因为a=18 18/2*2=18 不需要加
j+=prime[i];
if(j==prime[i]) //这句话相当于上一句一定要后加上 因为a=2 2/5*5=0 0+5=5 5+5=10才对
j+=prime[i];
for(; j<=b; j+=prime[i])
vis[j-a]=;
}
ll ans=;
for(int i=a; i<=b; i++)
if(!vis[i-a])
ans++;
printf("Case %d: %d\n",cas++,ans);
}
return ;
}

LightOJ 1197 Help Hanzo(区间素数筛选)的更多相关文章

  1. LightOj 1197 Help Hanzo 区间素数筛

    题意: 给定一个区间a,b,a-b>=100000,1<=a<=b<=231,求出给定a,b区间内的素数的个数 区间素数筛 (a+i-1)/ ii向上取整,当a为 i 的整数倍 ...

  2. LightOj 1197 - Help Hanzo(分段筛选法 求区间素数个数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1197 题意:给你两个数 a b,求区间 [a, b]内素数的个数, a and b ( ...

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

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

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

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

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

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

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

    题目链接:https://vjudge.net/problem/LightOJ-1197 1197 - Help Hanzo    PDF (English) Statistics Forum Tim ...

  7. LightOj 1236 Pairs Forming LCM (素数筛选&&唯一分解定理)

    题目大意: 有一个数n,满足lcm(i,j)==n并且i<=j时,(i,j)有多少种情况? 解题思路: n可以表示为:n=p1^x1*p2^x1.....pk^xk. 假设lcm(a,b) == ...

  8. M - Help Hanzo LightOJ - 1197 (大区间求素数)

    题意: 求[a,b]之间的素数的个数 数很大...数组开不起 所以要想到转化 因为小于等于b的合数的最小质因子 一定小于等于sqrt(b),所以只需要求出来[0,sqrt(b)]的素数  然后取倍数删 ...

  9. LightOJ 1197 Help Hanzo 素数筛

    题意:筛一段区间内素数的个数,区间宽度10w,区间范围INT_MAX 分析:用sqrt(INT_MAX筛一遍即可),注意先筛下界,再筛上届,因为有可能包含 #include <cstdio> ...

随机推荐

  1. Ubuntu 14 常用“快捷键”,Ctrl + Alt + F1 进入终端,按 Ctrl + Alt + F7 回到界面

    Ubuntu中所谓 Super键,就是 Windows建,一般在键盘的 ctrl 和 alt 2个键之间,一个微软窗口的图标. 1.持续按住 Super键,会弹出“键盘快捷键”大全: 2.修改快捷键路 ...

  2. OS X 10.10.5编译Android5.1.1源码

    --------------------------------------------------写在前面---------------------------------------------- ...

  3. Android高仿微信(一)——如何消除启动时的白屏

    默认情况下,APP启动时会先把屏幕刷成白色,然后才绘制第一个Activity中的View,这两个步骤之间的延迟会造成启动后先看到白屏(时间大概为1秒左右).时间不长,但是我们也看到,一般的APP时不存 ...

  4. cocoapods pod install 安装报错 is not used in any concrete target

    低版本的cocoa pods在编写Podfile文件时这样写就可以了 platform :ios, '8.0'pod 'AFNetworking' 高版本的cocoa pods在编写Podfile文件 ...

  5. tomcat中配置jmx监控

    1.在tomcat的start.bat中添加下面代码, -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote. ...

  6. sqlite采用的ORM包

    关注了两个库的使用方式,一个是Dapper,一个是Simple.Data.考虑一个可选的是ORMLite, Dapper和simple.data都比较好用,动态对象的使用妙不可言,区别在于 //Dap ...

  7. 简述JavaScript的运行机制

    想要理解JavaScript的运行机制,需要分别深刻理解以下几个点: · JavaScript的单线程机制 · 任务队列(同步任务和异步任务) · 事件和回调函数 · 定时器 · Event Loop ...

  8. Laravel 5.1 文档攻略 —— Eloquent Collection

    简介 像all()和get(),还有一些处理模型关系这种会返回多条数据的方法,在Eloquent里面会返回一个collection对象集合(对象装在对象里),而不是像DQB的数组结果集合(对象装在数组 ...

  9. gulp学习笔记2-安装

    安装nodejs -> 全局安装gulp -> 项目安装gulp以及gulp插件 -> 配置gulpfile.js -> 运行任务 1.去nodejs官网安装nodejs 2. ...

  10. Linux下ffmpeg的各种编解码器的安装

    首先要安装各种解码器 1.lame  tar -zxvf lame- cd lame- ./configure --enable-shared make make install 2.libogg  ...