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

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

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, ...

题目大意:给定一个区间问你区间内有多少个素数。

思路:先进行素数打表。小于b的合数肯定有一个小于根号b的一个素数因子。然后把这些合数筛出来,筛出来的数组大小为这个数减去a的数,假设a为第一个位置的数要不然数组中存不下来。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e6+100;
ll prime[maxn],f[maxn];
bool vis[maxn];
int cnt=0;
void get()
{memset(prime,0,sizeof(prime));memset(vis,false,sizeof(vis));for(int i=2;i<=maxn;i++){if(!vis[i]){prime[cnt++]=i;}for(int j=0;j<cnt&&prime[j]*i<=maxn;j++){vis[i*prime[j]]=true;if(i%prime[j]==0)break;}}
}
int main()
{get();int t;scanf("%d",&t);for(int kcase=1;kcase<=t;kcase++){ll a,b;memset(f,0,sizeof(f));scanf("%lld %lld",&a,&b);ll ans=b-a+1;for(ll i=0;i<cnt&&prime[i]*prime[i]<=b;i++){for(ll j=a/prime[i]*prime[i];j<=b;j+=prime[i]){if(j>=a&&j>prime[i]){f[j-a]=1;}}}for(int i=0;i<=b-a;i++){if(f[i]){ans--;}}cout<<"Case "<<kcase<<": "<<(a==1?ans-1:ans)<<endl;}
}

Help Hanzo LightOJ - 1197相关推荐

  1. Help Hanzo LightOJ - 1197(素数筛法)

    Help Hanzo LightOJ - 1197 题目链接: https://cn.vjudge.net/problem/26909/origin 题意: 求两个数a,b之间有多少个素数. 思路: ...

  2. Help Hanzo LightOJ - 1197 (素数 筛法)

    题目地址:http://lightoj.com/volume_showproblem.php?problem=1197 求区间[a,b]之间有多少素数, (1 ≤ a ≤ b < 231, b ...

  3. LightOJ 1197 Help Hanzo

    LightOJ 1197 Help Hanzo 题目链接 Amakusa, the evil spiritual leader has captured the beautiful princess ...

  4. Help Hanzo(LightOJ - 1197)(欧拉筛 + 思维)

    LightOJ - 1197 Help Hanzo 来源:LightOJ - 1197 Help Hanzo 题意: 判断a ~ b区间内,有多少个素数,a,b范围[1, 2 ^ 31 - 1],b ...

  5. E - Help Hanzo(LightOJ 1197)

    传送门 Pssword: nefu DescriptionAmakusa, the evil spiritual leader has captured the beautiful princess ...

  6. LightOJ 1197 Help Hanzo 素数筛

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

  7. 【代码超详解】LightOJ 1197 Help Hanzo(区间质数筛法)

    一.题目描述 二.算法分析说明与代码编写指导 对于求指定区间 [a, b] 的质数的题目,通常 a 和 b 都比较大,而 b - a 不太大. 采用埃氏筛或者欧拉筛的代码,一般都会同时给出前若干个质数 ...

  8. “kuangbin带你飞”专题计划——专题十四:数论基础

    写在前面 1.目前还没啥写的.开始时间:2021-05-13(其实博客上看得到该博客创建时间的) 2.上一个专题刷的是网络流(博客总结),属于第一次接触.本来想的是一周特别高效,然后一周略划水,结果是 ...

  9. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

最新文章

  1. NET 2005 中通过TryParse来检验和转换数据类型。
  2. 计算机应用基础寒假作业,计算机应用基础理论试卷寒假作业.doc
  3. C语言试题四十三之求出ss所指字符串中指定字符的个数,并返回此值。
  4. 结对项目开发之电梯调度问题
  5. 脉搏波相关产品的介绍
  6. 每周更新:React UI 库、WebStorm 2019.1.1、PaySDK v2.0.1和Spring Boot 2.2 第二个里程碑 M2等更新发布
  7. ZYNQ PL开发流程
  8. MFC+Opencv实现图片视频处理和人脸识别
  9. php存省市,PHP格式化全国省市区列表
  10. FusionSphere 物理CPU与VCPU的关系梳理总结
  11. 数据库(mysql)查询语句练习(中级)
  12. 网易云音乐评论抓取(js逆向)
  13. 对话李春龙:如何用Kubernetes管理有状态服务
  14. Android高版本上传图片出现旋转问题(三星手机,小米8)
  15. 直接在Visual Studio代码编辑器中加密字符串文件
  16. 《统计学》——思考题第二章数据的搜集(贾俊平)
  17. Android——多进程
  18. 还原android系统文件夹,如何从Android的内存中恢复文件-万兴恢复专家
  19. 区块链中的全节点与轻量级节点
  20. 基于Python的人脸识别方法

热门文章

  1. linux r7 4800u,r7 4800u和r7 4800h差距大吗?下面解读可以帮您
  2. 《你若安好,便是晴天》
  3. Python学习之路5☞文件处理
  4. Android 最简单的自定义证件照Mask之一
  5. Matter协议特性解析(一) 支持非Matter协议,私有协议,Matter Bridge技术分析
  6. PADS VX2.8 基本规则的设置方法
  7. 【Linux】Linux 磁盘与文件系统管理命令
  8. 常用的17大学术搜索引擎
  9. 在王者荣耀角度下分析面向对象程序设计B中23种设计模式之原型模式
  10. 老男孩上海校区Python面试题