题目:

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

分析:求给的那个区间的素数个数,暴力枚举,数据太大。所以我们将区间放到[2,sqrt(b)]之后将区间转化到[a,b]上。

利用筛选法删除[a,b]里边的和数。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const int N=1e5+5;bool vis[N],visab[N];
int prime[N],cnt=0;
void is_prime()
{memset(vis,0,sizeof(vis));vis[1]=1;for(int i=2;i<N;i++){if(!vis[i]){prime[cnt++]=i;for(int j=i+i;j<N;j+=i)vis[j]=1;}}
}int main()
{int t;cin>>t;is_prime();for(int kase=1;kase<=t;kase++){LL a,b;scanf("%lld%lld",&a,&b);int count=0;if(b<=N-1){for(LL i=a;i<=b;i++){if(!vis[i])count++;}}else{memset(visab,0,sizeof(visab));for(int i=0;i<cnt&&prime[i]<=b;i++){LL k=a/prime[i];if(k*prime[i]<a)k++;for(LL j=k*prime[i];j<=b;j+=prime[i]){visab[j-a]=1;}}for(LL i=a;i<=b;i++){if(!visab[i-a])count++;}}printf("Case %d: %d\n",kase,count);}
}

Help Hanzo(区间素数筛)相关推荐

  1. HDU 6069 数论 区间素数筛 + 赛后反思

    题目链接 设 x = p 1 a 1 p 2 a 2 . . . . p n a n x = p_1^{a_1}p_2^{a_2}....p_n^{a_n} x=p1a1​​p2a2​​....pna ...

  2. M - Help Hanzo (大数区间素数筛)

    题目 思路:先对1e5之内的素数筛好,用这些素数筛去大区间内的合数. #include<cstdio> #include<cstring> #include<iostre ...

  3. M - Help Hanzo(模拟素数筛思想+优化)

    题目意思就是求[a,b]的素数个数.这道题我一看枚举不就完了吗? 但是看到那个2^31次方就知道了,如果枚举肯定TLE了: 所以百度了一下说模拟素数筛.我觉得我自己有必要回忆一下素数筛原理. #inc ...

  4. HDOJ 6069 素数筛

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意: 就是题目中的公式 题解: 这个题公式很好推 d(nk) = (kc1 + 1)(kc2 + ...

  5. Help Hanzo(素数筛)

    题意:本题就是求l~u之间有多少个素数: 分析:因为题数很大,所以不能用正常的素数筛法,只能先筛一部分,在用这些素数再筛出l~u之间的素数.本题和另一道题目很相似(传送门): 另一道题题目链接:htt ...

  6. 面试官本拿求素数搞我,但被我优雅的“回击“了(素数筛)

    原创公众号(希望能支持一下):bigsai 转载请联系bigsai 文章收录在github 求star 前言 现在的面试官,是无数开发者的梦魇,能够吊打面试官的属实不多,因为大部分面试官真的有那么那几 ...

  7. nssl1210-质数【素数筛】

    正题 题目大意 求l∼rl\sim rl∼r这个区间素数或两个素数的乘积的数个数 解题思路 在欧式筛的时候判断j是不是素数,是就标记就行了. code #pragma GCC optimize(2) ...

  8. 筛表合集(素数筛 欧拉函数筛 莫比乌斯函数筛)

    [目录] 一.素数筛 1.素数判断 2.素数普通筛 3.素数线性筛 4.素数区间筛 二.欧拉函数筛 三.莫比乌斯函数筛 [素数筛] 1.直接判定质数 bool judgePrime( int num ...

  9. 算法笔记--素数筛(朴素筛,埃式筛,欧拉筛)

    素数 素数也叫质数,是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数.如2 , 3 , 5 , 7 , 11等. 素数筛 素数筛即筛选出1~n内的素数的方法,这里介绍三种 常见的求素数 ...

最新文章

  1. python爬虫程序说明_Python即时网络爬虫:API说明
  2. Linux 高可用开源方案 Keepalived VS Heartbeat对比
  3. 什么是Zookeeper,Zookeeper的作用是什么,在Hadoop及hbase中具体作用是什么
  4. 如何评估深度学习模型效果?阿里工程师这么做
  5. 图的割点、桥与双连通分支
  6. Java实现算法导论中快速傅里叶变换FFT递归算法
  7. LeetCode第 3 场双周赛(2019.6.29)第一题:小于 K 的两数之和
  8. Codeforces 482 - Diverse Permutation 构造题
  9. SpringMV工作流程分析
  10. espcms /public/class_connector.php intval truncation Vul Arbitrary User Login
  11. 数组的顺序存储和实现
  12. python 生成文字图片_[ImageFont] 如何利用字体生成文字图片
  13. 让你的淘宝宝贝上首页,每月销量1000+的淘宝排名算法技巧
  14. 移动端html5广告的优势,移动端H5广告的互动类型探析
  15. mysql附加数据库
  16. win10搭建网络代理,供Linux使用
  17. MySQL 查看每个 IP 的连接数
  18. 在美国,男 / 女卫生间(厕所)的正确称呼为什么?请用英语写出答案。
  19. ava web 开发过程中常见的一些错误
  20. linux绝育玩客云_绝育老母鸡(玩客云)PT下载补充。如何过新手考核

热门文章

  1. eclipse和myeclipse的he user operation is wating问题
  2. 慕课网风袖小程序 一一第一阶段
  3. thinkpadE430c加装固态硬盘小记
  4. 行测-判断推理-类比推理-语法关系
  5. 低配置系统安装 linux,在低配置机中安装Linux系统
  6. 全球及中国冶金工业市场产量分析及投资战略决策报告2021版
  7. 什么是非关系型数据库,Redis概述、安装及部署Redis群集
  8. 配置VScode上基于WSL的lc3汇编语言环境
  9. 在王者荣耀角度下分析面向对象程序设计B中23种设计模式之策略模式
  10. CH341a烧录器烧录华硕BIOS